| United States-English |
|
|
|
![]() |
Shells: User's Guide: HP 9000 Computers > Chapter 19 Substitution CapabilitiesParameter Substitution |
|
A parameter is an entity that holds a value. The two types of parameters discussed in this section are:
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:
to
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:
This is a simple example of a parameter which is named, (x), that is assigned a value (1). 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:
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. 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”).
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
for the $0 parameter (if the login shell is POSIX), or
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.
This sets positional parameter 1 to first, positional parameter 2 to second, and positional parameter 3 to third. This section covers special conventions used during parameter substitution:
These examples show how some of the parameter substitution techniques work. To get the base name of a path:
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:
In this case, the smallest substring, starting on the right, of /users/mary/prog that begins with a /, that is, that matches /*, is removed.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||