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

Sam
  • 1
  • 1
  • 2
  • 2
    Use `!=` instead of `=!` (and you also want to use `-a` instead of `-o`) – that other guy Jul 07 '21 at 20:45
  • 2
    Concerning `-a` vs `-o`, see ["Why non-equality check of one variable against many values always returns true?"](https://stackoverflow.com/questions/26337003/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. – Gordon Davisson Jul 07 '21 at 20:47
  • 1
    Mind, this whole thing is a better use for a `case` statement. – Charles Duffy Jul 07 '21 at 20:47
  • 1
    `case $variable in hello|hi) :;; *) echo "variable isn't hi or hello";; esac` – Charles Duffy Jul 07 '21 at 20:48
  • thanks, i used ```[ "$variable" != "hello" ] && [ "$variable" != "hi" ]``` and it worked – Sam Jul 07 '21 at 20:56

0 Answers0