user1581900 user1581900 - 10 months ago 55
Bash Question

String comparison in bash. [[: not found

I am trying to compare strings in bash. I already found an answer on how to do it on stackoverflow. In script I am trying, I am using the code submitted by Adam in the mentioned question:

string='My string';

if [[ "$string" == *My* ]]
echo "It's there!";

needle='y s'
if [[ "$string" == *"$needle"* ]]; then
echo "haystack '$string' contains needle '$needle'"

I also tried approach from ubuntuforums that you can find in 2nd post

if [[ $var =~ regexp ]]; then
#do something

In both cases I receive error:

[[: not found

What am I doing wrong?


[[ is a bash-builtin. Your /bin/bash doesn't seem to be an actual bash.