I believe glenn jackman's got it, but I want to elaborate a little more than will fit in a comment.
Shell scripts aren't oriented around expressions. They're oriented around commands and words. Shell variables are strings by default (in the original Bourne shell, there was no way to create any other kind of variable). The RHS of an assignment isn't an "expression" the way you're thinking of it, and your b
and c
aren't "expressions" either, even if you wish they were.
The basic assignment syntax is
var=string
The right hand side is just a string. The shell offers a few kinds of expansions that can be used to build strings, which make it possible to do stuff like
var1=string
var2=$var1
var3="This is a ${var1}"
var4=$(ls | wc -l)
var5=$(($var4 + 1))
The value that is assigned to each variable is the string on the RHS with some expansions (marked by $
) performed in it.
The last example, with $((...))
, is the only one that really contains something that can be called an "expression". The double-parenthesized expansion works on arithmetic expressions, so this would be the thing to use if your b
and c
are numeric.
So you should stop thinking of your desired result in terms of an expression being evaluated with the two child expressions b
and c
as inputs, because the concepts just don't generalize that way in the shell language.
If b
is a variable and you want to expand it with a default value to be used when the variable is unset, then use ${b-c}
. If you also want the default value to be used when the variable is set but empty, then use ${b:-c}
. The c
in these examples is a string, and like all other strings in the shell it can be built with expansions:
i=3
var=""
echo ${var:-$(cat somefile)} # since var is empty, substitute file contents
echo ${var:-$(($i * $i))} # since var is empty, do some math instead
Now if your b
is not a variable like $var
, you'll probably find that none of the above applies! Because The ${...:-...}
syntax is for variable expansion. You really will have to specify what b
is, and you can't say "expression" because in the shell there's no such thing.
||
operates on command exit statuses.b||c
is a command that runsc
ifb
fails. Did you mean it that way, and you wanta
to get the output of the command? Or areb
andc
supposed to be variables, in which case you should write them as$b
and$c
and the||
operator will not apply... – Playbill$b
but use the value of$c
instead when$b
is empty, you can use${b-$c}
in a proper shell (i.e. not csh) – Playbilla
would be a variable andb
andc
would be an expression resulting in some value. Imagine passing an argument; Isa= $1 || "some default value"
acceptable, or do you have to test the$#
of arguments, or that$1
is populated? – Shawnshawna${1-"some default value"}
is perfectly acceptable – Playbill