How to check that a parameter was supplied to a bash script [duplicate]

Use $# which is equal to the number of arguments supplied, e.g.:

if [ "$#" -ne 1 ]
then
  echo "Usage: ..."
  exit 1
fi

Word of caution: Note that inside a function this will equal the number of arguments supplied to the function rather than the script.

EDIT: As pointed out by SiegeX in bash you can also use arithmetic expressions in (( ... )). This can be used like this:

if (( $# != 1 ))
then
  echo "Usage: ..."
  exit 1
fi

Leave a Comment