Search code examples
bashbraces

Bash not comparing strings properly


This is my bash file

#!/bin/sh
ENV=DEV
echo "env: $ENV"
if [[ "$ENV" == DEV* ]]; then
    RUNTIME_CLASSPATH=$(cat ../build/dev.classpath)
    echo "cp: $RUNTIME_CLASSPATH"
fi
echo "done"

And here's the terminal output:

~proj/bin$ ./test.sh 
env: DEV
./test.sh: 7: [[: not found
done

I don't understand what's wrong. Is there some other way of doing string comparisons?


Solution

  • If you want to write a bash script, then don't write a POSIX shell script: change your shebang line to:

    #!/bin/bash
    

    On the other hand, if you want to write a portable shell script, use the case statement:

    case "$ENV" in 
      DEV*)
        RUNTIME_CLASSPATH=$(cat ../build/dev.classpath)
        echo "cp: $RUNTIME_CLASSPATH"
        ;;
    esac