Is it possible to put more than 1 condition in an if statement?
if [ "$name" != "$blank" && "$age" == "$blank" ]; then
Is it possible? If not how am I supposed to do that?
With [ expression ]
(POSIX Standard) syntax you can use the following:
if [ "$name" != "$blank" ] && [ "$age" = "$blank" ]; then
echo true
fi
But in [[ expression ]]
syntax you can use both conditions:
if [[ $name != "$blank" && $age == "$blank" ]]; then
echo true!
fi
Two advantages of [[
over [
:
[[
, and therefore many arguments need not be quoted (with the exception of the right-hand side of ==
and !=
, which is interpreted as a pattern if it isn't quoted).[[
easier to use and less error-prone.Downside of [[
: it is only supported in ksh, bash and zsh, not in plain Bourne/POSIX sh.
My reference and good page to comparing [[
and [
: bash FAQ
Security implications of forgetting to quote a variable in bash/POSIX shells
Yet another possibility not mentioned by @SepahradSalour is to use -a
operator:
if [ "$name" != "$blank" -a "$age" = "$blank" ]; then
BTW, be sure to quote properly all variables separately.