Updated April 14, 2023
Introduction to Bash Script Arguments
Bash Script Arguments, in the world of programming, we often encounter a value that might be passed for the successful execution of a program, a subprogram, or even a function. These values are mostly referred to as arguments or parameters. These data type contains data or codes. Now, as we speak, we often interchange the word parameter and argument, but if we look it deep down there is a very subtle difference between them. When an argument customizes a program, it is referred to as a parameter. Hence it is okay to sometimes interchangeably use them, till the meaning remains intact.
How does Bash Script Arguments Works?
Now let us look at how does bash script arguments are actually passed when a script is called. We already know that if we have to run a bash script, we would need to run bash <filename.sh> from the location where we have the file. Now along with that, any succeeding variable we send at the time of the bash script execution goes as arguments to the bash script. The syntax goes as bash <filename.sh> <argument1> <argument2>… <argument‘n’>. We need to be careful of the fact that each argument will be separated by a space in between them!
Now, in the next few lines, we will look at some special variable which can be used inside the script to take full advantage of the arguments passed through the bash script.
$1 to $n: As we pass different arguments, each of them gets stored in the chronological order it is sent from the script and hence can be referred to like $1 for the first argument, $9 for the 9th argument, and so on. Now you must be wondering that how can one refer to a two-digit numbered argument. The answer is as simple as the question, we would use curly brackets. Thus, the 10th argument can be referred to as ${10}, 9999th argument can be referred to as ${9999}, a nearly impossible number to reach in terms of arguments through the command line!
$0: This position is reserved for the script name, which is getting executed!
$$: This particular variable will return the process id of the current shell where the code is getting executed.
$*: This will return all the values of the arguments and it will be double-quoted when returning.
$#: This will calculate and return the total number of arguments that are passed to the script.
$@: This has utility the same as $* and will return the values of all the arguments.
$?: This will return the exit status id of the last command that is executed.
$!: With? we would get the exit status id, and with ! we would get the process id of the last command executed.
Different Utilities of Arguments Passed in Bash Script
Now that we are well aware of different feature set bash script arguments to bring to the table, it will be erstwhile to look at different utilities of arguments passed in bash script.
- If one needs to specify the output path, where we would want the files to be written to post the execution of the code, the arguments can easily take the advantage of sending the file path through the argument. But one would need to keep in mind that the path shouldn’t have space in between, or in case it has should be fit within single inverted commas.
- In case the code performs a check of the presence of a file entered by the user, passing the filename through the argument will be a useful use case for arguments’ utility.
- In some cases, one would like to present a glossary to help out the user on different asks it might have on running the code and if there is an option of using “-help” as an argument might be convenient for the user using the bash script!
Now we can easily find more such use cases where the presence of arguments would mean a lot in providing an interactive mode for the user to use the bash script or maybe for the developer to develop better interactive script!
Example to Implement Bash Script Arguments
From the above theoretical concepts, we discovered some features which can be utilized effectively as a bash script argument and now in this section we will discuss an example where we would showcase the above features’ actual working right in front of your eyes giving you enough confidence to try them in real-life problem-solving.
Example #1
Code:
#!/bin/bash
echo "**In the chronological order of the argument:**"
echo "The first argument is $1"
echo "The second argument is $2"
echo " "
echo "The name of the script running is: $0"
echo "The process id is: $$"
echo " "
echo "All the values passed in the script are: $*"
echo " "
echo "Similar to the * command @ command also prints all the values passed in the script as: $@"
echo " "
echo "Total number of arguments that are passed in the script running are: $#"
Output:
Conclusion
Now we are pretty confident that you would be able to utilize the capability of arguments in bash script in your day to day working and hence keep exploring more advanced scripts where the passing of arguments would ease off the task for you. With the example in the article, we are sure that the intuition behind the utility is crystal clear and hence now we sign off till we meet again in an exciting episode of learning bash and shell script the EduCBA way.
Recommended Articles
We hope that this EDUCBA information on “Bash Script Arguments” was beneficial to you. You can view EDUCBA’s recommended articles for more information.