2

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?

  • What do you mean "# 2" and "# 3" on end? – αғsнιη Jan 8 '15 at 8:32
  • @KasiyA To make question clear. – user218867 Jan 8 '15 at 8:36
  • Done. But I think you didn't need them. because if you run i=1; echo $((i++)) you will get 1 but if you run i=1;((i++)); echo $i you will get 2 – αғsнιη Jan 8 '15 at 8:41
0

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.

Reference

0

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

0

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.

0

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.

  • About the return value, it seems to return the result of expression.. – user218867 Jan 8 '15 at 9:06
  • @EricWang: No, read the link I gave in my answer or read the documentation of let builtin. – cuonglm Jan 8 '15 at 9:07
  • The doc in your link means return status which is get by $?, not return value. – user218867 Jan 8 '15 at 9:14
  • @EricWang: I mean return status, updated answer. – cuonglm Jan 8 '15 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.