In Bash and Dash, using quotes in an Arithmetic Expansion is illegal:
$ bash -c 'x=123;echo $(("$x"))'
bash: "123": syntax error: operand expected (error token is ""123"")
$ dash -c 'x=123;echo $(("$x"))'
dash: 1: arithmetic expression: expecting primary: ""123""
Bash gives the same error when invoked as sh. Ksh and FreeBSD's Bourne Shell don't mind it, though:
$ ksh -c 'x=123;echo $(("$x"))'
123
$ sh -c 'x=123;echo $(("$x"))'
123
According to the Bash Reference Manual:
The expression is treated as if it were within double quotes, but a double quote inside the parentheses is not treated specially. All tokens … undergo … quote removal.
(which is essentially the same as POSIX says.)
Finally, there's a distinction here in how Bash handles $(( ))
compared to other arithmetic contexts like (( ))
(as in conditional expressions, for example). The latter is fine with quotes.
Either I don't understand what quote removal means here, or this is a bug in some of these shell implementations. If it's the former, what does "quote removal" actually mean? Or, is it just a bug?
x='"123"';echo $(($x))
? That gives an error, too. – kojiro Jan 14 '16 at 01:51