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.
精彩评论