As the title describes, what's the proper way to do an or-assignment (eg a= b || c
) in shell scripting, specifically csh vs bash? I cannot test this, so perhaps the example above works.
I thought this was pretty common in scripting languages, but for those that don't quite understand, the variable a
will retain the value of b
if truthy, otherwise the value of c
. In the example b
and c
are expressions.
The usecase is typically to set to a
to some kind of value if supplied, otherwise to use a default value (eg a= $1 || "Foo"
).
I'm not sure what the reason is for the close votes as this question is not:
Please comment if you require some further explanation and need some amendment.
For bash you want
a=${b:-$c}
http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
${parameter:-word}
If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.