String comparison in bash. [[: not found

BashShellUbuntu 11.04String Comparison

Bash Problem Overview


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

#!/bin/bash
string='My string';

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

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

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

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

In both cases I receive error:

[[: not found

What am I doing wrong? [1]: https://stackoverflow.com/questions/229551/string-contains-in-bash [2]: http://ubuntuforums.org/showthread.php?t=562398

Bash Solutions


Solution 1 - Bash

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

From a comment:

Add #!/bin/bash at the top of file

Solution 2 - Bash

How you are running your script? If you did with

$ sh myscript

you should try:

$ bash myscript

or, if the script is executable:

$ ./myscript

sh and bash are two different shells. While in the first case you are passing your script as an argument to the sh interpreter, in the second case you decide on the very first line which interpreter will be used.

Solution 3 - Bash

Is the first line in your script:

#!/bin/bash

or

#!/bin/sh

the sh shell produces this error messages, not bash

Solution 4 - Bash

As @Ansgar mentioned, [[ is a bashism, ie built into Bash and not available for other shells. If you want your script to be portable, use [. Comparisons will also need a different syntax: change == to =.

if [ $MYVAR = "myvalue" ]; then
    echo "true"
else
    echo "false"
fi

Solution 5 - Bash

I had this problem when installing Heroku Toolbelt

This is how I solved the problem

$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 ago 15  2012 /bin/sh -> dash

As you can see, /bin/sh is a link to "dash" (not bash), and [[ is bash syntactic sugarness. So I just replaced the link to /bin/bash. Careful using rm like this in your system!

$ sudo rm /bin/sh
$ sudo ln -s /bin/bash /bin/sh

Solution 6 - Bash

If you know you're on bash, and still get this error, make sure you write the if with spaces.

[[1==1]] # This outputs error

[[ 1==1 ]] # OK

Solution 7 - Bash

Specify bash instead of sh when running the script. I personally noticed they are different under ubuntu 12.10:

bash script.sh arg0 ... argn

Solution 8 - Bash

Execute in your terminal:

sudo update-alternatives --install /bin/sh sh /bin/bash 100

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1581900View Question on Stackoverflow
Solution 1 - BashAnsgar WiechersView Answer on Stackoverflow
Solution 2 - BashAkos KView Answer on Stackoverflow
Solution 3 - BashWileyView Answer on Stackoverflow
Solution 4 - BashAmedee Van GasseView Answer on Stackoverflow
Solution 5 - BashjperelliView Answer on Stackoverflow
Solution 6 - BashRichard MillerView Answer on Stackoverflow
Solution 7 - BashSmeterlinkView Answer on Stackoverflow
Solution 8 - BashAntonio MorenoView Answer on Stackoverflow