On Thu, Apr 18, 2013 at 11:59:40AM +0200, and...@coolbox.se wrote: > The ARITHMETIC EVALUATION section of the man page claims equivalence with C > for all the operators, but in reality bash does not perform short circuit > evaluation, which implies that the logical operators do NOT produce the same > results as in C.
You missed this part, under EXPANSION: Expansion is performed on the command line after it has been split into words. There are seven kinds of expansion performed: brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, word splitting, and pathname expansion. The order of expansions is: brace expansion, tilde expansion, parameter, variable and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and pathname expansion. It's not clear from this part of the documentation how bash handles a command substitution nested inside an arithmetic expansion, but you've experimentally demonstrated that the nested substitution is performed first. Once the nested substitution has been done, short circuit evaluation DOES take effect. Here's a demonstration of short circuit without the nested substitution causing confusion: imadev:~$ echo $((1/0)) bash: 1/0: division by 0 (error token is "0") imadev:~$ echo $((0 && 1/0)) 0