Skip to main content
edited tags; edited tags
Link
Gilles 'SO- stop being evil'
  • 866.5k
  • 205
  • 1.8k
  • 2.3k
Source Link
devios1
  • 819
  • 3
  • 9
  • 11

Setting a shell variable in a null coalescing fashion

I'm really fond of "null coalescing", where you can set a variable to the first "non-null" value in a list of things. Many languages support this, for example:

C#:

String myStr = string1 ?? string2 ?? "default"; 

JavaScript:

var myStr = string1 || string2 || "default"; 

...etc. I'm just curious if this can be done in Bash to set a variable?

pseudo:

MY_STR=$ENV{VAR_NAME}??$ANOTHER_VAR??"default";