Parameter Expansion

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents
Appendix E.  Korn Shell Man Page


A parameter is a variable, one or more digits, or any of the characters *, @, #, ?, -, $, and !\. A variable is denoted by a vname. To create a variable whose vname contains a ., a variable whose vname consists of everything before the last . must already exist. A variable has a value and zero or more attributes. Variables can be assigned values and attributes by using the typeset special built-in command. The attributes supported by the shell are described later with the typeset special built-in command. Exported variables pass values and attributes to the environment.

The shell supports both indexed and associative arrays. An element of an array variable is referenced by a subscript. A subscript for an indexed array is denoted by an arithmeticexpression (see Arithmetic Evaluation later in this appendix) between a [and a ]. To assign values to an indexed array, use set -A vname value. . . . The value of all subscripts must be in the range of 0 through 4095. Indexed arrays need not be declared. Any reference to a variable with a valid subscript is legal and an array will be created if necessary.

An associative array is created with the -A option to typeset. A subscript for an associative array is denoted by a string enclosed between [ and ].

Referencing any array without a subscript is equivalent to referencing the array with subscript 0.

The value of a variable may be assigned by writing:


vname=value [ vname=value ] . . .
or
vname[subscript]=value [ vname[subscript]=value ] . . .

Note that no space is allowed before or after the =.

A nameref is a variable that is a reference to another variable. A nameref is created with the -n attribute of typeset. The value of the variable at the time of the typeset command becomes the variable that will be referenced whenever the nameref variable is used. The name of a nameref cannot contain a .. When a variable or function name contains a ., and the portion of the name up to the first . matches the name of a nameref, the variable referred to is obtained by replacing the nameref portion with the name of the variable referenced by the nameref. A nameref provides a convenient way to refer to the variable inside a function whose name is passed as an argument to a function. For example, if the name of a variable is passed as the first argument to a function, the command

typeset -n var=$1

inside the function causes references and assignments to var to be references and assignments to the variable whose name has been passed to the function.

If either of the floating point attributes, -E, or -F, or the integer attribute, -i, is set for vname, then the value is subject to arithmetic evaluation as described below.

Positional parameters, parameters denoted by a number, may be assigned values with the set special built-in command.

Parameter $0 is set from argument zero when the shell is invoked.

The character $ is used to introduce substitutable parameters.

${parameter}

The shell reads all the characters from ${ to the matching } as part of the same word even if it contains braces or metacharacters. The value, if any, of the parameter is substituted. The braces are required when parameter is followed by a letter, digit, or underscore that is not to be interpreted as part of its name, when the variable name contains a ., or when a variable is subscripted. If parameter is one or more digits then it is a positional parameter. A positional parameter of more than one digit must be enclosed in braces. If parameter is * or @, then all the positional parameters, starting with $1, are substituted (separated by a field separator character). If an array vname with subscript * or @ is used, then the value for each of the elements is substituted, separated by the first character of the value of IFS.

