ChatGPT解决这个技术问题 Extra ChatGPT

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:

#!/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 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?

I might do it wrong, but using a single square bracket always worked for me.
What does /bin/bash --version print?
[ won't work in this case, because it doesn't support patterns.

A
Abhay

[[ 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


if I type: type '[[' i get [[ is a shell keyword
Do you get the same result when you run /bin/bash -c "type [["?
This happened to me because I forgot to add #!/bin/bash at the top of my file
Happend to me because i ran the script with sh, lots for frustration. Now i just have to redo everything i undid before finding this answer.
I wish I could upvote you more... So much time lost.
F
Fermin Silva

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.


i got permission denied this way. with sudo ./myscript its command not found
do chmod +x myscript, then run again, you don't need sudo
How you are invoking you script?
like you said. normally i call it with '$sh myscipt.sh'. 2nd time after doing chmod +x myscript.sh i called it with ./myscript.sh
The error is expected when you run the script via sh myscript.sh, because /bin/sh emulates a Bourne shell where [[ is not a builtin. However, running the script via ./script.sh should not yield an error, because in that case the shebang should cause /bin/bash to be used.
K
Kevin

Is the first line in your script:

#!/bin/bash

or

#!/bin/sh

the sh shell produces this error messages, not bash


First line was also missing for me producing the error the author mentioned!
E
Etienne Gautier

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

Checked a bit with Ubuntu 16.04 and (sh ->) dash 0.5.8-2.1ubuntu2 and found that [ works fine in combination with "-eq" and alikes. Similar information on comparison operators for this context can be found here: stackoverflow.com/questions/10849297/… - I further cheked with (( )) evaluation operator as recommended in the next link but that seemed to fail for my minimal invasive changes /or/ it would need a much deeper level of code changes. softpanorama.org/Scripting/Shellorama/Control_structures/…
j
jperelli

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

Overriding the default sh for your distro is ill-advised IMHO. A shell which is run with sh should work with dash; if it doesn't, that's a bug in the script. If you need bash features, use bash, not sh.
this sounds terrible tbh
I agree, it was just an ugly workaround. A similar but better workaround would be to use update-alternatives (in debian-based-linux) like this justinconover.wordpress.com/2012/05/14/… but in the end, it would be the same.
R
Richard Miller

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

S
Smeterlink

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

bash script.sh arg0 ... argn


This fixed it for me!
A
Antonio Moreno

Execute in your terminal:

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