0

I am very new to shell scripting, and i have had this error. I am using a while loop that goes a bit like this:

while [ "$variable" =! "hello" -o "$variable" =! "hi" ]
do
        echo "variable isn't hi or hello"
done

but shell then gives the error [: too many arguments

5
  • 2
    Use != instead of =! (and you also want to use -a instead of -o) Commented Jul 7, 2021 at 20:45
  • 2
    Concerning -a vs -o, see "Why non-equality check of one variable against many values always returns true?" Also, both -a and -o can lead to parsing ambiguities; I'd recommend [ "$variable" != "hello" ] && [ "$variable" != "hi" ] instead. Commented Jul 7, 2021 at 20:47
  • 1
    Mind, this whole thing is a better use for a case statement. Commented Jul 7, 2021 at 20:47
  • 1
    case $variable in hello|hi) :;; *) echo "variable isn't hi or hello";; esac Commented Jul 7, 2021 at 20:48
  • thanks, i used [ "$variable" != "hello" ] && [ "$variable" != "hi" ] and it worked Commented Jul 7, 2021 at 20:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.