Bash If statement with NOT Operator

We can use NOT logical operator to invert a condition in an If statement in Bash scripting.

The syntax to use NOT logical operator in If statement’s condition is

if [ ! condition ];
then
    statement(s)
fi

Reference Bash If Statement.

Example

In the following script, we check if given number is greater than 10 or even number. Here we have two conditions, and we join these two conditions using OR logical operator.

example.sh

#! /bin/bash
 
num=8

if [ ! $num -gt 10 ];
then
    echo "Number is not greater than 10."
fi

Output

sh-3.2# ./example.sh 
Number is not greater than 10.
ADVERTISEMENT

Conclusion

In this Bash Tutorial, we learned how to use OR logical operator in If statement, with the help of syntax and examples.