I have the following piece of code where I am trying to match the word "test" in the given string:
str="some test string"
if [ $str == *"test"* ]; then # [: too many arguments
echo "string was found"
else
echo "string was not found"
fi
Output:
string was not found
Bash version:
GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)
What am I doing wrong?
if [[ "$str" == *"test"* ]]; then(Note the quotes around$strand the double[[ ]])[, the LHS operand always needs to be quoted"$str"[ ]and[[ ]]have significantly different syntaxes, and only[[ ]]can do pattern matching like you're trying to do. You must switch to the double-bracket version for this. See BashFAQ #13: "What is the difference betweentest,[and[[?" and the Unix & Linux question "What is the difference between the Bash operators[[vs[vs(vs((?"