开发者

What is the purpose of `$[`in shell scripts?

Looking for an easy way to make a random number, i came across a web page that used the follow snip of code

  echo $[ ( $RANDOM % $DIE_SIDES )  + 1 ]

What is the purp开发者_Python百科ose of the $[. Googling did not reveal the answer I seek. Thanks


The $[expression] construct is used for arithmetic; $[1+1], for example, returns 2. You can also say $((expression)) or expr 1 + 1. The expr command version is old-school and should work in any shell, the $[expression] and $((expression)) versions work in bash but I'm not sure if they're covered by POSIX.

Update: The $[expression] form is a bash extension, the $((expression)) form is specified for the POSIX shell.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