How to use logical not operator in complex conditional expression in Bash? [duplicate] How to use logical not operator in complex conditional expression in Bash? [duplicate] bash bash

How to use logical not operator in complex conditional expression in Bash? [duplicate]


if ! [[ $var == 2 || $var == 30 || $var == 50 ]] ; then  do somethingfi

Or:

if [[ ! ($var == 2 || $var == 30 || $var == 50) ]] ; then  do somethingfi

And a good practice is to have spaces between your conditional operators and operands.

Some could also suggest that if you're just comparing numbers, use an arithmetic operator instead, or just use (( )):

if ! [[ var -eq 2 || var -eq 30 || var -eq 50 ]] ; then  do somethingfiif ! (( var == 2 || var == 30 || var == 50 )) ; then  do somethingfi

Although it's not commendable or caution is to be given if $var could sometimes be not numeric or has no value or unset, since it could mean 0 as default or another value of another variable if it's a name of a variable.