How to Check the Number of Arguments in the Bash Script

How to Check the Number of Arguments in the Bash Script

Are you looking for a tutorial on how to check the number of arguments in the Bash Script? Then this guide is what you need.

When we write scripts in Bash, we often need to provide arguments when executing them. Other times, we may use scripts written by others. In that case, we don’t know initially how many arguments to pass to run the script successfully. 

Knowing how many arguments you need to pass in a Bash script will save you much time and a headache when doing scripting tasks. We will show you multiple ways to do this, explaining each method in detail. 

Let’s get started!

How to Check the Number of Arguments in the Bash Script

As mentioned above, we will go through a few methods you can use to count the number of arguments passed in a Bash Script. Let’s check them one by one.

Check the Number of Arguments in the Bash Script Using $# Variable

The $# variable is a special variable to count the number of arguments passed to a script of function. It’s used for storing the number of command-line arguments. If no argument is passed, then its value will be 0. It can also keep track of positional parameters.

All of these will make more sense once you see a real Bash script implementing it. So let’s create a simple script. You can use any text editor for that. We will use the nano text editor. First, create a script file. In our case, we will name it argcount.sh. Create the script with this command:

$ touch argcount.sh

Then make the script executable by running this command:

$ chmod u+x argcount.sh

Output:

create bash script file

Now open the script in a file editor. For nano, the command goes like this:

$ nano argcount.sh

Write in this example script:

#!/bin/bash

if [ $# -eq 0 ]; then

  echo "No arguments provided."

elif [ $# -eq 1 ]; then

  echo "One argument provided."

else

  echo "Multiple arguments provided."

fi
How to Check the Number of Arguments in the Bash Script

Save the file with ‘Ctrl+O’ and then exit using ‘Ctrl+X’. Let’s understand what’s happening here. We’re using conditional if statements to check the value of the $# variable. The -eq operator is used when we want to equate the two sides. 

So if we provide no arguments, we should get the first echo output. For only one provided argument, we will get the second one. And for more than one output, we get the output in the ‘else’ statement. Let’s run the script and see if it works:

Run the script with this command:

$ ./argcount.sh [arguments…]

Output:

run bash script

As you can see from the above screenshot, we get the expected outputs. Let’s take a look at another example, where you get to keep count of the total arguments passed and also keep track of the positional parameters.

Here’s the script:

#!/bin/bash

echo "The total number of arguments is: $#"

echo "The first argument is: $1"

echo "The second argument is: $2"

echo "The third argument is: $3"
How to Check the Number of Arguments in the Bash Script

Now run this script and add arguments to it, like this:

$ ./argcount.sh para1 para2 para3
run bash script

However, if you exceed 9 arguments, you must put the positional parameters inside curly braces. Let’s see that in action:

#!/bin/bash

echo "The 10th argument is: ${10}"

echo "The 11th argument is: ${11}"

echo "The 12th argument is: ${12}"
How to Check the Number of Arguments in the Bash Script

To test this script, run the below command:

$ ./argcount.sh para1 para2 para3 para4 para5 para6 para7 para8 para9 para10 para11 para12
run bash script

If you wrote it like $10 or ${ 10 }, both would be wrong. The syntax ${10} is the only correct one.

Check the Number of Arguments in the Bash Script Using the shift Command

The shift command is mainly used for shifting the positional parameters. By using a loop, we can shift the positions of the arguments. We can then set a variable to count them. At the end of the loop, we will find the total number of passed arguments. 

All of these will make more sense once you see an example. Let’s use the following script:

#!/bin/bash

count=0

while [ -n "$1" ]; do

  count=$((count + 1))

  shift

done

echo "The total number of arguments is: $count"
How to Check the Number of Arguments in the Bash Script

In this script, we check whether the variable $1 (the first positional parameter) is not empty using the -n operator. The loop continues until there are no arguments left. Finally, we just print out the value of the count variable to see the number of arguments. Let’s run this script.

$ ./argcount.sh arg1 arg2 arg3 arg4 arg5

Output:

run bash script

You can learn more about the shift command on its manual page.

Check the Number of Arguments in the Bash Script Using $1 Variable

This is another way to check whether or not an argument was provided to the Bash script. Unlike the other two methods, you can only check the value of specific positional parameters.

Let’s make a quick example:

#!/bin/bash

if [ -z "$1" ]; then

  echo "No arguments provided."

else

  echo "At least one argument provided."

fi
How to Check the Number of Arguments in the Bash Script

In this script, we are checking if the length of $1 is zero or not, meaning if it’s empty or not. In case it’s empty, that indicates no argument was passed. Otherwise, at least one argument was provided. Let’s run to test the script:

$ ./argcount.sh
$ ./argcount.sh arg1
$ ./argcount.sh arg1 arg2

Output:

run bash script

Depending on the number of arguments we provide, we are getting the expected output.

Check if a Specific Number of Arguments Were Provided

One last example we’ll use is one where the script doesn’t continue until the user inputs a specific number of arguments, say 5, for example. If the user passes fewer or more arguments, the script will keep asking for that exact number. Here’s the script:

#!/bin/bash

expected_args=5

while [ "$#" -ne "$expected_args" ]; do

  echo "Please provide $expected_args arguments."

  read -ra args

  set -- "${args[@]}"

done

echo "All $expected_args arguments provided. Continuing with the script..."
specific arguments required in bash script

This loop will continue to ask the user for arguments until exactly 5 arguments are provided. Let’s run the script and see what happens:

$ ./argcount.sh 1 2 3 4
$ ./argcount 1 2 3 4 5

Output:

run bash script

Final Thoughts

This tutorial shows you how to check the number of arguments in the Bash script. We’ve shown you multiple ways to do so. The provided examples will help you understand how each method works. If you don’t understand something, feel free to let us know in the comments. Don’t forget to check out our other tutorials on Bash commands.

If this guide helped you, please share it.

Related Posts