Updated April 10, 2023
Introduction to Shell Script Argument
In today’s world, when we interact in any of the major operating systems we indirectly interact with Shell. For example, a user running terminal on Unix or Linux is interacting with Shell. In understanding about shell and the arguments in shell scripting, it becomes imperative for us to understand a few terminologies behind which the entire argument structure of shell script is based on and on that front we would like to take you through few concepts of Kernel, Shell and Terminal in this article which will set the base right for understanding what shell is and correspondingly about arguments of Shell Script and finally at the end of the article we would end up with the importance of arguments in shell script and few examples! So, without much ado, let’s get the ball rolling!
How does the Argument in Shell Scripting work?
As we discussed, let us first take a glance at the initial concepts of Shell scripting let us take some time to understand the concepts of Kernel and then move to understand Shell and finally terminal. Understanding these terms will be a great boon in getting insights when we would be learning about arguments in shell scripting.
In an operating system, a Kernel is a program running in an operating system which is the core of the operating system and has access and complete control over everything in the operating system. It is capable of managing Files, Processes, Input-Output, Memory, and Device. Though there are many other things which a Kernel can manage the ones listed are the major ones widely used in the industry. Now coming to understanding the concept of Shell. Shell is a user program that allows the user to use the services of the operating system. Users can input the commands in the form of human-readable commands and the shell will convert it into an input for Kernel to understand. This command can be through the command line or GUI. Lastly, we talk about the Shell scripting. Now essentially it might be possible that we would be not writing just one command at a time for execution and in that case a bunch of commands would be present. Scripting helps in executing the bunch of commands in the script to be executed routinely, so that input of the commands to the Kernel is automated. All in all, shell scripting enables the smooth and automated execution of commands to perform a defined set of tasks!
And in fulfilling this task, argument plays a very big role in shell scripting. When a script is run, one can feed arguments to the script for it to work. Another thing, there is a word known as the parameter which is interchangeably used with argument. So for our readers, we would like them to keep in mind that parameter is something which is defined to provide information to the command or its options or if it is positional parameter can be the value of the variable stored in the context of the shell; whereas on the other hand, the argument is passed when the script is being executed.
When an argument is passed, each of the words separated by space is treated as a separate argument, except for the one where space is within double quotes “ ”. As and when the argument is passed into the script, each of the space-delimited variables is given a variable reference and using that particular reference, one can easily reference that value throughout the code which mentioning the value itself. The first argument which is passed is the path of the shell script itself, hence called the “Basename” or $0. Another thing we wanted to mention here was that any variable is reference as $<Number>, where <Number> is the order of the arguments passed while running the script. Hence, when we say $0, it is the first argument that is sent in running the shell script file, and this variable is always supposed to be the name of the file itself. Few other interesting variables we can reference are $@ which signifies a list of all the arguments which have been passed apart from the “Basename”, $# which signifies the number of arguments that are passed.
TRIVIA: One interesting variable is $* which signifies the same as $@ i.e. all the arguments except the “Basename”, but it doesn’t preserve any whitespace or quotes.
Importance of Shell Script Argument
We got a flavor of what are shell script arguments and a deep dive into how they are used in shell scripting. And now, it becomes imperative for us to note down the importance of the shell script argument, so that one using them can appreciate it, even more, when those are getting used in his/her day to the day job!
- The script doesn’t have any hard-coded value, hence can be reused for similar kind of task for some other arguments.
- Increased security instance as we won’t need to explicitly expose the variable in the code itself and can be input to the shell script during runtime.
- Improvement in terms of flexibility to various shell interpreters.
Example to Implement Shell Script Argument
Below is the example of Shell Script Argument:
Example #1
In this section, we would look at an example, which will carve out the factual interpretations of shell scripting arguments through an example
Code:
$ ./bin/shell_script_arg.sh learn "shell scripting" from EduCBA in 2020
Essentially, this command means that there is a file named shell_script_arg.sh which has several parameters defined inside the script file and when we are running this shell script we pass these parameters as “arguments” to the command. The various commands which are passed as an argument are:
- learn
- shell scripting
- from
- EduCBA
- in
- 2020
All the arguments are taken as parameter $<number> in the shell script file during execution. If you look at the argument mentioned in $2 you would eventually notice that even though there is a space in between “shell” and “scripting” both of them together along with space is treated as a parameter. These variables can be referenced using $<number> in the entire shell script file itself. One can also reference any other variable to the parameter from the argument. For example, in the code there is a syntax:
Code:
YEAR = $6
echo "Year of learning is: $YEAR"
Explanation: One would see “Year of learning is: 2020” when the script is executed.
Conclusion
So, in a nutshell we have understood the various aspects of shell script arguments and their importance in our day to day job. We also touched upon the difference between parameter and argument, which essentially lies in the fact that argument is passed to shell script and parameters are either information to the command or variable inside the script itself! So with this we conclude this topic and hope you are ready to ease out tasks in the vast world of shell scripting.
Recommended Articles
We hope that this EDUCBA information on “Shell Script Argument” was beneficial to you. You can view EDUCBA’s recommended articles for more information.