bash “-ne”在bash中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17689511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "-ne" mean in bash?
提问by ZenBalance
What does the command "-ne" mean in a bash script?
命令“-ne”在 bash 脚本中是什么意思?
For instance, what does the following line from a bash script do?
例如,bash 脚本中的以下行有什么作用?
[ $RESULT -ne 0 ]
回答by Keith Thompson
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.
[
实际上是一个命令,而不是您所期望的 bash shell 语法的一部分。它恰好是 Bash 内置命令,因此在 Bash 手册中有记录。
There's also an external command that does the same thing; on many systems, it's provided by the GNU Coreutils package.
还有一个执行相同操作的外部命令;在许多系统上,它由 GNU Coreutils 包提供。
[
is equivalent to the test
command, except that [
requires ]
as its last argument, and test
does not.
[
等价于test
命令,除了[
需要]
作为它的最后一个参数,而test
不是。
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).
假设您的系统上安装了 bash 文档,如果您键入info bash
并搜索'test'
or '['
(撇号是搜索的一部分),您将找到该[
命令的文档,也称为test
命令。如果使用man bash
代替info bash
,则搜索^ *test
(行首的单词test
,后跟一定数量的空格)。
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 stringinequality operator.
跟随对“Bash 条件表达式”的引用将引导您了解-ne
,它是数字不等式运算符(“ne”代表“不等于”)。相比之下,!=
它是字符串不等式运算符。
You can also find bash documentation on the web.
您还可以在网上找到 bash 文档。
- Bash reference
- Bourne shell builtins(including
test
and[
) - Bash Conditional Expressions-- (Scroll to the bottom;
-ne
is under "arg1OP arg2") - POSIX documentation for
test
- Bash 参考
- Bourne shell 内置函数(包括
test
和[
) - Bash 条件表达式——(滚动到底部;
-ne
在“ arg1OP arg2”下) - POSIX 文档
test
The officialdefinition of the test
command is the POSIX standard (to which the bash implementation should conform reasonably well, perhaps with some extensions).
该命令的官方定义test
是 POSIX 标准(bash 实现应该很好地符合该标准,也许还有一些扩展)。
回答by Omegaman
"not equal"
So in this case, $RESULT
is tested to not be equal to zero.
“不等于”所以在这种情况下,$RESULT
被测试为不等于零。
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.