The “!“ (NOT) operator in Bash scripting is a logical operator used to invert the exit status of a command. It’s valuable for conditionals and error checking. In this article, I will demonstrate 3 different examples of using the NOT operator in bash scripting. So let’s start.
- Understanding the role of the NOT operator in bash script.
- Checking the existence of the variable value using the NOT operator in a bash script.
NOT Operator in Shell Script
In shell scripting, the NOT operator is represented by the exclamation mark (!). It is used to negate a condition or logical expression. When you place the NOT operator before a condition, it flips the condition’s truth value. For example, ! true becomes false, and ! false becomes true. The NOT operator is commonly used in conditional statements to reverse the logic and perform actions when a condition is not met. It’s a fundamental tool for making decisions and creating complex logic in shell scripts.
Syntax of the NOT Operator in Bash Script
In Bash, the basic syntax of NOT Operator is as follows:
if [ ! condition ]; then # Code to execute if the condition is not true fi
if [[ ! condition ]]; then # Code to execute if the condition is not true fi
if ! test condition; then command1 command2 fi
In each syntax, the exclamatory sign (!) flips the output value of the conditions such that it returns true if the condition gives false and false if the conditional exit status is true.
4 Practical Examples of Using the NOT Operator in Bash Scripts
Given below, there are 4 practical examples that I have articulated for your perusal. The example will articulate the effective way of string and integer comparison. Further, you will learn how to check whether a variable is empty or not using the NOT operator. Lastly, you will learn the way to negate the IF conditional. Don’t forget to run those codes in your command line.
Example 01: Using NOT Operator for String Comparison in Bash
In my first code, the objective is to allow a user to input two strings and then compare them to determine whether they are equal or not. Check and follow the steps given below and replicate the same in your terminal.
- At first, launch an Ubuntu Terminal.
- Write the following command to open a file in Nano:
- Copy the script mentioned below:
#!/bin/bash #taking the input value from the user read -p "Enter the first string: " str1 read -p "Enter the second string: " str2 #checking whether the given strings are equal or not if [ "$str1" != "$str2" ]; then echo "Strings are not equal." else echo "Strings are equal." fi
- Press CTRL+O and ENTER to save the file; CTRL+X to exit.
- Use the following command to make the file executable:
chmod u+x string_comparison.sh
- Run the script by the following command:
Upon giving Hello and World strings to the command line, the bash script declares “Strings are not equal” to the command line. However, when I put LinuxSimply twice as the variable value, the code returns “Strings are equal”, indicating the string match based on the provided string.
Example 02: Inserting NOT Operator for Integer Comparison in Bash
Following the previous code, I want to enable users to input two numerical values and compare them to determine if they are equal or not. To accomplish the task, I will use the NOT operator in a bash script. Follow the script given below.
Script (integer_comparison.sh) >
#!/bin/bash read -p "Enter the first number: " number1 read -p "Enter the second number: " number2 if [ ! "$number1" -eq "$number2" ]; then echo "Numbers are not equal." else echo "Numbers are equal." fi
Use the following command to run the code
As you can understand from the image given above, the code can successfully identify the equality of two given integers and return the corresponding response to the command line.
Example 03: Checking the Variable Whether Empty Using NOT Operator in Bash
In my last example, the objective of this code is to allow a user to input a value and then determine whether the provided value is empty or not. Similar to before, I will incorporate a NOT operator to accomplish the task.
Script (variable_check.sh) >
#!/bin/bash read -p "Enter a value: " userVar if [ ! -z "$userVar" ]; then echo "The variable is not empty." else echo "The variable is empty." fi
Run the code by using the following command
As the image depicts above, the bash code gives a prompt to the user and asks to enter a value. In the first case, I did not put a null value to the assigned variable and subsequently, the code returns “The variable is empty” to the command line. However in the second case, Upon giving the “Random Value” as a string to the variable, the script says “The variable is not empty”.
Example 04: Negate the IF condition in a Bash Script Using the ! (NOT) Operator
From the script given below, you will learn how to negate if condition using the ! NOT operator in a bash script with two different approaches. One is incorporating! sign inside the double square bracket and the other is keeping the sign outside of the double square bracket.
When the NOT operator resides outside the double square brackets [[, it will run the expression(s) enclosed within the brackets and invert the outcome. That means, If you have multiple expressions, then the entire result will be negated. On the other hand, If you put NOT Operator inside the double square, then the operator will be applied only to the individual expression.
Script (negate_if.sh) >
#!/bin/bash # Assign a value to the 'num' variable read -p "Give me the first number to be evaluated: " num1 # Check if 'num1' is not equal to 0 if ! [[ $num1 -eq 0 ]] then # If the condition is true, print a message echo "Value of first number is not 0" fi read -p "Insert the second number: " num2 # Check if 'num2' is not equal to 0 if [[ ! $num2 -eq 0 ]] then # If the condition is true, print a message echo "Value of the second number is not 0 either" fi
Run the code by using the following command
As you can see from the image above, both methods return the same output successfully.
In Bash scripting, the NOT operator effectively reverses the truth value of a condition or expression. By placing the NOT operator before a statement, one can transform true into false and false into true. In this article, I have demonstrated an overview of the NOT operator in bash. However, if you have any questions or queries related to this article, feel free to comment below. Thank you.