Updated April 10, 2023
Introduction to Shell Script Parameters
Shell Spscript parameters are the entities that are used to store variables in Shell. Among these parameters, the named spaces are there in the memory and they permit us to access these stored variables. Generally, there are two types of parameters. They are called variables and special parameters. A parameter can be an argument or a variable can be called as a parameter even though it is not used as part of a command argument. Special parameters are pre-set by the shell and these parameters are read-only. The variables are managed by the shell or by the users.
Why do we need Shell Script Parameters?
To add additional features to the command while working with shell script can be achieved by using parameters i.e. by the help of command-line options along with the arguments. Parameters are used to help in choosing options from the command line. For example, we can use the parameters to have an interactive command line to display a particular file name and can perform operations such as overriding it or just simply to check if the file exists or not. Also, we can read the content of a shell script by the use of positional parameters.
All Shell Script Parameters with Examples
Below are the parameters used in a shell script.
- Variables: The variables which are a type of parameter are generally managed by the users or the system. We can take an example of $var which is a variable parameter. The system sets $var, but this variable parameter can be written by the user. So it’s not read-only, like the special parameters.
- Special Parameters: The special parameters are read-only which are maintained by the shell. The special parameters are with a predefined meaning. Below are the various special parameters:
Parameters | Description |
$# | It parameter represents the total number of arguments passed to the script. |
$0 | This parameter represents the script name. |
$n | This parameter represents the arguments corresponding to a script when a script is invoked such $1 $2…etc. $1, $2…etc are called positional parameters. |
$* | This parameter describes the positional parameters to be distinct by space. For example, if there are two arguments passed to the script, this parameter will describe them as $1 $2. |
$$ | This parameter represents the process ID of a shell in which the execution is taking place. |
$! | This parameter represents the process number of the background that was executed last. |
$@ | This parameter is similar to the parameter $*. |
$? | This parameter represents exit status of the last command that was executed. Here 0 represents success and 1 represents failure. |
$_ | This parameter represents the command which is being executed previously. |
$- | This parameter will print the current options flags where the set command can be used to modify the options flags. |
Examples of a few special parameters as shown below:
$cat program.sh
echo "The File Name is: $0"
echo "The First argument is: $1"
echo "The Second argument is: $2"
$ sh program.sh ab cd
The File Name: program.sh
The First argument is: ab
The Second argument is: cd
Advantage of Shell Script Parameters
One of the main advantages of using the parameters is that while passing the arguments to the function or a script, the code can be reused again and again. Here we do not need to open the script and the arguments can be passed by simply writing them after the script name with a space separating the arguments and the parameters can be used by the number of positions by $. Also with the usage of the parameters, the command line arguments can be used by shifting the position of the arguments. The advantage of passing the arguments improves security as there is no need for parsing the environment variables. Also, it adds flexibility where many shell interpreters can be used.
Rules and Regulation for Shell Script Parameters
- Special parameters are used to deliver information to programs by specifying the arguments in the command line. $n can be described by one or more digits, such as $1, $2, $3…., where $1, $2, $3 etc are the arguments to the command. The positional parameters are generally passed along with the command when it is invoked. But the parameter must be enclosed within {} where the parameter consists of more than 1 digit.
- The reading of a variable is called parameter expansion. It can be described as below:
$ var=apple
$ echo "The variable is $var"
- The default values can be provided for variables by using brackets or test commands. If a parameter which is not declared having a null value, then we need to use the default value. Also, parameters are checked to see if they represent specific words or directories or file names etc.
- The double quotes are used to treat most of the special characters as letters.
$ var=apple
$ echo "var$var"
$ echo $var"abcd"
The above command will show the below result
$ varapple
$ appleabcd
To read $ as a normal character, we need to use a single quote as below.
$ echo 'var$var'
$ var$var
- In order to perform arithmetic operations, we need to use $(( )) as below.
$ a=5
$echo 5+9 $a+9
$echo $((5+9))
$echo $(($a+9))
The result of $echo 5+9 $a+9 is 5+9 5+9. But the result of $echo $((5+9)) and $echo $(($a+9)) is 14.
Conclusion
The shell script parameters help in performing effective and flexible scripting with many added features. Such features are parameter expansion; advanced tests along with double square brackets which can be used as pattern matching. Also, parameter manipulation and arithmetic operations can be done very easily.
Recommended Article
We hope that this EDUCBA information on “shell script parameters” was beneficial to you. You can view EDUCBA’s recommended articles for more information.