这些 bash 语句都是等价的吗?
我刚刚发现了这个很棒的网站。当我阅读 bash 标记的帖子时,我想到了以下问题:
此代码:
var=$RANDOM
var1=$[ $var % 13 ]
echo "var1 = $var1"
var2=$( $var % 13 )
echo "var2 = $var2"
var3=$(( $var % 13 ))
echo "var3 = $var3"
var4=`expr $var % 13` # Note revised from original post by adding the expr
echo "var4 = $var4"
产生此输出:
var1 = 7
./question: line 7: 23225: command not found
var2 =
var3 = 7
var4 = 7
所以只有 var2 语句不起作用。我的问题是:使用其他三个中的哪一个只是个人选择的问题,还是还有其他需要考虑的因素?
(在看到回复后,我对这个问题进行了广泛的编辑。希望我以正确的方式做到这一点。)
I just discovered this great site. I was reading bash tagged posts when the following question entered my mind:
This code:
var=$RANDOM
var1=$[ $var % 13 ]
echo "var1 = $var1"
var2=$( $var % 13 )
echo "var2 = $var2"
var3=$(( $var % 13 ))
echo "var3 = $var3"
var4=`expr $var % 13` # Note revised from original post by adding the expr
echo "var4 = $var4"
Produces this output:
var1 = 7
./question: line 7: 23225: command not found
var2 =
var3 = 7
var4 = 7
So only the var2 statement does not work. My question is: is it only a matter of personal choice as to which of the other three should be used or are there other considerations that should be taken into account?
(I've edited this question extensively after seeing replies. Hope I'm doing this the right way.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
$[]
和$(())
版本本质上是可以互换的,只是并非所有 shell 都支持$[]
,因此不要使用它——使用$(())
代替。另外,在这两种形式中,表达式中使用的变量都会自动扩展,因此您不需要在其中使用$
:expr
实际上是一个命令,进行表达式求值——它具有与内置表达式求值大部分相同的功能,但没有一些细节。例如,您必须使用$
来扩展变量,并且必须转义 shell 将视为特殊字符的所有运算符(例如<
、>、
|
等):$()
完全不同——它不进行算术评估,而是将其内容作为命令运行。事实上,它几乎与反引号相同(除了它更容易阅读,并且语法更清晰,所以我总是使用它而不是反引号):为了让这个更复杂,让我添加更多选项
:你应该用什么?我会选择 var3=$(( var % 13 )) 以获得最大的兼容性和(恕我直言)干净的语法。
The
$[]
and$(())
versions are essentially interchangeable, except that not all shells support$[]
, so don't use it -- use$(())
instead. Also, in both of these forms, variables used in the expression will be automatically expanded, so you don't need to use$
inside them:expr
is actually a command which does expression evaluation -- it has most of the same capabilities as the builtin expression evaluation, but without some of the niceties. For instance, you must use$
to expand variables, and must escape any operators that the shell would treat as special characters (e.g.<
,>
,|
, etc):$()
is totally different -- it doesn't do arithmetic evaluation, it runs its contents as a command. In fact, it's almost the same as backquotes (except that it is easier to read, and has much cleaner syntax, so I always use it instead of backquotes):And just to make this more complicated, let me add some more options:
So what should you use? I'd go with
var3=$(( var % 13 ))
for maximum compatibility and (IMHO) clean syntax.$[…]
和$((…))
完全相同。但是,$[…]
形式是已弃用的 bash 和 zsh 扩展;$((…))
形式是 标准。expr
形式大致相同,但它调用外部程序expr
。在过去,shell 没有很多内置功能,特别是没有算术,因此人们会使用expr
进行算术。如今,expr
已经没有太多用处了。$( $var % 13 )
刚刚被破坏,它尝试调用$var
(或更准确地说,它的第一个单词)作为命令名称。The
$[…]
and$((…))
are exactly equivalent. However, the$[…]
form is a deprecated bash and zsh extension; the$((…))
form is standard.The
expr
form is roughly equivalent, but it calls the external programexpr
. In the old days, shells didn't have many built-in capabilities, in particular no arithmetic, and so one would useexpr
for arithmetic. Nowadays there's not much use forexpr
.$( $var % 13 )
is just broken, it tries to call$var
(or more precisely, the first word of it) as a command name.无效命令
有效命令,但更喜欢
$(( var % 13 ))
无效命令
结果: 因此,只有
$(( var % 13 ))
是获取$var
以 13 为模的有效方法。更新
编辑后我会说使用
var1
或var3
计算模数的方法。expr
是一个外部程序,最好使用 bash 的内部实用程序来完成相同的工作。Invalid command
Valid command but prefer
$(( var % 13 ))
Invalid command
Outcome: So out of all only
$(( var % 13 ))
is the valid way to get the modulo of$var
by 13.Update
After your edits I would say use either
var1
orvar3
way of getting modulo calculated.expr
is an external program and its better to use bash's internal utility to get the same job done.另一种方式是 var=((var%13)) 的快捷方式
is another way, a shortcut for var=((var%13))