Kevin
Kevin

Reputation: 1687

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?

Upvotes: 1

Views: 131

Answers (2)

glenn jackman
glenn jackman

Reputation: 247012

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

Upvotes: 5

nullshow
nullshow

Reputation: 19

Change

if [[ "$ENV" == DEV* ]]; then

to

if [ "$ENV" == "DEV" ]; then

.

Upvotes: -1

Related Questions