The not equal “-ne” controller inside the Linux Bash programming language compares two possible values when they’re not equivalent. The not equal function in Ubuntu bash is denoted by the symbol “-ne,” which would be the initial character of “not equal.” Also included is the “!=” operator that is used to indicate the not equal condition. The exclamation point, i.e., “!=” is also commonly used in certain computer languages to indicate that something is not equal. In addition, for the not equal expression to operate, it must be enclosed by brackets [[…]]. The not equal operation yields a boolean result of True or False. The not equal expression is often used in conjunction only with if or elif expressions to check for equality and run instructions.

Example 01:

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/1-28.jpg" data-lazy- height="21" src="data:image/svg xml,” width=”338″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/2-28.jpg" data-lazy- height="21" src="data:image/svg xml,” width=”329″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/3-27.jpg" data-lazy- height="149" src="data:image/svg xml,” width=”463″>

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”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/4-25.jpg" data-lazy- height="35" src="data:image/svg xml,” width=”337″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/5-23.jpg" data-lazy- height="149" src="data:image/svg xml,” width=”456″>

Upon the successful execution of the bash file via bash command, it printed out “It’s Not Equal” as expected.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/6-21.jpg" data-lazy- height="37" src="data:image/svg xml,” width=”335″>

Example 02:

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/7-21.jpg" data-lazy- height="164" src="data:image/svg xml,” width=”475″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/8-16.jpg" data-lazy- height="39" src="data:image/svg xml,” width=”333″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/9-14.jpg" data-lazy- height="162" src="data:image/svg xml,” width=”418″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/10-13.jpg" data-lazy- height="55" src="data:image/svg xml,” width=”482″>

Example 03:

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/11-11.jpg" data-lazy- height="162" src="data:image/svg xml,” width=”423″>

As the val1 and val2 variables have different numerical values, the execution displays that the “Numbers are Not Equal”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/12-9.jpg" data-lazy- height="37" src="data:image/svg xml,” width=”342″>

Example 04:

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/13-8.jpg" data-lazy- height="162" src="data:image/svg xml,” width=”425″>

As the condition goes wrong, that’s why it prints “Strings are Equal”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/14-8.jpg" data-lazy- height="34" src="data:image/svg xml,” width=”337″>

Example 05:

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/15-10.jpg" data-lazy- height="163" src="data:image/svg xml,” width=”418″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/16-7.jpg" data-lazy- height="37" src="data:image/svg xml,” width=”332″>

Conclusion:

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.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Author-Image-150×150.jpg612658fcace2e.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.