How to Check the Number of Arguments Passed to a Bash Script?

In the Bash script, the arguments are passed to obtain the dynamic features of the command. For example, the users pass the command line arguments, and the script generates results accordingly. Sometimes, the user may want to check the number of passed arguments to the script or check whether the particular argument is passed.

In today’s post, the following outline will enlighten the various examples to check the number of passed arguments in a Bash script.

  • Check the Number of Passed Arguments to a Bash Script
    • Check if No Argument is passed
    • Check the Expected Number of Passed Arguments
    • Check if the Particular Argument is Passed

Check the Number of Passed Arguments to a Bash Script

To check the number of passed Arguments to Bash script, Consider the following syntax.

Syntax 

#!/bin/Bash
if [ "$#" <operator> <value> ]
then
  Statement to execute
else
  Statement to execute
fi

The syntaxis defined as: 

  • if” condition to check the passed arguments using the “$#” variable along with appropriate <operator> and <value>.
  • The “then” section executes statements if the condition is true.
  • The “else” section to execute the statement if the condition fails.

Example 1: Check if No Argument is passed

By default, the special variable “$#” hold the number of passed arguments to the Bash script. This variable can be utilized to apply the checks on several passed arguments. The following script will check if no arguments are passed:

#!/bin/Bash
if [ "$#" -eq 0 ]
then
  echo "No arguments is Passed"
else
  echo "The Number of Passed Arguments is: $#"
fi

The script is defined as: 

  • if” condition to check if the passed arguments are equal to 0 using the “$#” variable.
  • The “then” section prints the appropriate message if the condition is true.
  • The “else” section is executed if at least one argument is passed and displays the total number of the passed argument.

Save the above script in the file.

Run the script file using the Bash command with no argument:

$ bash script.sh

As no arguments were passed, the appropriate message is displayed.

Example 2: Check the Expected Number of Passed Arguments

The subsequent script checks if the expected number of arguments are passed, not more or less:

#!/bin/Bash
if [ "$#" -ne 2 ]
then
  echo "Sorry! The Number of Arguments Must be 2"
else
echo "Two Arguments are Passed $1 and $2"
fi

The script is defined as:

  • if” condition to check if the passed arguments are not equal to 2.
  • The “then” section prints the appropriate message to ensure that the passed argument must be 2.
  • The “else” section is executed and displays the arguments stored in the $1, $2 variable if exactly two arguments are passed.

Save the script and exit.

Run the script file and test by passing the arguments:

$ bash script.sh 78 98 67
$ bash script.sh 78 98

In the first time, three arguments are passed. Hence the message is displayed that argument must be 2. Then, two arguments are passed the second time, and the message is displayed along with the passed arguments.

Example 3: Check if the Particular Argument is Passed

Likewise, to check if the particular argument is passed, use the following script:

#!/bin/Bash
if [ -n "$3" ]
then
  echo "The Third Argument is Passed"
else
  echo "The Particular Argument is not Passed"
fi

The script is defined as: 

  • if” condition to check if the third passed argument is passed using the “n” operator. Here “n” returns true if the $3 is non-empty
  • The “then” section prints the appropriate message if the third argument is present.
  • The “else” section is executed if the third argument is not passed.

Save the script in the file and exit.

Let’s check the script by passing the arguments:

$ bash script.sh 12 13 14
$ bash script.sh 12 13

At the first time, the three arguments are passed, and the message is printed that the third argument is passed. The second time, two arguments are passed, and the message is printed that the particular argument is not passed.

Conclusion

In the Bash script, to check the number of passed arguments, use the special variable “$#” that holds the total number of passed arguments. This variable can be used in the “if” statement to check the number of passed arguments. The user can also use the “$1, $2, $3” variables to check whether the particular argument is passed.

This write-up has illuminated the examples to check the number of passed arguments to the Bash script.