Programming with the UNIX system shell

Positional parameters

A positional parameter is a variable within a shell program; its value is set from an argument specified on the command line that invokes the program. Positional parameters are numbered and are referred to with a preceding ``$'': $1, $2, $3, and so on.

A shell program may reference up to nine positional parameters. If a shell program is invoked with a command line that appears like this:

   shell.prog pp1 pp2 pp3 pp4 pp5 pp6 pp7 pp8 pp9
then positional parameter $1 within the program is assigned the value pp1, positional parameter $2 within the program is assigned the value pp2, and so on, at the time the shell program is invoked.

To practice positional parameter substitution, create a file called pp (short for positional parameters). (Remember, the directory in which these example files reside must be in $PATH.) Then enter the echo commands shown in the following screen. Enter the command lines so that running the cat command on your completed file will produce the following output:

   $ cat pp
   echo  The first positional parameter is: $1
   echo  The second positional parameter is: $2
   echo  The third positional parameter is: $3
   echo  The fourth positional parameter is: $4
If you execute this shell program with the arguments one, two, three, and four, you will obtain the following results (but first you must make the shell program pp executable using the chmod command):
   $ chmod u+x pp
   $ pp one two three four
   The first positional parameter is: one
   The second positional parameter is: two
   The third positional parameter is: three
   The fourth positional parameter is: four
Another example of a shell program is bbday, which mails a greeting to the login entered in the command line. The bbday program contains one line:
   banner happy birthday | mail $1
Try sending yourself a birthday greeting. If your login name is sue, your command line will be:
   bbday sue
The who command lists all users currently logged in on the system. How can you make a simple shell program called whoson, that will tell you if the owner of a particular login is currently working on the system?

Type the following command line into a file called whoson:

   who | grep $1
The who command lists all current system users, and grep searches that output for a line with the string contained as a value in the positional parameter $1.

Now try using your login as the argument for the new program whoson. For example, suppose your login is sue. When you issue the whoson command, the shell program substitutes sue for the parameter $1 in your program and executes as if it were:

   who | grep sue
The output appears on your screen as follows:
   $ whoson sue
   sue   tty26      Jan 24 13:35
If the owner of the specified login is not currently working on the system, grep fails and the whoson prints no output.

The shell allows a command line to contain at least 128 arguments; however, a shell program is restricted to referencing only nine positional parameters, $1 through $9, at a given time. You can work around this restriction by using the shift command. See sh(1) for details. The special parameter ``$*'' (described in the next section) can also be used to access the values of all command line arguments.

Next topic: Special parameters
Previous topic: Variables

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004