Parameters

   

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

Table of Contents
Appendix H.  Pdksh Man Page


Parameters are shell variables; they can be assigned values and their values can be accessed using a parameter substitution. A parameter name is either one of the special single punctuation or digit character parameters described later in this appendix, or a letter followed by zero or more letters or digits ('_' counts as a letter). The latter form can be treated as arrays by appending an array index of the form: [expr] where expr is an arithmetic expression. Array indices are currently limited to the range 0 through 1023, inclusive. Parameter substitutions take the form $name, ${name} or ${name[expr]}, where name is a parameter name. If substitution is performed on a parameter (or an array parameter element) that is not set, a null string is substituted unless the nounset option (set -o nounset or set -u) is set, in which case an error occurs. Parameters can be assigned values in a number of ways. First, the shell implicitly sets some parameters like #, PWD, etc.; this is the only way the special single character parameters are set. Second, parameters are imported from the shell's environment at startup. Third, parameters can be assigned values on the command line, for example, 'FOO=bar' sets the parameter FOO to bar; multiple parameter assignments can be given on a single command line and they can be followed by a simple-command, in which case the assignments are in effect only for the duration of the command (such assignments are also exported). Note that both the parameter name and the = must be unquoted for the shell to recognize a parameter assignment. The fourth way of setting a parameter is with the export, readonly and typeset commands; see their descriptions in the Command Execution section later in this appendix. Fifth, for and select loops set parameters as well as the getopts, read and set -A commands. Lastly, parameters can be assigned values using assignment operators inside arithmetic expressions (see Arithmetic Expressions later in this appendix) or using the ${name=value} form of parameter substitution.

Parameters with the export attribute (set using the export or typeset -x commands, or by parameter assignments followed by simple commands) are put in the environment (see environ(5)) of commands run by the shell as name=value pairs. The order in which parameters appear in the environment of a command is unspecified. When the shell starts up, it extracts parameters and their values from its environment and automatically sets the export attribute for those parameters.

Modifiers can be applied to the ${name} form of parameter substitution:

${name:-word}

if name is set and not null, it is substituted, otherwise word is substituted.

${name:+word}

if name is set and not null, word is substituted, otherwise nothing is substituted.

${name:=word}

if name is set and not null, it is substituted, otherwise it is assigned word and the resulting value of name is substituted.

${name:?word}

if name is set and not null, it is substituted, otherwise word is printed on standard error (preceded by name:) and an error occurs (normally causing termination of a shell script, function or .-script). If word is omitted the string 'parameter null or not set' is used instead. In the above modifiers, the : can be omitted, in which case the conditions only depend on name being set (as opposed to set and not null). If word is needed, parameter, command, arithmetic and tilde substitution are performed on it; if word is not needed, it is not evaluated.

The following forms of parameter substitution can also be used:

${#name}

The number of positional parameters if name is *, @ or is not specified, or the length of the string value of parameter name.

${#name[*]}, ${#name[@]}

The number of elements in the array name.

${name#pattern}, ${name##pattern}

If pattern matches the beginning of the value of parameter name, the matched text is deleted from the result of substitution. A single # results in the shortest match, two #'s results in the longest match.

${name%pattern}, ${name%%pattern}

Like ${..#..} substitution, but it deletes from the end of the value.

The following special parameters are implicitly set by the shell and cannot be set directly using assignments:

!

Process id of the last background process started. If no background processes have been started, the parameter is not set.

#

The number of positional parameters (i.e., $1, $2, etc.).

$

The process ID of the shell, or the PID of the original shell if it is a subshell.

-

The concatenation of the current single letter options (see set command below for list of options).

?

The exit status of the last non-asynchronous command executed. If the last command was killed by a signal, $? is set to 128 plus the signal number.

0

The name the shell was invoked with (i.e., argv[0]), or the command-name if it was invoked with the -c option and the command-name was supplied, or the file argument, if it was supplied. If the posix option is not set, $0 is the name of the current function or script.

1 ... 9

The first nine positional parameters that were supplied to the shell, function or .-script. Further positional parameters may be accessed using ${number}.

*

All positional parameters (except parameter 0), i.e., $1 $2 $3.... If used outside of double quotes, parameters are separate words (which are subjected to word splitting); if used within double quotes, parameters are separated by the first character of the IFS parameter (or the empty string if IFS is null).

@

Same as $*, unless it is used inside double quotes, in which case a separate word is generated for each positional parameter - if there are no positional parameters, no word is generated ("$@" can be used to access arguments, verbatim, without loosing null arguments or splitting arguments with spaces).

The following parameters are set and/or used by the shell:

_ (underscore)

When an external command is executed by the shell, this parameter is set in the environment of the new process to the path of the executed command. In interactive use, this parameter is also set in the parent shell to the last word of the previous command. When MAILPATH messages are evaluated, this parameter contains the name of the file that changed (see MAILPATH parameter later in this appendix).

CDPATH

Search path for the cd built-in command. Works the same way as PATH for those directories not beginning with / in cd commands. Note that if CDPATH is set and does not contain . nor an empty path, the current directory is not searched.

COLUMNS

Set to the number of columns on the terminal or window. Currently set to the cols value as reported by stty(1) if that value is non-zero. This parameter is used by the interactive line editing modes, and by select, set -o and kill -l commands to format information in columns.

EDITOR

If the VISUAL parameter is not set, this parameter controls the command line editing mode for interactive shells. See VISUAL parameter later in this appendix for how this works.

ENV

If this parameter is found to be set after any profile files are executed, the expanded value is used as a shell start-up file. It typically contains function and alias definitions.

ERRNO

Integer value of the shell's errno variable - indicates the reason the last system call failed. Not implemented yet.

EXECSHELL

If set, this parameter is assumed to contain the shell that is to be used to execute commands that execve(2) fails to execute and which do not start with a '#! shell' sequence.

FCEDIT

The editor used by the fc command.

FPATH

Like PATH, but used when an undefined function is executed to locate the file defining the function. It is also searched when a command can't be found using PATH. See Functions later in this appendix for more information.

HISTFILE

The name of the file used to store history. When assigned to, history is loaded from the specified file. Also, several invocations of the shell running on the same machine will share history if their HISTFILE parameters all point at the same file. NOTE: if HISTFILE isn't set, no history file is used. This is different from the original Korn shell, which uses $HOME/.sh_history; in future, pdksh may also use a default history file.


       
    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