What does "-ne" mean in bash?
Solution 1:
This is one of those things that can be difficult to search for if you don't already know where to look.
[
is actually a command, not part of the bash shell syntax as you might expect. It happens to be a Bash built-in command, so it's documented in the Bash manual.
There's also an external command that does the same thing; on many systems, it's provided by the GNU Coreutils package.
[
is equivalent to the test
command, except that [
requires ]
as its last argument, and test
does not.
Assuming the bash documentation is installed on your system, if you type info bash
and search for 'test'
or '['
(the apostrophes are part of the search), you'll find the documentation for the [
command, also known as the test
command. If you use man bash
instead of info bash
, search for ^ *test
(the word test
at the beginning of a line, following some number of spaces).
Following the reference to "Bash Conditional Expressions" will lead you to the description of -ne
, which is the numeric inequality operator ("ne" stands for "not equal). By contrast, !=
is the string inequality operator.
You can also find bash documentation on the web.
- Bash reference
-
Bourne shell builtins (including
test
and[
) -
Bash Conditional Expressions -- (Scroll to the bottom;
-ne
is under "arg1 OP arg2") - POSIX documentation for
test
The official definition of the test
command is the POSIX standard (to which the bash implementation should conform reasonably well, perhaps with some extensions).
Solution 2:
"not equal"
So in this case, $RESULT
is tested to not be equal to zero.
However, the test is done numerically, not alphabetically:
n1 -ne n2 True if the integers n1 and n2 are not algebraically equal.
compared to:
s1 != s2 True if the strings s1 and s2 are not identical.