You can provide any ideal variety of command-line arguments to your Bash scripts in Ubuntu 20.04 though executing them. Nonetheless, from time to time the scripts are penned in this sort of a dynamic way that even the programmer does not know exactly how lots of arguments will be supplied by the consumer at runtime, but he could want to use that overall number someplace later in that script.
In this predicament, there should be a way by way of which you can check the whole range of arguments passed to any specific Bash script. For that, Bash has a special variable, i.e., $#. To figure out how this special variable performs, you will have to go by means of all the illustrations shared in this article.
Technique of Examining the Quantity of Arguments in Bash in Ubuntu 20.04:
For explaining to you the approach of checking the amount of arguments presented to a Bash script in Ubuntu 20.04, we have made a couple illustrations that are reviewed down below:
Example # 1: Printing the Full Quantity of Arguments in Bash:
To just print the overall quantity of arguments handed to a Bash script in Ubuntu 20.04, you can compose a Bash script like the one particular revealed in the adhering to picture:
The $# exclusive variable always holds the full number of arguments handed to any precise Bash script.
For screening this script, we have executed it with a few arguments or parameters as follows:
$ bash Arguments.sh 1 2 3
Listed here, Arguments.sh is the identify of our Bash script file, whilst 1, 2, and 3 are the arguments that we have passed to this Bash script. It suggests that the complete amount of arguments in this check situation is “3”. Permit us try to discover out if this Bash script has effectively shown the total amount of arguments or not.
When we execute this Bash script, it will display the full variety of arguments handed to it, which can be seen from the graphic proven below:
Example # 2: Printing the Total Selection of Arguments along with the Values of Arguments with Positional Parameters in Bash:
Now, we will compose a Bash script that will print the values of the arguments passed to a Bash script and their overall amount. For executing that, the Bash script is as follows:
In this Bash script, we have initial printed the values of the 3 positional parameters. It means that whichever arguments will be passed to this Bash script from the terminal will be saved in these a few positional parameters. Right after that, we have merely printed the worth of the $# particular variable.
In our first case in point, we will execute this script with the very 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 shown on the terminal alongside with their total amount, as you can see from the picture shown beneath:
Example # 3: Printing the Full Variety of Arguments along with the Values of Arguments with the [email protected] Exclusive Variable in Bash:
The intention of this instance is the exact as that of our 2nd instance even so, in this illustration, we will be applying another exclusive variable, i.e., [email protected], for printing the values of the handed arguments. Mainly, the [email protected] unique variable can maintain all the arguments that are passed to a Bash script. To recognize this, you can acquire a appear at the pursuing Bash script that we have made for you:
In this Bash script, we have only printed the price of the [email protected] distinctive variable, i.e., all the passed arguments, and the worth of the $# specific variable, i.e., the total selection of the passed arguments on the terminal.
To see how this modified Bash script is effective, we have again executed it with the similar parameters as we did in the two examples earlier mentioned. When this script was executed, the output turned out to be particularly the similar as we had in our next case in point.
Illustration # 4: Printing the Overall Selection of Arguments alongside with the Values of Arguments with the $* Particular Variable in Bash:
This is however an additional modified version of our next instance due to the fact, in this example, we will be making use of the $* special variable to print the values of the passed arguments on the terminal. Like the [email protected] exclusive variable, the $* distinctive variable can also hold the values of all the passed arguments to any certain Bash script. The modified Bash script is shown in the graphic underneath:
In this Bash script, we have only printed the benefit of the $* particular variable, i.e., all the passed arguments, and the worth of the $# special variable, i.e., the complete number of the handed arguments on the terminal.
To check this Bash script and visualize its output, we executed it with the similar parameters as we did in our very first a few examples. This time also when our Bash script was executed, its output was the identical as that of our second and third examples, as you can see from the pursuing picture:
Case in point # 5: Placing a Restrict on the Whole Range of Arguments in Bash:
Eventually, the $# exclusive variable can also be utilized to limit the whole number of arguments passed to a Bash script in Ubuntu 20.04. To fully grasp this phenomenon, you will have to go through the Bash script shown in the picture beneath:
In this Bash script, we have an “if” assertion that is applied to the $# special variable. We preferred to restrict the amount of arguments to “3”. If the arguments will be considerably less than “3” an mistake concept will be printed on the terminal. Likewise, if the arguments will be better than “3”, once again an mistake information will be printed on the terminal. Having said that, if the offered arguments will be equivalent to “3” only then the values of these arguments will be printed on the terminal.
We desired to examination all a few disorders of this Bash script. For that, we have initially executed this script with a few parameters, and the corresponding output is shown in the adhering to image:
Following that, we executed this Bash script with 4 arguments, since of which an mistake information was printed on the terminal as proven in the impression down below:
Last but not least, we executed this Bash script with two arguments, simply because of which an error information was printed once again on the terminal as proven in the adhering to image:
The $# unique variable will enable you come across out the overall variety of arguments passed to any Bash script incredibly effortlessly. By checking out the examples shared in this post, you would have a apparent concept about the working of this unique variable. Thus, you can now produce these kinds of Bash scripts with out any concerns that by some means use the complete quantity of furnished arguments to these Bash scripts at runtime on Ubuntu 20.04.