开发者

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

开发者 https://www.devze.com 2023-02-21 05:16 出处:网络
Looking for an easy way to make a random number, i came across a web page that used the follow snip of code

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

精彩评论

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

关注公众号