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

Shell parameter expansion happens before any arithmetic expansion.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

Shell parameter expansion happens before any arithmetic expansion. 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

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
Source Link
Stephen Kitt
  • 481.3k
  • 60
  • 1.2k
  • 1.4k

Shell parameter expansion happens before any arithmetic expansion. 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