| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
| 3.7.1 Simple Command Expansion | How Bash expands simple commands before executing them. | |
| 3.7.2 Command Search and Execution | How Bash finds commands and runs them. | |
| 3.7.3 Command Execution Environment | The environment in which Bash executes commands that are not shell builtins. | |
| 3.7.4 Environment | The environment given to a command. | |
| 3.7.5 Exit Status | The status returned by commands and how Bash interprets it. | |
| 3.7.6 Signals | What happens when Bash or a command it runs receives a signal. |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When a simple command is executed, the shell performs the following expansions, assignments, and redirections, from left to right.
If no command name results, the variable assignments affect the current shell environment. Otherwise, the variables are added to the environment of the executed command and do not affect the current shell environment. If any of the assignments attempts to assign a value to a readonly variable, an error occurs, and the command exits with a non-zero status.
If no command name results, redirections are performed, but do not affect the current shell environment. A redirection error causes the command to exit with a non-zero status.
If there is a command name left after expansion, execution proceeds as described below. Otherwise, the command exits. If one of the expansions contained a command substitution, the exit status of the command is the exit status of the last command substitution performed. If there were no command substitutions, the command exits with a status of zero.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
After a command has been split into words, if it results in a simple command and an optional list of arguments, the following actions are taken.
$PATH for a directory containing an executable file
by that name. Bash uses a hash table to remember the full
pathnames of executable files to avoid multiple PATH searches
(see the description of hash in Bourne Shell Builtins).
A full search of the directories in $PATH
is performed only if the command is not found in the hash table.
If the search is unsuccessful, the shell prints an error
message and returns an exit status of 127.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The shell has an execution environment, which consists of the following:
exec builtin
cd, pushd, or
popd, or inherited by the shell at invocation
umask or inherited from
the shell's parent
trap
set
or inherited from the shell's parent in the environment
set
shopt
alias (see section Aliases)
$$, and the value of
$PPID
When a simple command other than a builtin or shell function is to be executed, it is invoked in a separate execution environment that consists of the following. Unless otherwise noted, the values are inherited from the shell.
A command invoked in this separate environment cannot affect the shell's execution environment.
Command substitution, commands grouped with parentheses, and asynchronous commands are invoked in a subshell environment that is a duplicate of the shell environment, except that traps caught by the shell are reset to the values that the shell inherited from its parent at invocation. Builtin commands that are invoked as part of a pipeline are also executed in a subshell environment. Changes made to the subshell environment cannot affect the shell's execution environment.
If a command is followed by a `&' and job control is not active, the default standard input for the command is the empty file `/dev/null'. Otherwise, the invoked command inherits the file descriptors of the calling shell as modified by redirections.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When a program is invoked it is given an array of strings
called the environment.
This is a list of name-value pairs, of the form name=value.
Bash provides several ways to manipulate the environment.
On invocation, the shell scans its own environment and
creates a parameter for each name found, automatically marking
it for export
to child processes. Executed commands inherit the environment.
The export and `declare -x'
commands allow parameters and functions to be added to and
deleted from the environment. If the value of a parameter
in the environment is modified, the new value becomes part
of the environment, replacing the old. The environment
inherited by any executed command consists of the shell's
initial environment, whose values may be modified in the shell,
less any pairs removed by the unset and `export -n'
commands, plus any additions via the export and
`declare -x' commands.
The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.
If the `-k' option is set (see section The Set Builtin), then all parameter assignments are placed in the environment for a command, not just those that precede the command name.
When Bash invokes an external command, the variable `$_' is set to the full path name of the command and passed to that command in its environment.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
For the shell's purposes, a command which exits with a zero exit status has succeeded. A non-zero exit status indicates failure. This seemingly counter-intuitive scheme is used so there is one well-defined way to indicate success and a variety of ways to indicate various failure modes. When a command terminates on a fatal signal whose number is N, Bash uses the value 128+N as the exit status.
If a command is not found, the child process created to execute it returns a status of 127. If a command is found but is not executable, the return status is 126.
If a command fails because of an error during expansion or redirection, the exit status is greater than zero.
The exit status is used by the Bash conditional commands (see section Conditional Constructs) and some of the list constructs (see section Lists of Commands).
All of the Bash builtins return an exit status of zero if they succeed and a non-zero status on failure, so they may be used by the conditional and list constructs. All builtins return an exit status of 2 to indicate incorrect usage.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When Bash is interactive, in the absence of any traps, it ignores
SIGTERM (so that `kill 0' does not kill an interactive shell),
and SIGINT
is caught and handled (so that the wait builtin is interruptible).
When Bash receives a SIGINT, it breaks out of any executing loops.
In all cases, Bash ignores SIGQUIT.
If job control is in effect (see section Job Control), Bash
ignores SIGTTIN, SIGTTOU, and SIGTSTP.
Non-builtin commands started by Bash have signal handlers set to the
values inherited by the shell from its parent.
When job control is not in effect, asynchronous commands
ignore SIGINT and SIGQUIT in addition to these inherited
handlers.
Commands run as a result of
command substitution ignore the keyboard-generated job control signals
SIGTTIN, SIGTTOU, and SIGTSTP.
The shell exits by default upon receipt of a SIGHUP.
Before exiting, an interactive shell resends the SIGHUP to
all jobs, running or stopped.
Stopped jobs are sent SIGCONT to ensure that they receive
the SIGHUP.
To prevent the shell from sending the SIGHUP signal to a
particular job, it should be removed
from the jobs table with the disown
builtin (see section Job Control Builtins) or marked
to not receive SIGHUP using disown -h.
If the huponexit shell option has been set with shopt
(see section Bash Builtin Commands), Bash sends a SIGHUP to all jobs when
an interactive login shell exits.
If Bash is waiting for a command to complete and receives a signal
for which a trap has been set, the trap will not be executed until
the command completes.
When Bash is waiting for an asynchronous
command via the wait builtin, the reception of a signal for
which a trap has been set will cause the wait builtin to return
immediately with an exit status greater than 128, immediately after
which the trap is executed.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] |