1

I came across the following syntax that I think it looks like the ternary operator in c++ but searching for ternary operator in bash revealed that there is a ternary operator in bash (?).

var_a=${var_b:-$var_c}

How does this above syntax work. and what is the technical term for it? apologies if the title itself is misleading in some way.

Gr-Disarray
  • 514
  • 1
  • 6
  • 17

1 Answers1

0

It evaluates to $var_b, unless $var_b is empty in which case it evaluates to $var_c.

Look for :- in the bash man page:

${parameter:-word}

Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

Other similar constructs include := to assign a default value, :? to error out if the variable is unset, and :+ to substitute an alternate value.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578