Pattern: Use of else if
instead of elif
Issue: -
Many languages allow alternate branches with else if
, but sh
is not one of them. Use elif
instead.
Example of incorrect code:
if [ "$#" -eq 0 ]
then
echo "Usage: ..."
else if [ "$#" -lt 2 ]
then
echo "Missing operand"
fi
Example of correct code:
if [ "$#" -eq 0 ]
then
echo "Usage: ..."
elif [ "$#" -lt 2 ]
then
echo "Missing operand"
fi
else if
is a valid (though confusing) way of nesting an if
statement in a parent's else
. If this is your intention, please use canonical formatting and put a linefeed between else
and if
.
if x
then
echo "x"
else # line break here
if y
then
echo "y"
fi
fi