Join my free Bash scripting course on Udemy 👀

Check if any arguments are passed to a bash script

July 3, 2022  ‐ 1 min read

In some cases your bash script may rely on arguments that are passed from the command line. If sensible defaults are not an option you can exit your script when no arguments are passed.

To do so we make use of the special variable $# which contain the number of positional arguments passed to the script. If this number is equal (-eq) to zero we can exit our script.

#/bin/env bash

if [ $# -eq 0 ]; then
  echo "Pass at least one argument"
  exit 1
fi

echo "$@"