Skip to main content
2 of 2
Parameter expansion inside arithmetic is separate from earlier parameter expansion, but still happens first.
Stephen Kitt
  • 481.4k
  • 60
  • 1.2k
  • 1.4k

Shell parameter expansion happens before the expression is evaluated, including comma handling:

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.

You can see this with

unset n
echo $((n++,k=$n))

The error message,

bash: n++,k=: syntax error: operand expected (error token is "=")

shows that $n is replaced before the whole arithmetic expression is handled.

In your case, the expression which is evaluated is

n++,k=3
Stephen Kitt
  • 481.4k
  • 60
  • 1.2k
  • 1.4k