You can provide any desired number of command-line arguments to your Bash scripts in Ubuntu 20.04 while executing them. However, sometimes the scripts are written in such a dynamic manner that even the programmer does not know exactly how many arguments will be provided by the user at runtime, but he might want to use that total number somewhere later in that script.

In this situation, there must be a way through which you can check the total number of arguments passed to any particular Bash script. For that, Bash has a special variable, i.e., $#. To figure out how this special variable works, you will have to go through all the examples shared in this article.

Method of Checking the Number of Arguments in Bash in Ubuntu 20.04:

For explaining to you the method of checking the number of arguments provided to a Bash script in Ubuntu 20.04, we have designed a few examples that are discussed below:

Example # 1: Printing the Total Number of Arguments in Bash:

To simply print the total number of arguments passed to a Bash script in Ubuntu 20.04, you can write a Bash script like the one shown in the following image:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image1-52.png" data-lazy- height="107" src="data:image/svg xml,” width=”798″>

The $# special variable always holds the total number of arguments passed to any specific Bash script.

For testing this script, we have executed it with three arguments or parameters as follows:

$ bash Arguments.sh 1 2 3

Here, Arguments.sh is the name of our Bash script file, whereas 1, 2, and 3 are the arguments that we have passed to this Bash script. It means that the total number of arguments in this test case is “3”. Let us try to find out if this Bash script has correctly displayed the total number of arguments or not.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image3-47.png" data-lazy- height="22" src="data:image/svg xml,” width=”458″>

When we execute this Bash script, it will display the total number of arguments passed to it, which can be seen from the image shown below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image2-53.png" data-lazy- height="77" src="data:image/svg xml,” width=”472″>

Example # 2: Printing the Total Number of Arguments along with the Values of Arguments with Positional Parameters in Bash:

Now, we will write a Bash script that will print the values of the arguments passed to a Bash script and their total number. For doing that, the Bash script is as follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image5-40.png" data-lazy- height="140" src="data:image/svg xml,” width=”796″>

In this Bash script, we have first printed the values of the three positional parameters. It means that whichever arguments will be passed to this Bash script from the terminal will be stored in these three positional parameters. After that, we have simply printed the value of the $# special variable.

In our first example, we will execute this script with the same arguments as we passed on to the Bash script. This time when we will execute our Bash script, the values of the three passed arguments will also be displayed on the terminal along with their total number, as you can see from the image shown below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image4-45.png" data-lazy- height="113" src="data:image/svg xml,” width=”499″>

Example # 3: Printing the Total Number of Arguments along with the Values of Arguments with the [email protected] Special Variable in Bash:

The goal of this example is the same as that of our second example; however, in this example, we will be using another special variable, i.e., [email protected], for printing the values of the passed arguments. Basically, the [email protected] special variable can hold all the arguments that are passed to a Bash script. To understand this, you can take a look at the following Bash script that we have designed for you:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image7-32.png" data-lazy- height="142" src="data:image/svg xml,” width=”794″>

In this Bash script, we have simply printed the value of the [email protected] special variable, i.e., all the passed arguments, and the value of the $# special variable, i.e., the total number of the passed arguments on the terminal.

To see how this modified Bash script works, we have again executed it with the same parameters as we did in the two examples above. When this script was executed, the output turned out to be exactly the same as we had in our second example.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image6-38.png" data-lazy- height="107" src="data:image/svg xml,” width=”511″>

Example # 4: Printing the Total Number of Arguments along with the Values of Arguments with the $* Special Variable in Bash:

This is yet another modified version of our second example since, in this example, we will be using the $* special variable to print the values of the passed arguments on the terminal. Like the [email protected] special variable, the $* special variable can also hold the values of all the passed arguments to any particular Bash script. The modified Bash script is shown in the image below:

In this Bash script, we have simply printed the value of the $* special variable, i.e., all the passed arguments, and the value of the $# special variable, i.e., the total number of the passed arguments on the terminal.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image9-24.png" data-lazy- height="139" src="data:image/svg xml,” width=”794″>

To test this Bash script and visualize its output, we executed it with the same parameters as we did in our first three examples. This time also when our Bash script was executed, its output was the same as that of our second and third examples, as you can see from the following image:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image8-27.png" data-lazy- height="110" src="data:image/svg xml,” width=”464″>

Example # 5: Putting a Limit on the Total Number of Arguments in Bash:

Finally, the $# special variable can also be used to limit the total number of arguments passed to a Bash script in Ubuntu 20.04. To understand this phenomenon, you will have to go through the Bash script shown in the image below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image12-17.png" data-lazy- height="245" src="data:image/svg xml,” width=”800″>

In this Bash script, we have an “if” statement that is applied to the $# special variable. We wanted to limit the number of arguments to “3”. If the arguments will be less than “3” an error message will be printed on the terminal. Similarly, if the arguments will be greater than “3”, again an error message will be printed on the terminal. However, if the provided arguments will be equal to “3” only then the values of these arguments will be printed on the terminal.

We wanted to test all three conditions of this Bash script. For that, we have first executed this script with three parameters, and the corresponding output is shown in the following image:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image10-21.png" data-lazy- height="73" src="data:image/svg xml,” width=”474″>

After that, we executed this Bash script with four arguments, because of which an error message was printed on the terminal as shown in the image below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image11-17.png" data-lazy- height="62" src="data:image/svg xml,” width=”486″>

Finally, we executed this Bash script with two arguments, because of which an error message was printed again on the terminal as shown in the following image:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image13-14.png" data-lazy- height="58" src="data:image/svg xml,” width=”459″>

Conclusion:

The $# special variable will let you find out the total number of arguments passed to any Bash script very easily. By checking out the examples shared in this article, you would have a clear idea about the working of this special variable. Therefore, you can now write such Bash scripts without any worries that somehow use the total number of provided arguments to those Bash scripts at runtime on Ubuntu 20.04.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Author-Image-150×150.jpg612e421c658c2.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.