Technology and General Blog

The not equal “-ne” controller within the Linux Bash programming language compares two doable values when they are not equal. The not equivalent purpose in Ubuntu bash is denoted by the symbol “-ne,” which would be the preliminary character of “not equal.” Also provided is the “!=” operator that is made use of to show the not equal condition. The exclamation stage, i.e., “!=” is also commonly utilised in specific computer languages to reveal that one thing is not equal. In addition, for the not equal expression to operate, it ought to be enclosed by brackets [[…]]. The not equal operation yields a boolean result of Real or Wrong. The not equal expression is normally applied in conjunction only with if or elif expressions to look at for equality and operate guidelines.

Instance 01:

Let us see how not equal sign works in bash. For this purpose, let’s login from the Linux running program very first. In our situation, we have been functioning on Ubuntu 20.04. Right after the successful login, launch the console software named “terminal” at your desktop by “Ctrl+Alt+T”. Alternatively, you can investigate it from the Exercise menu bar and lookup it making use of the look for bar. The terminal application has been introduced correctly we will create a new bash file to conserve the bash code inside it. So, we have produced a bash file named “” with the constructed-in “touch” query as below.

When you are carried out with file development, open this file in any of the editors now built-in or put in in your Linux method. In our circumstance, we have GNU editor configured in our Ubuntu 20.04. For this reason, we have been opening the “” file with the nano query as follows:

You will see that the newly designed bash file will be opened via GNU editor. Now generate the code shown in the image below inside of your bash file. So, we have extra the bash extension initially. After that, we have declared a variable “val” possessing a string worth “Aqsa”. Inside the “if” statement, we have declared a condition. We’ll make a string ingredient $val and assess it to the string “Aqsa”. Through this instance, we’ll see regardless of whether the offered textual content bash variable “val” isn’t identical to the specified string “Aqsa”. If the ailment satisfies and the two the values are not matched, it will operate the to start with echo assertion. Or else, it will execute the other element of a code and conclusion the “if-else” assertion. When evaluating text sorts, the -ne operator could not be cast-off alternatively, the “!=” operator should often be castoff. So, you can see that we have employed “!=” as a substitute of “-new” listed here in the code underneath. Preserve this code with “Ctrl+S” though stop by using the “Ctrl+X” shortcut process.

When the bash file was executed within the terminal, it displayed the other section of the code mainly because the affliction wasn’t satisfied. As for ailments to be accurate, there ought to be not equal string form values. Therefore, we got the output “It’s Equal”.

Open up the bash file when once more with the “nano” question. The only change is to be carried out in the “if-statement” is inside of the “brackets”. We have just modified the string “Aqsa” to “Aqsaa”. Now the variable benefit “Aqsa” and this string “Aqsaa” really do not satisfy just about every other equally. The condition does not fulfill in this article. For that reason, the echo part of the “then” clause should be executed and print “It’s Not Equal” inside the terminal. Let us preserve the bash code at the time much more and give up the Editor.

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

Instance 02:

Let us have a distinct glimpse at code this time. There is a minimal various in this article. We have been making use of the two variables to be when compared this time. We have named these string variables as “fname” and “lname” with different values, e.g., “Aqsa” and “Yasin”. Now, within just the “if” assertion problem portion, we have utilized the two variables to compare through the not equivalent “!=” operator. If the affliction satisfies, it will implement the echo statement of the “then” aspect. Or else, it will run the “echo” portion of the “else” assertion.

Upon functioning the take a look bash doc in the terminal, we have acquired the end result of the first echo assertion “Names are not Equal” as per issue contented.

Let’s see what comes about when we use “-ne” as an alternative of “!=” inside of the bash code while comparing the string form variables. Open the bash file as soon as much more with nano instruction. After the file is opened, replace the “!=” aspect of the “if” assertion situation line with “-ne”. The remaining code will be the exact, and there will be no transform in it.

This time, when we have executed the bash code, it gets us an exception in the terminal expressing “integer expression expected”. This implies that the “-ne” operator ought to only be used for integer styles of variables for comparison. On the other hand, it also shows the mistaken output “Names are Equal”, and it is a large error.

Case in point 03:

This time we will be utilizing the integer type variables to review the “-ne” operator rather of the “!=” operator in the case in point. So, we have initialized “val1” and “val2” integer-variety variables with numerical values. Then we have utilised these variables in the “if” issue clause to have a “-ne” comparison. The remaining code is the same with the minimal changes.

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

Example 04:

We have applied the solitary phrase string sort values or some integer types in all the over circumstances. This time we will be applying a prolonged string or sentence inside of the variable to get when compared. Right after opening the bash file, we have declared two string sort variables, “s1” and “s2” with the exact string values. This time we have assigned the lengthy sentence as worth to equally variables, e.g., “Aqsa Yasin is a Content Writer”. We have as opposed the two variables with the “!=” operator in just the if statement and in the bracket section as both of those variables are string types.

As the affliction goes mistaken, that is why it prints “Strings are Equal”.

Illustration 05:

Within just our final case in point, we have declared two variables owning e-mail as their string values to be compared. In a one glance, you will not be in a position to point out an mistake, but the two values are not the similar. The relaxation of the code does not change. Conserve the code and flip to the terminal.

As the emails are not equivalent, it executed the first echo assertion of then clause expressing “Mails are Not Equal”, when the file has been executed in the shell with the “bash” query.


In this straightforward manual, we have found quite a few situations for the working of not the equal operator. We have elaborated these illustrations for the string and integer kind variables. We hope this short article will be advantageous and simple to do for you.

Leave a Reply

Your email address will not be published. Required fields are marked *