Let’s see how not equal sign works in bash. For this purpose, let’s login from the Linux operating system first. In our case, we have been working on Ubuntu 20.04. After the successful login, launch the console application named “terminal” at your desktop by “Ctrl+Alt+T”. Alternatively, you can explore it from the Activity menu bar and search it using the search bar. The terminal app has been launched successfully; we will create a new bash file to save the bash code within it. So, we have made a bash file named “test.sh” with the built-in “touch” query as below.
When you are done with file creation, open this file in any of the editors already built-in or installed in your Linux system. In our case, we have GNU editor configured in our Ubuntu 20.04. Hence, we have been opening the “test.sh” file with the nano query as follows:
You will see that the newly created bash file will be opened via GNU editor. Now write the code shown in the image below within your bash file. So, we have added the bash extension first. After that, we have declared a variable “val” having a string value “Aqsa”. Within the “if” statement, we have declared a condition. We’ll make a string element $val and compare it to the string “Aqsa”. Throughout this instance, we’ll see whether the provided text bash variable “val” isn’t identical to the specified string “Aqsa”. If the condition satisfies and both the values are not matched, it will run the first echo statement. Otherwise, it will execute the other part of a code and end the “if-else” statement. When comparing text types, the -ne operator couldn’t be cast-off; alternatively, the “!=” operator must always be castoff. So, you can see that we have used “!=” instead of “-new” here in the code below. Save this code with “Ctrl+S” while quit via the “Ctrl+X” shortcut method.
When the bash file was executed within the terminal, it displayed the other part of the code because the condition wasn’t satisfied. As for conditions to be true, there must be not equal string type values. Therefore, we got the output “It’s Equal”.
Open the bash file once again with the “nano” query. The only change is to be done in the “if-statement” is within the “brackets”. We have just changed the string “Aqsa” to “Aqsaa”. Now the variable value “Aqsa” and this string “Aqsaa” don’t meet each other equally. The condition doesn’t meet here. Therefore, the echo part of the “then” clause must be executed and print “It’s Not Equal” within the terminal. Let’s save the bash code once more and quit the Editor.
Upon the successful execution of the bash file via bash command, it printed out “It’s Not Equal” as expected.
Let’s have a different look at code this time. There is a little different here. We have been using the two variables to be compared this time. We have named these string variables as “fname” and “lname” with different values, e.g., “Aqsa” and “Yasin”. Now, within the “if” statement condition part, we have used both variables to compare via the not equal “!=” operator. If the condition satisfies, it will implement the echo statement of the “then” part. Or else, it will run the “echo” part of the “else” statement.
Upon running the test.sh bash document in the terminal, we have got the result of the first echo statement “Names are not Equal” as per condition satisfied.
Let’s see what happens when we use “-ne” instead of “!=” within the bash code while comparing the string type variables. Open the test.sh bash file once more with nano instruction. After the file is opened, replace the “!=” part of the “if” statement condition line with “-ne”. The remaining code will be the same, and there will be no change in it.
This time, when we have executed the bash code, it gets us an exception in the terminal saying “integer expression expected”. This means that the “-ne” operator must only be used for integer types of variables for comparison. On the other hand, it also shows the wrong output “Names are Equal”, and it’s a big error.
This time we will be using the integer type variables to compare the “-ne” operator instead of the “!=” operator in the example. So, we have initialized “val1” and “val2” integer-type variables with numerical values. Then we have used these variables in the “if” condition clause to have a “-ne” comparison. The remaining code is the same with the minor changes.
As the val1 and val2 variables have different numerical values, the execution displays that the “Numbers are Not Equal”.
We have used the single word string type values or some integer types in all the above instances. This time we will be using a long string or sentence within the variable to get compared. After opening the bash file, we have declared two string type variables, “s1” and “s2” with the same string values. This time we have assigned the long sentence as value to both variables, e.g., “Aqsa Yasin is a Content Writer”. We have compared both variables with the “!=” operator within the if statement and in the bracket section as both variables are string types.
As the condition goes wrong, that’s why it prints “Strings are Equal”.
Within our last example, we have declared two variables having emails as their string values to be compared. In a single glance, you will not be able to indicate an error, but both values are not the same. The rest of the code doesn’t change. Save the code and turn towards the terminal.
As the emails are not equal, it executed the first echo statement of then clause saying “Mails are Not Equal”, once the file has been executed in the shell with the “bash” query.
In this simple guide, we have seen many instances for the working of not the equal operator. We have elaborated these examples for the string and integer type variables. We hope this article will be beneficial and easy to do for you.