Skip to main content
4 of 5
deleted 4 characters in body
Hauke Laging
  • 94.6k
  • 21
  • 132
  • 185

Bash's conditional operator and assignment

Can we use bash's conditional operator with assignment operators after colon?

Bash reference manual explains the arithmetic operators as follows.

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

First, this code seems to work well:

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

But when I use assignment operators after :, I got 'attempted assignment to non-variable' error:

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

Why can we use only assignment operators before colon?

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