Negate an if Condition in a Bash Script in Linux


To negate an "if" condition in a Bash script in Linux, you can use the "!" operator. For example, if you have an "if" statement that checks if a variable "x" is equal to 5, you can negate that condition by using "if [ ! $x -eq 5 ]" instead. This will run the commands inside the "if" block if the condition is not true (i.e. if "x" is not equal to 5).

Integer Comparison

In Bash, you can use the following operators to compare integers −

  • eq (equal to)

  • ne (not equal to)

  • gt (greater than)

  • ge (greater than or equal to)

  • lt (less than)

  • le (less than or equal to)

For example, you can use the "-eq" operator to check if a variable "x" is equal to 5 −

if [ $x -eq 5 ]
then
   echo "x is equal to 5"
fi

You can also use the "-ne" operator to check if a variable "x" is not equal to 5 −

if [ $x -ne 5 ]
then
   echo "x is not equal to 5"
fi

You can use other comparison operator similarly.

Please note that the spaces around the square brackets and the comparison operator are important, and the variable must be enclosed in double quotes when you are comparing with string.

String Comparison

In Bash, you can use the following operators to compare strings −

  • = (equal to)

  • != (not equal to)

  • -z (string is empty)

  • -n (string is not empty)

For example, you can use the "=" operator to check if a variable "s" is equal to "hello" −

if [ "$s" = "hello" ]
then
   echo "s is equal to hello"
fi

You can also use the "!=" operator to check if a variable "s" is not equal to "hello" −

if [ "$s" != "hello" ]
then
   echo "s is not equal to hello"
fi

You can also use the "-z" operator to check if a string is empty −

if [ -z "$s" ]
then
   echo "s is an empty string"
fi

And you can use the "-n" operator to check if a string is not empty −

if [ -n "$s" ]
then
   echo "s is not an empty string"
fi

Please note that the spaces around the square brackets and the comparison operator are important, and the variable must be enclosed in double quotes.

Negation

In Bash, you can negate a condition using the "!" operator.

For example, you can negate a comparison of a variable "x" being equal to 5 using the "!" −

if [ ! "$x" -eq 5 ]
then
   echo "x is not equal to 5"
fi

You can also negate a string comparison using the "!" −

if [ ! "$s" = "hello" ]
then
   echo "s is not equal to hello"
fi

You can negate the result of any command or condition with the "!" operator.

Additionally, you can use double negation to achieve the same effect, like this −

if [[ "$x" != 5 ]]
then
   echo "x is not equal to 5"
fi

It's also worth noting that you can use [[...]] instead of [...] for string and numeric comparison. It's more readable and flexible.

Please note that the spaces around the square brackets and the comparison operator are important, and the variable must be enclosed in double quotes.

Conclusion

In Bash, you can use different comparison operators to check for equality, inequality, greater than, less than, and other conditions. These operators can be used with variables containing integers or strings. Additionally, you can negate a condition using the "!" operator to check if it is false. [[...]] is also a way to check the condition and more readable and flexible. It's important to note that the spaces around the square brackets and the comparison operator are important, and the variable must be enclosed in double quotes when you are comparing with string.

Updated on: 25-Jan-2023

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements