I'm looking for the equivalent to this JS assignment:
FOO = FOO || "I must have been falsey!";
I'm looking for the equivalent to this JS assignment:
FOO = FOO || "I must have been falsey!";
Either of these expansions might be what you're looking for, depending on when exactly you want to do the assignment:
Omitting the colon results in a test only for a parameter that is unset. [...]
${parameter:-word}If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
${parameter:=word}If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.
If you just want to set a default on first use, then:
some-command "${FOO:=default value}"
other-command "$FOO" # both use "default value" if FOO was null/unset
If you want to be explicit about it:
FOO="${FOO:-default value}"
some-command "${FOO}"
It depends on what you mean by false. Bash doesn't have any special values for True or False, so the only "false" value is 0. Then, a variable can be set to an empty string or it can be unset.
Set the variable if it isn't already set, or if it's set to NULL (\0) or the empty string:
## unset variable
$ foo=${foo:="I must have been unset empty or null!"}
$ echo "$foo"
I must have been unset, empty, or null!
$ foo="" ## set to the empty string
$ foo=${foo:="I must have been unset, empty, or null!"}
$ echo "$foo"
I must have been unset, empty, or null!
Set the variable if its current value is false (0):
$ foo=0
$ (($foo)) || foo="I must have been unset, empty, or null!"
$ echo "$foo"
I must have been unset, empty, or null!