HP-UX Reference (11i v3 07/02) - 1 User Commands N-Z (vol 2)

s
sh-posix(1) sh-posix(1)
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 example,
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. Func-
tions 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 functions
it calls.
The return special command is used to return from function calls. Errors within functions return control
to the caller.
Function identifiers can be listed with the
+f option of the typeset special command. Function
identifiers and the associated text of the functions can be listed with the
-f
option. Functions can be
undefined with the
-f option of the unset special command.
Ordinarily, functions are unset when the shell executes a shell script. The -xf option of the
typeset
command allows a function to be exported to scripts that are executed without reinvoking the shell. Func-
tions that must be defined across separate invocations of the shell should be placed in the ENV file.
Jobs
If the monitor option of the set command is turned on, an interactive shell associates a job with each
pipeline. It keeps a table of current jobs, printed by the jobs command, and assigns them small integer
numbers. When a job is started asynchronously with &, the shell prints a line that looks like:
[1] 1234
indicating that job number 1 was started asynchronously and had one (top-level) process whose process ID
was 1234.
If you are running a job and wish to do something else, you can type the suspend character (the susp char-
acter defined with stty; see stty(1)) to send a SIGSTOP signal to the current job. The shell then indi-
cates that the job has been Stopped, and prints another prompt. Then you can manipulate the state of
this job by putting it in the background with the bg command, running other commands, and eventually
returning the job to the foreground with the fg command. A suspend takes effect immediately and resem-
bles an interrupt, since pending output and unread input are discarded when the suspend is entered.
HP-UX 11i Version 3: February 2007 19 Hewlett-Packard Company 261