1

I am trying to write a script but it is giving the above error

 if [ [ [ "$1" != "abc" ] && [ "$1" != "def" ] ] || [ [ "$2" != "1" ] && [ "$2" != "0" ] ] ];
 then
    echo "Hello World"
 fi
3
  • 1
    You should use more meaningful variable's name as it will be hard for you (or other) to understand your code. For instance, at the beginning of you string do: fish=$1; shark=$2 Commented Aug 29, 2014 at 8:20
  • As @anubhava answer suggest Bash syntax is more powerful than POSIX shell. However it is less portable. Commented Aug 29, 2014 at 8:22
  • [ is not a grouping operator; it's a command name, and as such you cannot nest them. Commented Aug 29, 2014 at 13:18

1 Answer 1

2

Be careful with && and ||. You can simplify it to this in BASH:

if [[ "$1" != "abc" && "$1" != "def" ]] || [[ "$2" != "1" && "$2" != "0" ]];
then
    echo "Hello World"
fi
Sign up to request clarification or add additional context in comments.

2 Comments

Or even [[ ("$1" != "abc" && "$1" != "def" ) || ( "$2" != "1" && "$2" != "0" ) ]], which is equivalent to [[ "$1" != "abc" && "$1" != "def" || "$2" != "1" && "$2" != "0" ]].
Yes sure [[ ( "$1" != "abc" && "$1" != "def" ) || ( "$2" != "1" && "$2" != "0" ) ]] should also work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.