Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Post Timeline

I have a string in Bash:

string="My string"
string="My string"

How can I test if it contains another string?

if [ $string ?? 'foo' ]; then
 echo "It's there!"
fi
if [ $string ?? 'foo' ]; then
 echo "It's there!"
fi

Where ?? is my unknown operator. Do I use echoecho and grep?

if echo "$string" | grep 'foo'; then
 echo "It's there!"
fi
if echo "$string" | grep 'foo'; then
 echo "It's there!"
fi

That looks a bit clumsy.

I have a string in Bash:

string="My string"

How can I test if it contains another string?

if [ $string ?? 'foo' ]; then
 echo "It's there!"
fi

Where ?? is my unknown operator. Do I use echo and grep?

if echo "$string" | grep 'foo'; then
 echo "It's there!"
fi

That looks a bit clumsy.

I have a string in Bash:

string="My string"

How can I test if it contains another string?

if [ $string ?? 'foo' ]; then
 echo "It's there!"
fi

Where ?? is my unknown operator. Do I use echo and grep?

if echo "$string" | grep 'foo'; then
 echo "It's there!"
fi

That looks a bit clumsy.

edited tags
Link
S.S. Anne
  • 15.7k
  • 8
  • 43
  • 82
edited title
Link
codeforester
  • 43.8k
  • 21
  • 123
  • 159

String How to check if a string contains a substring in Bash

Tweak title.
Link
jww
  • 103.7k
  • 107
  • 454
  • 975
Loading
deleted 8 characters in body
Source Link
codeforester
  • 43.8k
  • 21
  • 123
  • 159
Loading
edited tags
Link
Amir
  • 11.1k
  • 10
  • 52
  • 76
Loading
Copy edited (e.g. ref. <http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29>).
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 110
  • 134
Loading
Remove coincidental newbie problems from the code
Source Link
tripleee
  • 191.9k
  • 37
  • 319
  • 370
Loading
added 4 characters in body
Source Link
Jahid
  • 22.6k
  • 10
  • 97
  • 114
Loading
Pipe, not logical or.
Source Link
Linus Kleen
  • 34.8k
  • 10
  • 97
  • 100
Loading
Question Protected by Community Bot
Source Link
davidsheldon
  • 40.4k
  • 4
  • 30
  • 28
Loading
lang-bash

AltStyle によって変換されたページ (->オリジナル) /