84

I'm working with a bash script that is currently working on a server (RHEL4). I'm developing on my laptop with Ubuntu 10.04, but I don't think the platform is causing the problem.

Here's what's happening: I have a skeleton script that calls another script that does most of the work. However, it makes calls to getConfig.sh a lot. getConfig.sh basically just parses some command line argument (using getopts) and calls a Java program to parse some XML files. Anyways, getConfig.sh is throwing up lots of errors (but still seems to work).

Here's the message that I'm getting

getconfig.sh: 89: [[: not found
getconfig.sh: 89: [[: not found
getconfig.sh: 94: [[: not found

I get those three errors every time it runs; however, the script completes and the Java code runs.

Here's the relavent code section

parseOptions $*

if [[ "${debugMode}" == "true" ]] ; then
    DEBUG="-DDEBUG=true"
    echo "${JAVA_HOME}/bin/java ${DEBUG} -Djava.endorsed.dirs=${JAXP_HOME} -jar $(dirname $0)/GetXPath.jar ${XML_File} ${XPath_Query}"
fi

Line 89 is "parseOptions $* and line 94 is "fi"

Thanks for the answers.

Justin
  • 843
  • 1
  • 6
  • 4

3 Answers3

181

If your script is executable and you are executing it like ./getconfig.sh, the first line of your script needs to be:

#!/bin/bash

Without that shebang line, your script will be interpreted by sh which doesn't understand [[ in if statements.

Otherwise, you should run your script like bash getconfig.sh, not sh getconfig.sh. Even if your default shell is bash, scripts run with sh will use a reduced set of bash's features, in order to be more compliant with the POSIX standard. [[ is one of the features that is disabled.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • 2
    Thanks Dennis. The dufus who originally wrote this code used #!/bin/sh on all the scripts. I missed changing this one. – Justin Aug 03 '10 at 23:17
  • 3
    I am seeing this same issue, but i have `#!/bin/bash` at the top of my script. – prolink007 Oct 19 '12 at 16:40
  • 19
    I figured out the issue. If you are calling the script from another script make sure you are using `bash nameofscript.sh` instead of `sh nameofscript.sh`. – prolink007 Oct 19 '12 at 16:49
  • 1
    Ugh.. Thanks for pointing out the obvious. Mine said #!/bin/sh. How did that get there?! – Tony Fraser Sep 18 '20 at 00:09
  • `$ sudo rm /bin/sh` and execute `$ sudo ln -s /bin/bash /bin/sh` – Antonio Moreno Apr 29 '21 at 19:05
  • @AntonioMoreno: This could cause issues and would likely be reverted during a system update. It's better to not delete the file and put the symlink in `/usr/local/bin` and have that directory earlier in your `PATH` than `/bin`. – Dennis Williamson Jun 26 '22 at 16:09
  • @DennisWilliamson Thank you , this is one of those thing that you need to look for when the hours are getting long... – soBusted Dec 15 '22 at 16:56
42

Use:

bash scriptname.sh

instead of:

sh scriptname.sh

Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
-1

If you are checking for equality, shouldn't the if be ?

if [[ "${debugMode}" = "true" ]]; then .... fi

Gangadhar
  • 1,893
  • 9
  • 9