Table of Contents

COMMAND EXECUTION

If a command name contains no slashes, the shell attempts to locate it. If there exists a shell function by that name, the function is invoked as described below in FUNCTIONS. If there exists a shell builtin by that name, the builtin is invoked.

Otherwise, the shell searches each element of path for a directory containing an executable file by that name. If the search is unsuccessful, the shell prints an error message and returns a nonzero exit status.

If execution fails because the file is not in executable format, and the file is not a directory, it is assumed to be a shell script. /bin/sh is spawned to execute it. If the program is a file beginning with #!, the remainder of the first line specifies an interpreter for the program. The shell will execute the specified interpreter on operating systems that do not handle this executable format in the kernel.

FUNCTIONS

The function reserved word is used to define shell functions. Shell functions are read in 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 and share all files and present working directory with the caller. A trap on EXIT set inside a function is executed after the function completes in the environment of the caller.

The return builtin is used to return from function calls.

Function identifiers can be listed with the functions builtin. Functions can be undefined with the unfunction builtin.

The following functions, if defined, have special meaning to the shell:

chpwd
Executed whenever the current working directory is changed.

precmd
Executed before each prompt.

periodic
If the parameter PERIOD is set, this function is executed every PERIOD seconds, just before a prompt.

TRAPxxx
If defined and non-null, this function will be executed whenever the shell catches a signal SIGxxx, where xxx is a signal name as specified for the kill builtin (see below). The signal number will be passed as the first parameter to the function. In addition, TRAPZERR is executed whenever a command has a non-zero exit status, TRAPDEBUG is executed after each command, and TRAPEXIT is executed when the shell exits, or when the current function exits if defined inside a function. If a function of this form is defined and null, the shell and processes spawned by it will ignore SIGxxx.

JOBS

If the MONITOR option is set, 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 which looks like:

[1] 1234

indicating that the job which was started asynchronously was job number 1 and had one (top-level) process, whose process id was 1234.

If you are running a job and wish to do something else you may hit the key ^Z (control-Z) which sends a TSTP signal to the current job. The shell will then normally indicate that the job has been `suspended', and print another prompt. You can then manipulate the state of this job, putting it in the background with the bg command, or run some other commands and then eventually bring the job back into the foreground with the foreground command fg. A ^Z takes effect immediately and is like an interrupt in that pending output and unread input are discarded when it is typed.

A job being run in the background will suspend if it tries to read from the terminal. Background jobs are normally allowed to produce output, but this can be disabled by giving the command stty tostop. If you set this tty option, then background jobs will suspend when they try to produce output like they do when they try to read input.

There are several ways to refer to jobs in the shell. A job can be referred to by the process id of any process of 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 whenever a process changes state. It normally informs you whenever a job becomes blocked so that no further progress is possible. If notify is not set, it waits until just before it prints a prompt before it informs you.

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

When you try to leave the shell while jobs are running or suspended, you will be warned that `You have suspended (running) jobs.' You may use the jobs command to see what they are. If you do this or immediately try to exit again, the shell will not warn you a second time; the suspended jobs will be terminated, and the running jobs will be sent a SIGHUP signal. To avoid having the shell terminate the running jobs, either use the nohup(1) command or the disown builtin (see below).


Mark D. Borges