您的位置:首页 > 运维架构 > Shell

Bash's ArithmeticExpression

2013-12-08 20:10 211 查看
Bash's ArithmeticExpression

let command: 

let a=17+23
echo "a = $a"      # Prints a = 40
let a=17 + 23      # WRONG
let a="17 + 23"    # Right
let a=28/6
echo "a = $a"      # Prints a = 4


In addition to the let command, one may use the (( )) syntax to enforce an arithmetic context. If there is a $ (dollar sign) before the parentheses, then a substitution is performed (more on this below). White space is allowed inside (( )) with much greater leniency than with normal assignments, and variables inside (( )) don't require $(because string literals aren't allowed):



(( )) without the leading $ is a Bash-only feature. $(( )) substitution is allowed in the POSIX shell. As one would expect, the result of the arithmetic expression inside the$(( )) is substituted into the original command. Here are some examples of the use of the arithmetic substitution syntax:



use declare command to set the type of a variant
declare -i b        # Declare b as an integer


参考:http://bash.cumulonim.biz/ArithmeticExpression.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: