In some Bash scripts, there is an option to pass arguments to the script when you are executing it. This allows the user to specify more information in the same command used to run the script.
If you plan on giving users the option to pass arguments in your Bash script, it is important to include some type of error checking to verify that the expected number of arguments have been passed. Additionally, you can have your script react differently depending on the number of arguments that are passed. And this is why you will need to detect the number of arguments passed to the script.
In this tutorial, you will learn how to check the number of arguments passed to a Bash script on a Linux system. We will go over a few examples so you can see what this functionality looks like inside of a Bash script.
In this tutorial you will learn:
- How to detect number of arguments passed to a Bash script
- How to access the arguments that were passed
- How to use number of arguments for error checking
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Software||Bash shell (installed by default)|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Examples for checking the number of arguments passed to a Bash script
See some of the examples below on checking the number of arguments passed to a Bash script. You will also see how this information can be useful in the context of error checking and determining what to do next in the script.
- The number of arguments is always stored inside of the
$#variable. We can use this in our Bash script like so:
#!/bin/bash echo "The number of arguments in this script is: $#"
Here is what happens when we execute this script:
$ ./test.sh The number of arguments in this script is: 0 $ ./test.sh arg1 arg2 arg3 The number of arguments in this script is: 3
- The arguments themselves are stored inside variables like
$3and so on. Check the example below to see how we can access the arguments from within the Bash script:
#!/bin/bash echo "Number of arguments: $#" echo "The arguments are: $1 $2 $3"
Here is what happens when we execute the script with three arguments:
$ ./test.sh arg1 arg2 arg3 Number of arguments: 3 The arguments are: arg1 arg2 arg3
- The problem with the script above is that it will only work with three or less arguments. But maybe we are not sure how many arguments the user is going to append, and we want to echo all of them. In that case, we can use the
$@variable, which contains all the arguments that were passed. Note that you can alternatively use the
$*variable that does the same thing.
#!/bin/bash echo "Number of arguments: $#" echo "The arguments are: $@"
Here is what happens when we execute the script with five arguments:
$ ./test.sh arg1 arg2 arg3 arg4 arg5 Number of arguments: 5 The arguments are: arg1 arg2 arg3 arg4 arg5
- We mentioned error checking earlier. A common component of some Bash scripts is that they expect a particular number of arguments. If the user forgets an argument, or perhaps puts too many arguments, the script can issue an error and fail to proceed. Here is a script that will only proceed if it detects there are a total of three arguments:
#!/bin/bash if [ $# -ne 3 ]; then echo "please specify 3 arguments" else echo "there are 3 arguments" echo "the arguments are $@" fi
Here is what happens when we execute the script with the correct and incorrect number of arguments.
$ ./test.sh arg1 arg2 arg3 there are 3 arguments the arguments are arg1 arg2 arg3 $ ./test.sh arg1 arg2 please specify 3 arguments $ ./test.sh arg1 arg2 arg3 arg4 please specify 3 arguments
The script works by using an
ifstatement to check if the number of arguments is not equal to three. If it is not, then you will get the error message. If the number of arguments is equal to three, then the
elseclause is triggered, which will echo the arguments.
In this tutorial, you saw how to check the number of arguments in a Bash script on Linux. You also learned how to access the data in those arguments, and which variables are used to store that information. Since checking the number of arguments is often used in error checking, particularly with an
if statement, you saw an example of that as well. Feel free to use our examples on your own system and edit them to fit into your own scripts as needed.