DistributedNetworks DistributedNetworks

Shell Variables   «Prev  Next»
Lesson 2Accessing command-line arguments
Objective Use information from the command line as variables in a script.

Accessing Command-line Arguments in Unix

UNIX commands rely on parameters to alter their behavior. For example, the ls command can use the -a parameter to list hidden files or the -l parameter to list file details (a “l”ong listing). Shell scripts can also use information from the command line to determine how to proceed. This information is called the command line arguments[1] or script parameters. A user normally enters these parameters when launching the script. A program that starts the script can also add parameters to the command that launches the script.
For example, suppose you have a script named convert that changes a database file from one format to another. The script could be written to use a specific filename, but it is much more useful to be able to start the script by following the script name with whatever particular file you would like converted at that moment.
So, by entering this:

% convert june99.dat

You would have the script operate on the database file named june99.dat. This is done by accessing command-line arguments as variables in your script.

Positional Variables

These variables are sometimes called positional variables, because you access them by referring to their position on the command line. In the above example, the script name, convert, is in position 0. The filename june99.dat is in position 1. Each position on the command line is separated by one or more spaces or tab characters.
To access these variables in your script, you simply refer to $0 and $1, respectively. In the convert script this might take a form like this:
Positional variable: Variables defined by the shell whenever a program or shell script is launched that contain the command line arguments that were included when the program was started.

echo The following file will be converted: $1

This displays the name of the file from the command line where $1 is shown, as a message to the user running the script.
If your script requires a certain number of parameters to operate, you can test to determine how many parameters are included on the command line. You can also check the value of each parameter to see that it is appropriate for your scripts functionality.
If you try to use a positional variable that does not exist, that variable will be empty. For example, if the command line is:

% convert may99.dat june99.dat july99.dat

Only the positional variables $0, $1, $2, and $3 will have valid values. $4, and higher will not be defined. A script must be designed to accept parameters in certain ways and report an error if the positional variables indicate that the correct information is not passed to the script.
When you use a regular expression such as *dat on the command line, the shell substitutes all of the matching filenames before starting the script. For example, if three files match the pattern *dat, the command line arguments might be one.dat, two.dat, three.dat, which you could access in a script as $1, $2, and $3. A looping structure, which you will learn about later in the course, is used to handle these situations.
The following Slide Show shows how a series of positional variables are accessed in a script.

Using Positional Variables
Positional variables are only one type of predefined variable. In the next lesson you learn about others.

[1]Command line argument: Information included on the command line (as supplementary data) in a command interpreter when a program is launched.