What does the command "-ne" mean in a bash script?
For instance, what does the following line from a bash script do?
[ $RESULT -ne 0 ]
Join Stack Overflow to learn, share knowledge, and build your career.
What does the command "-ne" mean in a bash script?
For instance, what does the following line from a bash script do?
[ $RESULT -ne 0 ]
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.
test
and [
)-ne
is under "arg1 OP arg2")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).
test
is, after all, a POSIX command.
– Charles Duffy
Jul 17 '13 at 1:32
"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.
$RESULT
is "not equal" to0
– lurker Jul 17 '13 at 1:16!=
is string inequality. – Keith Thompson Jul 17 '13 at 1:17[
runs a command calledtest
.-ne
is an argument to thetest
command, not to bash, and you can find its documentation inman test
. – Charles Duffy Jul 17 '13 at 1:18[ "$RESULT" -ne 0 ]
at the minimum -- or, much better,(( RESULT != 0 ))
. (Better than that -- if you're getting this from$?
, you could just branch on the exit status of the command you're running directly, instead of running it, capturing its exit status into a variable, and then substituting that variable's value into atest
command). – Charles Duffy Jul 17 '13 at 1:20