Scripting

Check the Number of Arguments in Bash

At times while running a Bash script, we also pass certain arguments to these scripts that might be used in different calculations within a script. Moreover, the number of these arguments may also play a very significant role in the overall result of a Bash script depending upon its logic. Therefore, this article will teach you the different methods through which you can conveniently check the number of arguments in a Bash script.

Finding Out the Number of Arguments in Bash

There are different methods that can be used for checking the number of arguments that are passed to a Bash script while executing it. You can learn these methods by going through the following examples:

Example 1: Printing Only the Number of Arguments

In this example, we print the total number of arguments that are provided to a Bash script. For that, we wrote the simple Bash script shown in the following image:

In this script, we used the “$#” special variable of Bash which holds the total number of arguments provided to a Bash script. We simply used this variable with the “echo” command to print its value on the terminal.

To test this script, we used the following command:

$ bash args.bash 1 2 3 4 5

We passed five arguments to this Bash script while running it. Therefore, the number of arguments passed to this Bash script turned out to be “5” as shown in the following image:

Example 2: Printing the Values of Arguments Along with Their Number

Now, we also print the values of the arguments on the terminal along with their total number. For that, we use the following Bash script:

In this example, we want to pass two arguments to our Bash script while executing it. Therefore, we printed the values of the “$1” and “$2” special variables. However, you can also use the “$3”, “$4”, and so on, special variables if you intend to pass more than two arguments to your Bash script. Then, we simply used the “$#” special variable to print their total number on the terminal.

While executing this script, we intentionally passed two arguments to it as shown in the following illustration. If you pass more than two arguments to the same script, the values of only the first two are printed because we hard coded it in our script.

$ bash args.bash 1 2

The total number of arguments that were provided to our Bash script along with their exact values are shown in the following image:

Example 3: Using the “[email protected]” Special Variable

In this example, we will not restrict the number of arguments that can be provided to a Bash script. You can consider the following Bash script to clearly understand this:

In this script, we used the “[email protected]” special variable to print the values of the provided arguments on the terminal. The “[email protected]” special variable acts as a buffer that holds the values of all the arguments provided to the Bash script. Therefore, we can pass as many arguments as we want to this Bash script. Then, we used the “$#” special variable to print the total number of arguments on the terminal.

To execute this script, we used the command that follows. This time, we passed three arguments to our Bash script:

$ bash args.bash 1 2 3

The total number of arguments along with their values are shown in the following image:

Example 4: Using the “$*” Special Variable

This example is pretty much similar to our third example. The only difference is that this time, instead of using the “[email protected]” special variable, we will use the “$*” special variable which serves the exact same purpose. The modified Bash script for this example is shown in the following image:

In this script, we replaced the “[email protected]” with “$*”. The rest of the script is exactly the same as that of our third example.

We used the following command to execute this script:

$ bash args.bash 1 2 3

The total number of arguments and their values are shown in the following image:

Conclusion

With this article, we wanted to help you out with the methods of checking the total number of arguments passed to a Bash script. In this regard, we shared four different methods with you by using them in different examples. You can choose to follow any of these methods depending upon the format of the results that you wish to get.

 

Similar Posts