0

I am trying to test if a string is not equal to either of two strings. For example, in C++, this would be written as:

if (string1 != string2 || string1 != string3)

However, attempting to create the Bash equivalent

if [ "$string1" != "$string2" || "$string1" != "$string3" ]

results in the error [: missing ']' and (contents of string1): command not found.

I'm not exactly too sure what is causing it, but searching didn't turn up any results I could use. It would be greatly appreciated if one could point out how the code I am using can be changed to suit the needs, and if possible, provide sample code.

Thanks in advance! Harry

perhapsmaybeharry
  • 874
  • 3
  • 13
  • 32

1 Answers1

1

At least three possibilities here:

if [ "$string1" != "$string2" ] || [ "$string1" != "$string3" ]

Or:

if [ "$string1" != "$string2" -o "$string1" != "$string3" ]

Or:

if [[ "$string1" != "$string2" || "$string1" != "$string3" ]]
julienc
  • 19,087
  • 17
  • 82
  • 82