Skip to main content
2 of 5
deleted 7 characters in body
MS.Kim
  • 1.7k
  • 4
  • 18
  • 18

Bash's conditional operator and assignment

Can we use bash's conditional operator with assignment operators like this?

Bash reference manual explains the arithmetic operators as follows.

  • conditional operator expr ? expr : expr
  • assignment = *= /= %= += -= <<= >>= &= ^= |=

Fisrt, this code seems to work well.

a=1; ((a? b=1 : 2 )) #seems to work

But when I use assignment operator after :, I got the attempted assignment to non-variable error.

a=1; ((a? b=1 : c=1)) #attempted assignment to non-variable error

Why we can use only assignment operators before colon?

MS.Kim
  • 1.7k
  • 4
  • 18
  • 18