Skip to content

Files

Latest commit

 

History

History
53 lines (39 loc) · 867 Bytes

SC1075.md

File metadata and controls

53 lines (39 loc) · 867 Bytes

Pattern: Use of else if instead of elif

Issue: -

Description

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

Exceptions

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

Further Reading