Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“哪一个更好?”==&引用;或-eq";用于比较bash shell中的整数?_Bash_Shell - Fatal编程技术网

“哪一个更好?”==&引用;或-eq";用于比较bash shell中的整数?

“哪一个更好?”==&引用;或-eq";用于比较bash shell中的整数?,bash,shell,Bash,Shell,我可以看到一些文章,比如提到用“-eq”来比较整数,但这并不是说我们不能用“==”来比较整数 我在bashshell本地验证了这一点,“==”工作正常。 所以,有谁能让我帮助了解使用哪个选项更好,如果使用“-eq”,那么为什么呢?要比较整数,请使用-eq。区别在于==比较字符串值,而-eq比较数值。下面是一个例子,它们产生不同的结果: $ [ 03 = 3 ] ; echo $? 1 $ [ 03 -eq 3 ] ; echo $? 0 使用[[]也一样: $ [[ 03 == 3 ]] ;

我可以看到一些文章,比如提到用“-eq”来比较整数,但这并不是说我们不能用“==”来比较整数

我在bashshell本地验证了这一点,“==”工作正常。
所以,有谁能让我帮助了解使用哪个选项更好,如果使用“-eq”,那么为什么呢?

要比较整数,请使用
-eq
。区别在于
==
比较字符串值,而
-eq
比较数值。下面是一个例子,它们产生不同的结果:

$ [ 03 = 3 ] ; echo $?
1
$ [ 03 -eq 3 ] ; echo $?
0
使用
[[
]也一样:

$ [[ 03 == 3 ]] ; echo $?
1
$ [[ 03 -eq 3 ]] ; echo $?
0
作为一个数字,
03
等于
3
。但是作为一个字符串,
03
3
是不同的


摘要:要比较数值是否相等,请使用
-eq

要比较整数,请使用
-eq
。区别在于
==
比较字符串值,而
-eq
比较数值。以下是它们产生不同结果的示例:

$ [ 03 = 3 ] ; echo $?
1
$ [ 03 -eq 3 ] ; echo $?
0
使用
[[
]也一样:

$ [[ 03 == 3 ]] ; echo $?
1
$ [[ 03 -eq 3 ]] ; echo $?
0
作为一个数字,
03
等于
3
。但是作为一个字符串,
03
3
是不同的


摘要:要比较数值是否相等,请使用
-eq

这取决于上下文。在数学上下文中(如果您专门为bash编写脚本,则最好使用
=

(( foo == 3 ))     ## foo = 3 would be an assignment here, and foo -eq 3 an error
数学上下文也存在于其他情况中——例如,索引到非关联数组,使
=
成为首选,但在下面的示例中
-eq
非法:

foo=( yes no )
bar=3
echo "${foo[bar == 3 ? 0 : 1]}" # echoes "yes"

[[]]
中,使用
-eq

[[ $foo -eq 3 ]]   ## $foo = 3 would be a string comparison here; $foo == 3 is a synonym,
                   ## but bad finger-memory to have if one wants to write POSIX code
                   ## elsewhere.
[]
中,使用
-eq
--并引用:

[ "$foo" -eq 3 ]   ## = would be a valid string comparison here; == would be a
                   ## POSIX-incompatible string comparison here; -eq is correct.

这取决于上下文。在数学上下文中(如果您专门为bash编写脚本,则最好使用
=

(( foo == 3 ))     ## foo = 3 would be an assignment here, and foo -eq 3 an error
数学上下文也存在于其他情况中——例如,索引到非关联数组,使
=
成为首选,但在下面的示例中
-eq
非法:

foo=( yes no )
bar=3
echo "${foo[bar == 3 ? 0 : 1]}" # echoes "yes"

[[]]
中,使用
-eq

[[ $foo -eq 3 ]]   ## $foo = 3 would be a string comparison here; $foo == 3 is a synonym,
                   ## but bad finger-memory to have if one wants to write POSIX code
                   ## elsewhere.
[]
中,使用
-eq
--并引用:

[ "$foo" -eq 3 ]   ## = would be a valid string comparison here; == would be a
                   ## POSIX-incompatible string comparison here; -eq is correct.

即使对于字符串,
=
也不是
[]
内部的最佳实践;POSIX sh标准只指定了
=
。Bash支持
=
,但这是一个扩展,使用它的脚本可能与所有其他POSIX shell不兼容。@CharlesDuffy也是一个“bashism”即使对于字符串,
=
也不是
[]
内部的最佳实践;POSIX sh标准只指定了
=
。Bash支持
=
,但这是一个扩展,使用它的脚本可能与所有其他POSIX shell不兼容。@CharlesDuffy也是一个“bashism”?顺便说一句——我发现有必要添加我自己的答案,原因只有几个:没有提到数学上下文(在这种情况下,
==
是正确的),并且暗示
==
[[[]]
中的最佳实践,即使对于字符串也是如此(而使用
[]
时会导致POSIX不符合习惯).BTW——我发现有必要添加我自己的答案,原因只有几个:没有提到数学上下文(在这种情况下,
==
是正确的),并且暗示
==
[[]]
中的最佳实践,即使对于字符串也是如此(而使用
[]
时会导致POSIX不符合习惯)。