Bash's ArithmeticExpression

Bash's ArithmeticExpression

 let command: 

1 let a=17+23
2 echo "a = $a"      # Prints a = 40
3 let a=17 + 23      # WRONG
4 let a="17 + 23"    # Right
5 let a=28/6
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

原文地址:https://www.cnblogs.com/tekkaman/p/3464199.html