Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
Shells: User's Guide: HP 9000 Computers > Chapter 19 Substitution Capabilities

Parameter Substitution

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Index

A parameter is an entity that holds a value.

The two types of parameters discussed in this section are:

  • named parameters, such as HOME, IFS.

  • positional parameters, such as 1, 2, 3.

Each of these is described in detail in the subsequent sections.

Parameter substitution is the process the shell does to a command line when it replaces the parameters with their value, for example, changing:

echo $HOME

to

echo /users/mary

Then, after parameter substitution, the line is executed. The echo command never see the $HOME, it is the shell that does the substitution.

The value of a named parameter can be accessed by preceding the name with a dollar sign $.

$parameter

where the $ specifies substitution of the value of the parameter. For example:

$ x=1
$ echo $x
1

This is a simple example of a parameter which is named, (x), that is assigned a value (1).

Setting and Using Keyword/Named Parameters

At the risk of sounding circular, a named parameter is a parameter with a name. The name may be any word consisting only of alphanumeric characters and the _ (underscore), and beginning with an underscore or alphabetic character. new_prog1 is a named parameter. The value of a named parameter can be set using the syntax:

name= value

For example:

$ x=1

sets the name to x with a value of 1.

Attributes of a parameter may be set with the typeset command. The typeset command has many options or attributes (such as read-only, integer, left justify) it can assign to each name. See Chapter 23 “Advanced Concepts and Commands” for details on these.

Setting and Using Positional Parameters

Positional parameters are passed to a command or shell script or set with the set command. The positional parameters follow the script or command name on the command line. Then every item on the line following the command or script name, separated by a whitespace, is given a positional parameter name 0, 1, 2, 3, and so forth. These correspond directly to the items on the command line; 0 is the first item (script name), 1 is the second item. This assignment process continues for the rest of the parameters on the line.

For example, the following function uses the while construct to shift through and print the parameters and their position on the command line. The shift command might also help you accomplish this task (see shift in Chapter 24 “Command Reference”).

$ function print_args
> {
> typeset -i x=0 # set x=0; declare x integer
>
> while [ x -le $# ] # -le is "less than or equal"
> do
> echo "positional parameter $x is: $(eval echo \$$x)"
> let x=x+1
> done
> }
$ print_args A B C
positional parameter 0 is: print_arg
positional parameter 1 is: A
positional parameter 2 is: B
positional parameter 3 is: C
$

In the previous example the current shell is the Korn Shell. The POSIX Shell behaves differently compared to Korn Shell with respect to the $0 positional parameter. The same example in the POSIX Shell prints

-sh

for the $0 parameter (if the login shell is POSIX), or

/bin/posix/sh

for the $0 parameter (if the login shell is not POSIX). This happens because within the POSIX Shell definition, the positional parameter $0 is either the shell file name (if the shell is interactive) or is the script name.

The set command may be used to set the positional parameters for the shell.

$ set  first second third
$ echo $1 $2 $3
first second third
$

This sets positional parameter 1 to first, positional parameter 2 to second, and positional parameter 3 to third.

Parameter Substitution Conventions

This section covers special conventions used during parameter substitution:

${parameter}

Braces are required when parameter is followed by a letter, digit, or underscore that you do not want be interpreted as part of the parameter's name, for example, ls /tmp/${file}_text. Braces are also required for enclosing multidigit positional parameters, for example, ${17}.

${parameter:-word}

If parameter is set and nonnull, its value is substituted; otherwise word is substituted. For example:

$ unset x
$ echo ${x:-"x is unset"}
x is unset
$

${parameter:=word}

If parameter is not set or null, the value is set to word's value.

${parameter:?word}

If parameter is set and nonnull, substitute its value; otherwise, print word and exit from the shell. When word is omitted a standard message is printed.

${parameter:+word}

If parameter is set and nonnull, substitute word; otherwise substitute nothing.

For the above four substitutions with the colon (:), if the colon is omitted, the check for a null value is omitted.

$ x=""
$ echo ${x:- "x is null"}
x is null
$ echo ${x- "x is null"}

$

${parameter#pattern}
${parameter##pattern}

If the Shell pattern matches the beginning of the value of the parameter, substitute the value of the parameter with the matching pattern removed; otherwise substitute the value of this parameter. In the first form, the smallest matching pattern is deleted and, in the latter form, the largest matching pattern is deleted.

${parameter%pattern}
${parameter%%pattern}

If the Shell pattern matches the end of the value of parameter, then the value of parameter with the matched part deleted is substituted; otherwise substitute the value of parameter. In the first form, the smallest matching pattern is deleted and, in the latter form, the largest matching pattern is deleted.

These examples show how some of the parameter substitution techniques work. To get the base name of a path:

$ prog=/users/mary/prog
$ basename=${prog##*/}
$ echo $basename
prog
$

As you can see in this example, the pattern /users/mary is matched and then removed from the string.

To get the corresponding path prefix you could do the following:

$ prog=/users/mary/prog
$ prefix=${prog%/*}
$ echo $prefix
/users/mary
$

In this case, the smallest substring, starting on the right, of /users/mary/prog that begins with a /, that is, that matches /*, is removed.

Special Parameters

$@ or $*

If the parameter is * or @, then all the positional parameters, starting with $1, are substituted. For example, the script sc uses $@ to echo all its parameters at one time:

$ sc first second third
echo $@
first second third

${array[*]}

If the parameter describes an array with elements * or @, then the value for each of the elements is substituted. For example: echo ${array[*]} (An array is a collection of contiguous elements that can be accessed by a subscript. A subscript can also be metacharacters such as *, $@, #, and $*. For more details on arrays see Chapter 21 “Basic Shell Programming”.)

${#parameter}

The # specifies that the number of the characters in the parameter is to be substituted. If parameter is * , the number of positional parameters on the command line is substituted.

${#array[*]}

If the parameter is an array name with elements * or @, and the array name is preceded by #, the number of elements in the array is substituted.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-1991 Hewlett-Packard Development Company, L.P.