${#parameter}

If parameter is * or @, the number of positional parameters is substituted. Otherwise, the length of the value of the parameter is substituted.

${#vname[*]}

${#vname[@]}

The number of elements in the array vname is substituted.

${!vname}

Expands to the name of the variable referred to by vname. This will be vname except when vname is a name reference.

${!vname [subscript]}

Expands to name of the subscript unless subscript is * or @. When subscript is *, the list of array subscripts for vname is generated. For a variable that is not an array, the value is 0 if the variable is set. Otherwise it is null. When subscript is @, same as above, except that when used in double quotes, each array subscript yields a separate argument.

${!prefix *}

Expands to the names of the variables whose names begin with prefix.

${parameter:-word}

If parameter is set and is non-null then substitute its value; otherwise substitute word.

${parameter:=word}

If parameter is not set or is null then set it to word; the value of the parameter is then substituted. Positional parameters may not be assigned to in this way.

${parameter:?word}

If parameter is set and is non-null then substitute its value; otherwise, print word and exit from the shell (if not interactive). If word is omitted then a standard message is printed.

${parameter:+word}

If parameter is set and is non-null then substitute word; otherwise substitute nothing.

${parameter:offset:length}

${parameter:offset}

Expands to the portion of the value of parameter starting at the character (counting from 0) determined by expanding offset as an arithmetic expression and consisting of the number of characters determined by the arithmetic expression defined by length. In the second form, the remainder of the value is used. If parameter is * or @, or is an array name indexed by * or @, then offset and length refer to the array index and number of elements respectively.

${parameter #pattern}

${parameter ##pattern}

If the shell pattern matches the beginning of the value of parameter, then the value of this expansion is the value of the parameter with the matched portion deleted; otherwise the value of this parameter is substituted. In the first form the smallest matching pattern is deleted and in the second form the largest matching pattern is deleted. When parameter is @, *, or an array variable with subscript @ or *, the substring operation is applied to each element in turn.

${parameter %pattern}

${parameter %%pattern}

If the shell pattern matches the end of the value of parameter, then the value of this expansion is the value of the parameter with the matched part deleted; otherwise substitute the value of parameter. In the first form the smallest matching pattern is deleted and in the second form the largest matching pattern is deleted. When parameter is @, *, or an array variable with subscript @ or *, the substring operation is applied to each element in turn.

${parameter /pattern /string}

${parameter //pattern /string}

${parameter /#pattern /string}

${parameter /%pattern /string}

Expands parameter and replaces the longest match of pattern with the given string. Each occurrence of \n in string is replaced by the portion of parameter that matches the n -th sub-pattern. In the first form, only the first occurrence of pattern is replaced. In the second form, each match for pattern is replaced by the given string. The third form restricts the pattern match to the beginning of the string while the fourth form restricts the pattern match to the end of the string. When string is null, the pattern will be deleted and the / in front of string may be omitted. When parameter is @, *, or an array variable with subscript @ or *, the substitution operation is applied to each element in turn.

In the above, word is not evaluated unless it is to be used as the substituted string, so that, in the following example, pwd is executed only if d is not set or is null:

print ${d:- $(pwd)}

If the colon (:) is omitted from the above expressions, then the shell only checks whether parameter is set or not.

The following parameters are automatically set by the shell:

#

The number of positional parameters in decimal.

-

Options supplied to the shell on invocation or by the set command.

?

The decimal value returned by the last executed command.

$

The process number of this shell.

_

Initially, the value of _ is an absolute pathname of the shell or script being executed as passed in the environment. Subsequently it is assigned the last argument of the previous command. This parameter is not set for commands which are asynchronous. This parameter is also used to hold the name of the matching MAIL file when checking for mail.

!

The process number of the last background command invoked.

.sh.edchar

This variable contains the value of the keyboard character (or sequence of characters if the first character is an ESC, ascii 033) that has been entered when processing a KEYBD trap (see KeyBindings below). If the value is changed as part of the trap action, then the new value replaces the key (or key sequence) that caused the trap.

.sh.edcol

The character position of the cursor at the time of the most recent KEYBD trap.

.sh.edmode

The value is set to ESC when processing a KEYBD trap while in vi insert mode. (See Vi Editing Mode later in this appendix.) Otherwise,.sh.edmode is null when processing a KEYBD trap.

.sh.edtext

The characters in the input buffer at the time of the most recent KEYBD trap. The value is null when not processing a KEYBD trap.

.sh.name

Set to the name of the variable at the time that a discipline function is invoked.

.sh.subscript

Set to the name subscript of the variable at the time that a discipline function is invoked.

.sh.value

Set to the value of the variable at the time that the set discipline function is invoked.

.sh.version

Set to a value that identifies the version of this shell.

LINENO

The current line number within the script or function being executed.

OLDPWD

The previous working directory set by the cd command.

OPTARG

The value of the last option argument processed by the getopts built-in command.

OPTIND

The index of the last option argument processed by the getopts built-in command.

PPID

The process number of the parent of the shell.

PWD

The present working directory set by the cd command.

RANDOM

Each time this variable is referenced, a random integer, uniformly distributed between 0 and 32767, is generated. The sequence of random numbers can be initialized by assigning a numeric value to RANDOM.

REPLY

This variable is set by the select statement and by the read built-in command when no arguments are supplied.

SECONDS

Each time this variable is referenced, the number of seconds since shell invocation is returned. If this variable is assigned a value, then the value returned upon reference will be the value that was assigned plus the number of seconds since the assignment.

The following variables are used by the shell:

CDPATH

The search path for the cd command.

COLUMNS

If this variable is set, the value is used to define the width of the edit window for the shell edit modes and for printing select lists.

EDITOR

If the value of this variable ends in emacs, gmacs, or vi and the VISUAL variable is not set, then the corresponding option will be turned on. See the set command later in this appendix.

ENV

If this variable is set, then parameter expansion, command substitution, and arithmetic substitution are performed on the value to generate the pathname of the script that will be executed when the shell is invoked (see Invocation section later in this appendix). This file is typically used for alias and function definitions.

FCEDIT

Obsolete name for the default editor name for the hist command. FCEDIT is not used when HISTEDIT is set.

FIGNORE

A pattern that defines the set of filenames that will be ignored when performing filename matching.

FPATH

The search path for function definitions. This path is searched for a file with the same name as the function or command when a function with the -u attribute is referenced and when a command is not found. If an executable file with the name of that command is found, then it is read and executed in the current environment.

HISTCMD

Number of the current command in the history file.

HISTEDIT

Name for the default editor name for the hist command.

HISTFILE

If this variable is set when the shell is invoked, then the value is the pathname of the file that will be used to store the command history (see Command Re-entry later in this appendix).

HISTSIZE

If this variable is set when the shell is invoked, then the number of previously entered commands that are accessible by this shell will be greater than or equal to this number. The default is 128.

HOME

The default argument (home directory) for the cd command.

IFS

Internal field separators, normally space, tab, and new-line that are used to separate the results of command substitution or parameter expansion and to separate fields with the built-in command read. The first character of the IFS variable is used to separate arguments for the "$*" substitution (see Quoting later in this appendix). Each single occurrence of an IFS character in the string to be split, that is not in the isspace character class, and any adjacent characters in IFS that are in the isspace character class, delimit a field. One or more characters in IFS that belong to the isspace character class, delimit a field. In addition, if the same isspace character appears consecutively inside IFS, this character is treated as if it were not in the isspace class, so that if IFS consists of two tab characters, then two adjacent tab characters delimit a null field.

LANG

This variable determines the locale category for any category not specifically selected with a variable starting with LC_ or LANG.

LC_ALL

This variable overrides the value of the LANG variable and any other LC_ variable.

LC_COLLATE

This variable determines the locale category for character collation information.

LC_CTYPE

This variable determines the locale category for character handling functions. It determines the character classes for pattern matching (see File Name Generation later in this appendix).

LC_NUMERIC

This variable determines the locale category for the decimal point character.

LINES

If this variable is set, the value is used to determine the column length for printing select lists. Select lists will print vertically until about two-thirds of LINES lines are filled.

MAIL

If this variable is set to the name of a mail file and the MAILPATH variable is not set, then the shell informs the user of arrival of mail in the specified file.

MAILCHECK

This variable specifies how often (in seconds) the shell will check for changes in the modification time of any of the files specified by the MAILPATH or MAIL variables. The default value is 600 seconds. When the time has elapsed the shell will check before issuing the next prompt.

MAILPATH

A colon (:) separated list of file names. If this variable is set, then the shell informs the user of any modifications to the specified files that have occurred within the last MAILCHECK seconds. Each file name can be followed by a ? and a message that will be printed. The message will undergo parameter expansion, command substitution, and arithmetic substitution with the variable $_ defined as the name of the file that has changed. The default message is "you have mailin $_".

PATH

The search path for commands (see Execution later in this appendix). The user may not change PATH if executing under rsh (except in .profile).

PS1

The value of this variable is expanded for parameter expansion, command substitution, and arithmetic substitution to define the primary prompt string which by default is "$". The character ! in the primary prompt string is replaced by the command number (see Command Re-entry later in this appendix). Two successive occurrences of ! will produce a single ! when the prompt string is printed.

PS2

Secondary prompt string, by default ">".

PS3

Selection prompt string used within a select loop, by default "#?".

PS4

The value of this variable is expanded for parameter evaluation, command substitution, and arithmetic substitution and precedes each line of an execution trace. By default, PS4 is "+". In addition when PS4 is unset, the execution trace prompt is also "+".

SHELL

The pathname of the shell is kept in the environment. At invocation, if the basename of this variable is rsh, rksh, or krsh, then the shell becomes restricted.

TMOUT

If set to a value greater than zero, TMOUT will be the default timeout value for the read built-in command. The select compound command terminates after TMOUT seconds when input is from a terminal. Otherwise, the shell will terminate if a line is not entered within the prescribed number of seconds while reading from a terminal. (Note that the shell can be compiled with a maximum bound for this value which cannot be exceeded.)

VISUAL

If the value of this variable ends in emacs, gmacs, or vi then the corresponding option (see set command later in this appendix) will be turned on. The value of VISUAL overrides the value of EDITOR.

The shell gives default values to PATH, PS1, PS2, PS3, PS4, MAILCHECK, FCEDIT, TMOUT and IFS, while HOME, SHELL, ENV, and MAIL are not set at all by the shell (although HOME is set by login(1)). On some systems MAIL and SHELL are also set by login(1).


       
    Top
     



    Korn Shell. Unix and Linux Programming Manual, Third Edition
    Korn Shell. Unix and Linux Programming Manual, Third Edition
    ISBN: N/A
    EAN: N/A
    Year: 2000
    Pages: 177

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net