Assigning Positional Parameter Values Using the set Statement in Shell Scripts

Although you cannot use any positional parameter name on the left side of an assignment statement, you can still assign values to the positional parameters if you use the set statement:

set value1 value2 ... valueN

With the preceding syntax, $1 has value value1, $2 has value value2, and so on. The value of the positional parameter $0 is the name of the script. Use the set statement to create a parameter list using the statement or variable substitution.

set $(cal)
set $var1

To sort the positional parameters lexicographically, use the set -s statement. If you sort the list of values in lexical order, the first value on the list is assigned to $1, the second to $2, and so forth.

set -s

The statement “set –” unsets all the positional parameters. Thus, $1, $2, and so on, have no values. The value of $0, is still the script name.

set -

The “set -s” and “set –”" statements work on the positional parameters regardless of how the positional parameter names were assigned their values. These statements work whether values were assigned from command-line arguments or by using the set statement.

A statement or variable substitutions, combined with the set statement, is useful. For example, to find out how many days are in the current month, use the cal statement. The cal statement outputs a value for each day in the month plus an additional nine values: the month, the year, and the day-of-week values.

Example of Using the set Statement

The following pospar2.ksh example script sets the values of the positional parameters using the set statement. The values a, b, and c are passed into the script and assigned to positional parameters $1, $2, and $3 respectively. The values of the positional parameters are displayed using the print statement and then given new values with the set statement.

The textline variable is assigned a value, which is a string consisting of several words. The statement set $textline assigns new values to the positional parameters. Because $textline is replaced by its value (a string of several words), the words become the positional parameter values. The script then prints the positional parameter values using the print $* statement. Then the script prints the values of the $1 and $4 positional parameters.

Next, the positional parameters are sorted by the statement set -s, and then print $* is used to print the sorted list. Lastly, the “set –” statement unsets the positional parameters so that when the print $0 $* statement is executed, only the name of the script (which is held in $0) is printed.

$ cat pospara2.ksh
#!/bin/ksh

# Script name: pospara2.ksh

print "Executing script $0 \n"
print "$1 $2 $3"

set uno duo tres
print "One two three in Latin is:"
print "$1"
print "$2"
print "$3 \n"

textline="name phone address birthdate salary"
set $textline
print "$*"
print 'At this time $1 =' $1 'and $4 =' $4 "\n"

set -s
print "$* \n"

set -
print "$0 $*"
$ ./pospara2.ksh a b c
Executing script ./pospara2.ksh

a b c
One two three in Latin is:
uno
duo
tres

name phone address birthdate salary
At this time $1 = name and $4 = birthdate

address birthdate name phone salary
./pospara2.ksh