Bash's ArithmeticExpression

时间:2022-12-07 13:35:23

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):

Bash's ArithmeticExpression

(( )) 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:

Bash's ArithmeticExpression

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

参考:http://bash.cumulonim.biz/ArithmeticExpression.html