In Bash, figuring out how you interpret the term if not condition before digging into the solutions is mandatory. For example, are you taking as negating the condition/expression, which means make the condition/expression False
if it is True
and vice versa? Or, you want to make some comparisons to assess inequality; this comparison can be of numbers or strings. We covered all the use cases below, and you can jump around to find your desired solution.
Comparing Numbers/Strings for Inequality
Use the -ne
operator to check if two numbers are not equal to each other in Bash.
1 2 3 4 5 6 7 8 9 |
#!/bin/bash number1=8 number2=3 if [ $number1 -ne $number2 ] then echo "The '$number1' and '$number2' are not equal." fi |
1 2 3 |
The '8' and '3' are not equal. |
Use !=
operator to check if strings are not equal to each other in Bash.
1 2 3 4 5 6 7 8 9 |
#!/bin/bash string1='Mehmood' string2='Ali' if [ $string1 != $string2 ] then echo "The '$string1' and '$string2' are not equal." fi |
1 2 3 |
The 'Mehmood' and 'Ali' are not equal. |
We can also assign string values to variables without wrapping them around with single or double quotes. See the following example.
1 2 3 4 5 6 7 8 9 |
#!/bin/bash string1=Mehmood string2=Ali if [ $string1 != $string2 ] then echo "The '$string1' and '$string2' are not equal." fi |
1 2 3 |
The 'Mehmood' and 'Ali' are not equal. |
Don’t add white space before and/or after the
=
sign while assigning value to the variables, such asstring1= 'Mehmood'
orstring1 = Mehmood
; otherwise, you will get an error illustrating the command not found.
Negating the Condition/Expression
Use the !
operator to negate the value of the specified condition meaning turn the resulting value to True
if it is False
and vice versa.
1 2 3 4 5 6 7 8 9 10 11 |
#!/bin/bash number1=8 number2=3 if ! [ $number1 -ne $number2 ] then echo "The '$number1' and '$number2' are not equal." else echo "The '$number1' and '$number2' are equal." fi |
1 2 3 |
The '8' and '3' are equal. |
This example is similar to the one where we compared two numbers using the -ne
operator. In the above code, we negated the condition, which means the output of the [ $number1 -ne $number2 ]
condition was True
because 8
is not equal to 3
, but we turned this result into False
using !
operator; that’s why if
block was not executed but else
block.
Similarly, use the !
operator to negate the value of the specified condition for string values by turning the resulting value to True
if it is False
and vice versa.
1 2 3 4 5 6 7 8 9 |
#!/bin/bash string1=Mehmood string2=Ali if ! [ $string1 != $string2 ] then echo "The '$string1' and '$string2' are equal." fi |
For this code, the [ $string1 != $string2 ]
resulted in True
, but the !
operator made it False
, so the echo
command within the if
block was not executed and we didn’t get any output. So let’s practice the above example with an expression as follows.
Use the !
operator to negate the value of the specified expression for string values by turning the resulting value to True
if it is False
and vice versa.
1 2 3 4 5 6 7 8 9 10 |
#!/bin/bash string=Mehmood if ! [ -z "$string" ] then echo "The specified string is not empty." else echo "The specified string is empty." fi |
1 2 3 |
The specified string is not empty. |
Here, we used -z
to check if the specified string is empty, so the [ -z "$string"]
expression returned False
because $string
is not empty, but it was turned to True
due to using !
operator and resulted in if
block execution; otherwise, the else
block would be executed.