Skip to content

Files

Latest commit

 

History

History
40 lines (30 loc) · 785 Bytes

SC2317.md

File metadata and controls

40 lines (30 loc) · 785 Bytes

Pattern: Unreachable command

Issue: -

Description

The problematic code wanted to exit with success if the user explicitly asked for --help. However, since the usage function already had an exit 1, this statement could never run.

One possible solution is to change usage() to only echo, and let callers be responsible for exiting.

Example of incorrect code:

usage() {
  echo >&2 "Usage: $0 -i input"
  exit 1
}     
if [ "$1" = "--help" ]
then
  usage
  exit 0   # Unreachable
fi

Example of correct code:

usage() {
  echo >&2 "Usage: $0 -i input"
}     
if [ "$1" = "--help" ]
then
  usage
  exit 0
fi

Further Reading