Bash If Not Operator With Examples
Bash platform is a bonus for new Linux users who want to get hands-on experience in programming. It allows you to use any statements, loops, and different options to get different results. One of those statements is the “if” statement that is used to execute a part of a code until a certain condition is true. Just like that, the “if-not” condition is also considered to do the same things while using the exact opposition condition.
For this, we need to utilize the not operator “!” with the “if” statement in the bash script. Let’s discuss the use of the “if-not” operator in Bash programming with the help of some examples. Get started with the new Bash file creation while using the terminal shell of the Ubuntu 20.04 system as per the touch query.
Example 1
Within the first Bash example of this article, we will be using the “if-not” operator to check which one of the two strings is less than the other. For this, we have added the Bash support within the file and added two new string variables v1 and v2. The value of v1 is a little greater than the value of v2. After this, we have started the “if” statement with the “!” operator to check the condition among the two string variables i.e., if the value of v1 is less than v2 or not.
As we have been using the “!” operator with the “if” statement, we need to set the results according to the situation. We have to set the echo statement to “v2 is less than v1” for the ‘then’ part of this condition. This is because when the “
After executing this code with Bash instruction, we have got to know that the results will be altered according to the condition specified and are accurate.
Example 2
Let’s use the “if-not” operator to check the equality of two integer variables. For this, we will be updating our code as shown. We have initialized two integer variables v1 and v2 with the integer values. We have used the “if” statement with the “not” operator to check the condition of whether the two integer variable values are equal or not. For checking their equality, we have been using the “-eq” operator of Bash within the condition clause. If the two values are equal and the “-eq” operator returns “true”, the “!” operator will reverse it and make it “false”. Thus, the “else” part will be executed stating “EQUAL” from the echo statement.
If the condition”-eq” returns “false”, the “!” operator will make it “true” and the echo statement from the “then” part will display “NOT EQUAL” as a result.
After running this Bash code, we have got the result “NOT EQUAL” as v1 is not the same as v2.
Let’s update this code a little bit by adding the same value to both integer variables i.e., v1=14 and v2=14. This time, we have also updated the inner condition for two variables. So, we have been using the “not equal” operator i.e., “-ne” to check if the two values are not equal. The “!” operator is also used within the “if” statement. If the “-ne” operator returns “true” the “!” operator will reverse it by “false” and the else part will be executed.
On the contrary, if the “-ne” operator returns “false”, the “!” operator will make it “true” and the “then” part will be executed. According to variables, the “then” part must be executed and display “EQUAL”.
After running this Bash code, we have come to know that the result is the same as expected.
Example 3
Let’s try the “if-not” operator to check a different condition this time. This time, we have been using the “-z” operator to check whether the variable is empty or not. For this, we have started the code with the initialization of an empty variable “v”. The “if-not” operator condition is checking whether the variable “v” is empty or not using the “-z” option here. The condition will display “Not Empty” upon getting the “true” from the “if-not” operator condition. Else, it will display “Empty” after getting the “false” as a return value from the “-z” option.
After running this Bash code, we have got “Empty” as the variable “v” is empty.
Example 4
Within our last example, we will be checking whether the simple Bash file is located in the current home directory or other folders or not. For this, we will be using the “-f” option within the if-not operator condition. So, we have initialized a FILE variable with the file location as “/home/Linux/ifnot.sh”. The “if” statement with the “!” operator will be used to reverse the result of the condition in the square brackets. The “-f” option is checking whether the given FILE variable contains a file or not. If so, the “then” and “else” parts of the statement will be executed according to the condition returned value i.e., “true” or “false”.
After executing this code, we got the “It’s a Bash file” message.
Conclusion
This article is all about the use of the “if-not” condition within the Bash script with the use of simple Bash examples. We have tried it using many options of Bash like “-z”, “-f”, “-ne”, -“eq”, and “
About the author
Omar Farooq
Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.
How to make «if not true condition»?
I would like to have the echo command executed when cat /etc/passwd | grep «sysa» is not true. What am I doing wrong?
if ! [ $(cat /etc/passwd | grep "sysa") ]; then echo "ERROR - The user sysa could not be looked up" exit 2 fi
@acraig5075 it’s valid either way, but there’s no need for a test command (which is what the brackets are) in this statement at all.
8 Answers 8
if ! grep -q sysa /etc/passwd ; then
grep returns true if it finds the search target, and false if it doesn’t.
So NOT false ( ! false ) == true .
if evaluation in shells are designed to be very flexible, and many times doesn’t require chains of commands (as you have written).
Also, looking at your code as is, your use of the $( . ) form of cmd-substitution is to be commended, but think about what is coming out of the process. Try echo $(cat /etc/passwd | grep «sysa») to see what I mean. You can take that further by using the -c (count) option to grep and then do if ! [ $(grep -c «sysa» /etc/passwd) -eq 0 ] ; then which works but is rather old school.
BUT, you could use the newest shell features (arithmetic evaluation) like
if ! (( $(grep -c "sysa" /etc/passwd) == 0 )) ; then . `
which also gives you the benefit of using the c-lang based comparison operators, ==,,>=,
In this case, per a comment by Orwellophile, the arithmetic evaluation can be pared down even further, like
if ! (( $(grep -c "sysa" /etc/passwd) )) ; then .
if (( ! $(grep -c "sysa" /etc/passwd) )) ; then .
Finally, there is an award called the Useless Use of Cat (UUOC) . 🙂 Some people will jump up and down and cry gothca! I’ll just say that grep can take a file name on its cmd-line, so why invoke extra processes and pipe constructions when you don’t have to? 😉