ksh(1)

ksh(1)

used within a function de®nes local variables whose scope includes the current function and all functions it calls.

The special command return is used to return from function calls. Errors within functions return control to the caller.

Function identi®ers can be listed with the +f option of the typeset special command. Function identi®ers and the associated text of the functions can be listed with the -foption. Functions can be unde®ned with the -foption of the unset special command.

Ordinarily, functions are unset when the shell executes a shell script. The -xfoption of the typeset command allows a function to be exported to scripts that are executed without reinvoking the shell. Functions that must be de®ned across separate invocations of the shell should be placed in the ENV ®le.

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 resembling:

[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 want to do something else, type the suspend character (usually ÃZ (Ctrl-Z)) to send a STOP signal to the current job. The shell then indicates that the job has been `Stopped', and prints another prompt. The state of this job can be manipulated by using the bg command to put it in the back- ground, running other commands (while it is stopped or running in the background), and eventually restarting or returning the job to the foreground by using the fg command. A ÃZ takes effect immediately and resembles an interrupt, since pending output and unread input are discarded when ÃZ is typed.

A job run in the background stops if it tries to read from the terminal. Background jobs normally are allowed to produce output, but can be disabled by giving the stty tostop command. If the user sets this tty option, background jobs stop when trying to produce output.

There are several ways to refer to jobs in the shell. A job can be referred to by the process ID of any process in the job or by one of the following:

%number

The job with the given number.

%string

Any job whose command line begins with string.

%?string

Any job whose command line contains string.

%%Current job.

%+

Equivalent to %%.

%-

Previous job.

The shell learns immediately when a process changes state. It informs the user when a job is blocked and prevented from further progress, but only just before it prints a prompt.

When the monitor mode is on, each background job that completes triggers any trap set for CHLD.

Attempting to leave the shell while jobs are running or stopped produces the warning, You have stopped (running) jobs. Use the jobs command to identify them. An immediate attempt to exit again terminates the stopped jobs; the shell does not produce a warning the second time.

Signals

The INT and QUIT signals for an invoked command are ignored if the command is followed by & and the monitor option is off. Otherwise, signals have the values inherited by the shell from its parent, with the exception of signal 11 (but see also the trap command below).

Execution

Substitutions are made each time a command is executed. If the command name matches one of the Special Commands listed below, it is executed within the current shell process. Next, ksh checks the command name to determine whether it matches one of the user-de®ned functions. If it does, ksh saves the positional parameters and then sets them to the arguments of the function call. The positional parameter 0 is set to the function name. When the function completes or issues a return, ksh restores the positional parameter list and executes any trap set on EXIT within the function. The value of a function is the value of the last command executed. A function is executed in the current shell process. If a command name is not a special command or a user-de®ned function, ksh creates a process and attempts to

HP-UX Release 11i: December 2000

− 10 −

Section 1405

k