I know in shell(bash), arithmetic operation could be done inside ((..))
,
e.g.
i=1
((i++)) #i=1
((i+=1)) #i=3
My question is:
What exactly did ((..))
do? Does it create an anonymous variable?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityI know in shell(bash), arithmetic operation could be done inside ((..))
,
e.g.
i=1
((i++)) #i=1
((i+=1)) #i=3
My question is:
What exactly did ((..))
do? Does it create an anonymous variable?
Arithmetic Expression Syntax -
(( expression ))
The expression is evaluated according to the rules described under ARITHMETIC EVALUATION. If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to let "expression"
.
Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:
$(( expression ))
The expression is treated as if it were within double quotes, but a double quote inside the parentheses is not treated specially. All tokens in the expression undergo parameter and variable expansion, command substitution, and quote removal. The result is treated as the arithmetic expression to be evaluated. Arithmetic expansions may be nested.
Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, \, and, when history expansion is enabled, !. The characters $ and ` retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the following characters: $, ", \, or . A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash. The backslash preceding the ! is not removed.
It allows the calculation of an mathmatical expression and the substitution of the result
Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:
$((expression))
The expression is treated as if it were within double quotes, but a double quote inside the parentheses is not treated specially. All tokens in the expression undergo parameter expansion, string expansion, command substitution, and quote removal. Arithmetic expansions may be nested.
The evaluation is performed according to the rules listed below under ARITHMETIC EVALUATION. If expression is invalid, bash prints a message indicating failure and no substitution occurs.Bash Manpage
The ((..))
simply tells the shell interpreter you will be doing arithmetic. Normally in bash you would need backticks `...`
to do arithmetic. This is an alternative syntax that is more C like and allows things like i++ to increment the variable i
by 1. There is an answer on stackoverflow that explains it well.
In bash
(not in POSIX shell), ((expression)) allow you to do arithmetic evaluation. It is the same as:
let "expression"
It does not create any anonymous variables, it evaluated expression in current shell, affected all variables in current shell context. If the value of expression is non-zero, return status is 0, otherwise return status is 1.
A note that ((...))
and let
are features from ksh
and only available in bash
, zsh
and ksh
. You should using ((..))
over let
because it more legible and easy to quote than using let
.
For portability, using $((...)) construct.
let
builtin.
– cuonglm
Jan 8 '15 at 9:07
return status
which is get by $?
, not return value.
– user218867
Jan 8 '15 at 9:14
i=1; echo $((i++))
you will get 1 but if you runi=1;((i++)); echo $i
you will get 2 – αғsнιη Jan 8 '15 at 8:41