For those of you wondering how to use the “not equal to” operator in Bash, here’s a detailed guide to know everything about it.
In Bash programming, the “not equal to” operator helps compare values and determine whether they are equal. The “not equal to” operator is denoted by “!=” or “-ne” in Bash.
With this article on how to use the “not equal to” operator in Bash, we look into several methods of comparing the string and numeric values. We have also provided scripts and their outputs for each method for easier understanding and implementation.
What is the Need for the “Not Equal To” Operator in Bash?
The “not equal to” operator executes particular code blocks based on the inequality of two values within the conditional statements, such as if statements or loops. Additionally, we can determine whether variables, numbers, or strings are different by using the not equal to operator.
With the help of this operator, we have a strong tool for making choices and managing the progress of our Bash scripts. Knowing how to use the “not equal to” operator in Bash enables us to create more versatile and resilient scripts to handle various inequality-based circumstances.
Methods of How to Use the “Not Equal To” Operator in Bash
The “not equal to” operator can be applied in Bash using “!=” or “-ne” and used in different scenarios.
For instance, the “!=” operator checks the inequality between strings and numbers. However, the “-ne” operator can only check the inequality between numbers and not strings.
Using the “!=” Operator
To check the inequality between two numeric values or two string values, the “!=” operator can be used. Here are the Bash files for both, along with their outputs:
Checking the Inequality Between Numbers
Create a Bash file with the following script that uses the “!=” operator to determine whether a number entered is equal to 10 or not.
The output shown below shows two script executions. In the first execution, (20) is used as the input, and “The number is ‘not equal to’ 18” is printed. In the second execution, (18) is used as the input. Hence, the output “The number is equal to 18” is printed.
Checking the Inequality Between Strings
Make a Bash file with the following script, which uses the “!=” operator to determine whether or not two string values are equal.
When the user types in different string values in the first scenario (such as “Hello” and “World”), the script will output “The strings are not equal”.
When the user provides the same string value (for example, “OpenAI”) for both variables in the second case, the script will output “The strings are equal”.
The script compares the values of the str1 and str2 variables using the “not equal to” operator (!=). Then it decides what to do next based on the comparison outcome.
Using the “-ne” Operator
The “-ne” operator works similarly to the “!=” operator and can be used to check the inequality between two numeric values. Unlike the “!=” operator, it cannot be used to compare two string values. Here are the Bash files for both, along with their outputs:
Checking the Inequality Between Numbers
Create a Bash file with the following script that uses the “-ne” operator to determine whether the numbers provided are equal.
In the above script, numbers 10 and 20 are assigned respectively to num1 and num2. The “if” statement compares num1 and num2 using the “-ne” operator and generates the output.
The statement “The numbers are not equal” will be displayed if the condition is true (the numbers are not equal). If the condition is false, it will output “The numbers are equal”.
As the numbers we provided are unequal, the script generates the condition “true” and the output “The numbers are not equal”.
Checking the Inequality Between Strings
Make a Bash file that accepts the username as an input using the script below. Next, the “-ne” operator determines whether the input value is equal to “admin”.
The “-ne” operator cannot compare two string values, and thus generates similar output for both cases.
In the above script, the script prompts the user to enter a username, and the [tr -d ‘\n’] command is used to remove the newline character from the input value.
The comparison uses the “-ne” operator, and the statement “Valid User” will be displayed if the condition is false.
As “-ne” operator can only check inequality between numbers and not strings, it cannot compare inequality and generates a “Valid User” output for both cases.
Practical Examples and Use Cases of “Not Equal To” Operator in Bash
Here are some practical examples of the “not equal to” operator in various scenarios:
- File Existence Check: The script below considers the specified filename and determines whether the file exists using the “not equal to” operator.
- String Comparison: The below script compares the input variable’s value with the word “yes” and then produces the output using the “not equal to” operator.
- Loop Control: The below script uses for loop and iterates until the value of the ‘i’ variable is “not equal to” 10.
- Array Element Check: The below script considers the string “value” and the first element of the array are being compared using the “not equal to” operator.
Through this article on how to use the “not equal to” operator in Bash, you can use the “-ne” and “!=” operators to make decisions and manage the flow of your scripts based on inequality conditions in if statements and loops.
To get the best out of the “-ne” and “!=” operators, it is advised to follow proper quoting, consistent data types, clear variable naming, and code comments.
You can use the “not equal to” operator to improve the functionality and logic of your Bash scripts through real-world examples like file existence checks, text comparisons, loop control, and array element checks.
If this guide helped you, please share it.