HP-UX Reference (11i v2 04/09) - 1 User Commands N-Z (vol 2)
s
sh-posix(1) sh-posix(1)
Order is significant in redirection. The shell evaluates each redirection in terms of the (file descriptor ,
file) assignment at the time of evaluation. For example:
1>fname 2>&1
first assigns file descriptor 1 to file fname. It then assigns file descriptor 2 to the file assigned to file
descriptor 1 (that is, fname).
If the order of redirection is reversed, as in
2>&1 1>fname
file descriptor 2 is assigned to the file assigned to file descriptor 1 (probably the terminal) and then file
descriptor 1 is assigned to file fname.
By using the redirection operators above, the input and output of a coprocess may be moved to a num-
bered file descriptor, allowing other commands to write to them and read from them. If the input of the
current coprocess is moved to a numbered file descriptor, another coprocess may be started.
If a command is followed by
&
and job control is inactive, the default standard input for the command is
the empty file
/dev/null. Otherwise, the environment for the execution of a command contains the file
descriptors of the invoking shell as modified by input/output specifications.
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 identifiers 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. Exe-
cuted commands inherit the environment. If the user modifies the values of these parameters or creates
new ones by using the
export or typeset -x special commands, 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 may be modified by the current shell, plus any additions
which must be noted in export or typeset -x commands.
The environment for any simple command or function can be augmented by prefixing it with one or more
parameter assignments. A parameter assignment argument takes the form identifier
=value. For exam-
ple, both the following
TERM=450 cmd args
(export TERM; TERM=450; cmd args)
are equivalent (as far as the execution of cmd is concerned, except for the special commands that are pre-
ceded by a percent sign (%).
If the
-k option 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 -k
option is set,
the second echo statement prints only
c:
echo a=b c → a=b c
set -k
echo a=b c → 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 command (described in the Compound Commands subsection) defines 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 the
Execution subsection.)
Functions execute in the same process as the caller and share all files and current working directory with
the caller. Traps defined by the caller remain in effect within the function until another
trap command
is executed. Traps set within a function remain in effect after the function returns. Ordinarily, variables
are shared between the calling program and the function. However, the typeset special command can
be used within a function to define local variables whose scope includes the current function and all func-
tions it calls.
The
return special command is used to return from function calls. Errors within functions return con-
trol to the caller.
HP-UX 11i Version 2: September 2004 − 19 − Hewlett-Packard Company Section 1−−863