Positional Parameters

by mike on January 27, 2011

Special built-in environmental  variables are positional parameters which hold command-line arguments to positions with the names 1,2,3,4, etc. which are indicated by $1,$2,$3,$4, etc.  Argument $0 is the name of the script.   These parameters are named positional because within the script the reference is to the position they occur on the command line.

These positional  variables can be used by a script.  Those used typically are  variables $0-$9 and $#.   Once you have created a script and you name and execute the script you can begin using these positional parameters.  For a script named param.sh, when you call the script you can follow the script name with variables.  Here is an example:

sh param.sh server mail

In this example the script is called param.sh so that is the first parameter “$0” which is automatically used.  Then the next 9 parameters can be entered on the command line.  Here the first parameter is “server” “$1” and the second parameter is “mail” “$2”.

sh param.sh server mail
Call with 2 parameters
The Shell Script Name is param.sh
The first parameter is server
The second parameter is mail
The third parameter is
All parameters are server mail

Here is the actual script and a list of how to use the parameters.

$#        number of parameters
$0        script name
$1-9        parameters by position entered
$@        list all parameters used (places arguments on separate lines)
$*        list all parameters used (places arguments on one line separated by spaces)

#!/bin/bash

echo “Call with $# parameters”
echo “The Shell Script Name is $0″
echo “The first parameter is $1″
echo “The second parameter is $2″
echo “The third parameter is $3″

echo “All parameters are $@”

The parameters that you enter are picked up and used by the script, these can vary every time you use the script.    So if you create the script the output of these two examples will each be different because the positional parameters are different.

sh param.sh  wokstation tech research
sh param.sh  server  web  apache

You can use the builtin command set to assign values of the arguments for the positional parameters.  The set command is a builtin command that can be used with “-o” to turn a bash feature on and “+o” to turn a feature off. At times the set command can be used inside a script to assign values to variables. Here is a simple script that illustrates the point.  Here called with no parameters and called with one parameter the output is the same because they have been assigned values.

Sciptname param2.sh
#!/bin/bash
set server ip network
echo $3 $2 $1

sh param2.sh
network ip server
sh param2.sh 2
network ip server

These positional parameters are very useful when using them in scripts.

{ 4 comments }

noryungi January 27, 2011 at 10:05 am

One very useful thing I have done in the past is to use $0 to make the script do different things based on the name used to call it.

For instance, you can have a script named “foobar.sh”, with two symbolic links named “start_foobar” and “stop_foobar”. Based on the name used to call foobar, it will then start or stop (case $0 in … etc …) a service or daemon.

mike January 27, 2011 at 1:44 pm

Very interesting idea…thanks for the input.

azaghal January 27, 2011 at 11:04 pm

Another very useful case for this is when you want to create “instances” of the same script that uses some configuration – in order to pick-up differently named configurations based on the script name. This can be useful for some init.d scripts, for example (although it can feel a bit hackish), reducing the need to propagate changes to all instances of the script.

SerInformaticos January 29, 2011 at 9:25 am

Very clever!

Comments on this entry are closed.

Previous post:

Next post: