In Linux, the bash script supports five major operator categories that are arithmetic, boolean, bitwise, relational, and file test. Each category provides a large list of operators that perform special tasks based on their names like relational operators support the “not equal” operator. It is the most commonly used to compare two operands, either a string or numeric value.
This guide explains the not-equal operator’s working, usage, and functionality for string comparison.
- Compare Strings Using “!=” Operator
- Compare Strings Using “-ne” Operator
- Compare Specific String Using “!=” Operator
How Does the “not equal” Operator Work?
The “not equal” operator is represented by “-ne,” which is the first keyword of the “not equal” operator. It can also be expressed symbolically as “!=”.To perform the comparison operations, it must be surrounded by the double square brackets, i.e., “[[…]]”.
It is mainly used to compare strings and numeric values in looping statements. It returns the boolean result whether the condition is true or false.
Let’s understand how it works for string comparison with the help of the following examples.
Example 1: Compare Strings Using “!=” Operator
Create or open a bash script in the “nano” text editor that uses the “!=” operator for the comparison of two strings:
$ nano loop.sh
#!/bin/bash a="anna" j="johnson" if [ "$a" != "$j" ]; then echo "Both strings are NOT Equal." else echo "Both strings are Equal." fi
The description of the above block is written here:
- The “#! /bin/bash” represents the “Bash Shebang” for the execution of the script in “bash” shell.
- The two variables “a” and “j” are declared to have string values, i.e., “a=anna” and “j=johnson.”
- The “if-then-else-fi” loop sets the condition that if the string of variable “$a” is not equal to “$b,” then the first echo statement will print its output.
- If the condition becomes false, the “else” statement will execute.
Press “Ctrl+S” to save and “Ctrl+X” to exit the editor.
Execute the script and get the output:
The output shows that the condition added in the if loop as “$a” is not equal to “$j” and has been evaluated as true.
Example 2: Compare Strings Using “-ne” Operator
If the user wants to compare strings using the “-ne” operator, then enclose the condition in double square brackets like [[ “$a” -ne “$j” ]]. This is because the single square brackets having “-ne” operators only support the integer value.
Create/Open a Script
Let’s create/open another bash script to compare the strings using the “-ne” operator:
$ nano sample.sh
#!/bin/bash std1="milton" std2="maddox" if [[ "$std1" -ne "std2" ]]; then echo "strings are NOT Equal." else echo "strings are equal" fi
The above lines of code are described below:
- The “std1” and “std2” are two variables that contain strings “milton” and “maddox”.
- The “ “if-then-else-fi” loop defines a condition that if the string “$std1” is not equal to “$std2”, then the “echo” statement will display the statement “strings are NOT Equal.”
- The “else” echo statement will execute if the condition becomes false:
Run the “sample.sh” script for the output:
The output shows that defined strings are not equal.
Example 3: Compare Specific String Using “!=” Operator
Apart from two strings, the particular string can also be compared using the “not equal” operator. Let’s see how this can be performed.
Create/Open a Script
A “new.sh” existing script has these lines of code:
#!/bin/bash name=herry if [ "$name" != "herry" ];then echo "string is not equal"elseecho "string is equal" fi
The description of “new.sh” script code is here:
- The “name” variable is declared as having a string “herry”.
- The “if” loop uses the “!=” operator for comparing the “name” variable string.
- The “echo” statement will execute if the loop condition becomes true.
- The “else” part contains another “echo” statement that will be printed if the condition becomes false.
The output of the “new.sh” script is as follows:
As the “$name” variable value is equal to the string “herry” and the second echo statement of the “if” loop has been executed.
The “not equal” operator is the relational operator that is used to compare the string or numeric values. It is generally utilized in the if “loop” for comparing two strings or either the specified string. Its usage is quite straightforward.
This guide has illustrated the working and usage of the “not equal” operator for string comparison.