k

ksh(1)

ksh(1)

Redirection order is signi®cant because the shell evaluates redirections referencing ®le descriptors in terms of the currently open ®le associated with the speci®ed ®le descriptor at the time of evaluation. For exam- ple:

... 1>fname 2>&1

®rst assigns ®le descriptor 1 (standard output) to ®le fname , then assigns ®le descriptor 2 (standard error) to the ®le assigned to ®le descriptor 1; i.e., fname. On the other hand, if the order of redirection is reversed as follows:

... 2>&1 1>fname

®le descriptor 2 is assigned to the current standard output (user terminal unless a different assignment is inherited). File descriptor 1 is then reassigned to ®le fname without changing the assignment of ®le descriptor 2.

The input and output of a co-processcan be moved to a numbered ®le descriptor allowing other commands to write to them and read from them using the above redirection operators. If the input of the current co- process is moved to a numbered ®le descriptor, another co-processcan be started.

If a command is followed by & and job control is inactive, the default standard input for the command is the empty ®le /dev/null. Otherwise, the environment for the execution of a command contains the ®le descriptors of the invoking shell as modi®ed by input/output speci®cations.

Environment

The environment (see environ(5)) is a list of name-value pairs passed to an executed program much like a normal argument list. The names must be identi®ers and the values are character strings. The shell interacts with the environment in several ways. When invoked, the shell scans the environment and creates a parameter for each name found, gives it the corresponding value, and marks it export. Executed commands inherit the environment. If the user modi®es the values of these parameters or creates new ones by using the export or typeset -xcommands, the values become part of the environment. The environment seen by any executed command is thus composed of any name-value pairs originally inherited by the shell whose values can be modi®ed by the current shell, plus any additions which must be noted in export or typeset -xcommands.

The environment for any simple-commandor function can be augmented by pre®xing it with one or more parameter assignments. A parameter assignment argument takes the form identi®er=value. For example,

TERM=450 cmd args

and

(export TERM; TERM=450; cmd args)

are equivalent (as far as the above execution of cmd is concerned except for special commands listed below that are preceded by a percent sign).

If the -koption is set, all parameter assignment arguments are placed in the environment, even if they occur after the command name. The following echo statement prints a=b c. After the -koption is set, the second echo statement prints only c:

echo a=b c set -k echo a=b c

This feature is intended for use with scripts written for early versions of the shell, and its use in new scripts is strongly discouraged. It is likely to disappear someday.

Functions

The function keyword (described in the Commands section above) is used to de®ne shell functions. Shell functions are read and stored internally. Alias names are resolved when the function is read. Functions are executed like commands, with the arguments passed as positional parameters (see Execution below).

Functions execute in the same process as the caller except that command substitution of a function creates a new process. Functions share all ®les and present working directory with the caller. Traps caught by the caller are reset to their default action inside the function. If a function does not catch or speci®cally ignore a trap condition, the function terminates and the condition is passed on to the caller. A trap on EXIT set inside a function is executed after the function completes in the environment of the caller. Ordinarily, variables are shared between the calling program and the function. However, the typeset special command

Section 1404

− 9 −

HP-UX Release 11i: December 2000