Check existence of input argument in a Bash shell script

It is:

if [ $# -eq 0 ]
    echo "No arguments supplied"

The $# variable will tell you the number of input arguments the script was passed.

Or you can check if an argument is an empty string or not like:

if [ -z "$1" ]
    echo "No argument supplied"

The -z switch will test if the expansion of "$1" is a null string or not. If it is a null string then the body is executed.

Leave a Comment