Bash implements essentially the same grammar, parameter and
variable expansion, redirection, and quoting as the Bourne Shell.
Bash uses the POSIX standard as the specification of
how these features are to be implemented and how they should behave.
There are some
differences between the traditional Bourne shell and Bash; this
section quickly details the differences of significance.
A number of these differences are explained in greater depth in
previous sections.
This section uses the version of sh included in SVR4.2 (the
last version of the historical Bourne shell) as the baseline reference.
sh behavior (see Bash and POSIX).
bind builtin.
complete, compgen, and compopt, to
manipulate it.
PS0, PS1, PS2, and PS4)
(see Controlling the Prompt).
PS0 prompt string variable.
PROMPT_COMMAND array variable before
issuing each primary prompt.
history and fc builtins to manipulate it.
The Bash history list maintains timestamp information and uses the
value of the HISTTIMEFORMAT variable to display it.
csh-like history expansion
(see History Expansion).
$'…' quoting syntax, which expands ANSI-C
backslash-escaped characters in the text between the single quotes
(see ANSI-C Quoting).
$"…" quoting syntax and performs
locale-specific translation of the characters between the double
quotes.
The -D, --dump-strings, and --dump-po-strings
invocation options list the translatable strings found in a script
(see Locale-Specific Translation).
alias and unalias
builtins (see Aliases).
! reserved word to negate the return value of
a pipeline (see Pipelines).
This is very useful when an if statement needs to act only if a
test fails.
The Bash ‘-o pipefail’ option to set will cause a pipeline to
return a failure status if any command fails
(see The Set Builtin).
time reserved word and command timing (see Pipelines).
The display of the timing statistics may be controlled with the
TIMEFORMAT variable.
coproc reserved word
(see Coprocesses).
for (( expr1 ; expr2 ; expr3 ))
arithmetic for command, similar to the C language (see Looping Constructs).
select compound command, which allows the
generation of simple menus (see Conditional Constructs).
[[ compound command, which makes conditional
testing part of the shell grammar (see Conditional Constructs), including
optional regular expression matching.
case and
[[ constructs (see Conditional Constructs).
case statement action list terminators:
‘;&’ and ‘;;&’ (see Conditional Constructs).
(( compound command (see Conditional Constructs),
the let builtin,
and arithmetic expansion (see Shell Arithmetic).
export
command.
$9 using
${num} (see Shell Parameter Expansion).
${#xx}, which returns the length of ${xx},
is supported (see Shell Parameter Expansion).
${var:offset[:length]},
which expands to the substring of var’s value of length
length, beginning at offset, is present
(see Shell Parameter Expansion).
${var/[/]pattern[/replacement]},
which matches pattern and replaces it with replacement in
the value of var, is available (see Shell Parameter Expansion),
with a mechanism to use the matched text in replacement.
${!prefix*} expansion, which expands to
the names of all shell variables whose names begin with prefix,
is available (see Shell Parameter Expansion).
${!word}
(see Shell Parameter Expansion) and implements the nameref
variable attribute for automatic indirect variable expansion.
${var@X}, where ‘X’ specifies the transformation
(see Shell Parameter Expansion).
$() form of command substitution
is implemented (see Command Substitution),
and preferred to the Bourne shell’s `` (which
is also implemented for backwards compatibility).
${ command;} or ${|command;}
(see Command Substitution).
UID, EUID, and GROUPS), the current host
(HOSTTYPE, OSTYPE, MACHTYPE, and HOSTNAME),
and the instance of Bash that is running (BASH,
BASH_VERSION, and BASH_VERSINFO).
See Bash Variables, for details.
RANDOM, SRANDOM, EPOCHSECONDS,
EPOCHREALTIME,
TIMEFORMAT, BASHPID, BASH_XTRACEFD,
GLOBIGNORE, HISTIGNORE, and BASH_VERSION.
See Bash Variables, for a complete list.
GLOBSORT shell variable to control how to sort
the results of filename expansion (see Filename Expansion).
IFS variable to split only the results of expansion,
not all words (see Word Splitting).
This closes a longstanding shell security hole.
extglob
shell option is enabled (see Pattern Matching).
globstar shell option extends filename expansion to recursively
scan directories and subdirectories for matching filenames
(see Pattern Matching).
sh does not separate the two name spaces.
local builtin, and thus users can write useful recursive functions
(see Bash Builtin Commands).
noclobber option is available to avoid overwriting existing
files with output redirection (see The Set Builtin).
The ‘>|’ redirection operator may be used to override noclobber.
sh, all variable assignments
preceding commands are global unless the command is executed from the
file system.
BASH_ARGC, BASH_ARGV, BASH_LINENO,
BASH_SOURCE),
the DEBUG, RETURN, and ERR traps,
‘declare -F’,
and
the caller builtin.
csh-like directory stack, and provides the
pushd, popd, and dirs builtins to manipulate it
(see The Directory Stack).
Bash also makes the directory stack visible as the value of the
DIRSTACK shell variable.
builtin and command builtins (see Bash Builtin Commands).
caller builtin
(see Bash Builtin Commands), which displays the context of
any active subroutine call (a shell function or a script executed with
the . or source builtins).
This supports the Bash debugger.
cd and pwd builtins (see Bourne Shell Builtins)
each take -L and -P options to switch between logical and
physical modes.
command builtin allows selectively skipping shell functions
when performing command lookup (see Bash Builtin Commands).
declare builtin to modify the full set of variable
and function attributes, and to assign values to variables.
disown builtin can remove a job from the internal shell
job table (see Job Control Builtins) or suppress sending
SIGHUP to a job when the shell exits as the result of a
SIGHUP.
enable builtin (see Bash Builtin Commands) can enable or disable
individual builtins
and implements support for dynamically loading
builtin commands from shared objects.
exec builtin takes additional options that allow users
to control the contents of the environment passed to the executed
command, and what the zeroth argument to the command is to be
(see Bourne Shell Builtins).
export -f (see Shell Functions).
export and readonly builtins
(see Bourne Shell Builtins can
take a -f option to act on shell functions, a -p option to
display variables with various attributes set in a format that can be
used as shell input, a -n option to remove various variable
attributes, and ‘name=value’ arguments to set variable attributes
and values simultaneously.
hash builtin allows a name to be associated with
an arbitrary filename, even when that filename cannot be found by
searching the $PATH, using ‘hash -p’
(see Bourne Shell Builtins).
help builtin for quick reference to shell
facilities (see Bash Builtin Commands).
mapfile builtin to quickly read the contents
of a file into an indexed array variable (see Bash Builtin Commands).
printf builtin is available to display formatted output
(see Bash Builtin Commands),
and has additional custom format specifiers and an option to assign
the formatted output directly to a shell variable.
read builtin (see Bash Builtin Commands)
will read a line ending in ‘\’ with
the -r option, and will use the REPLY variable as a
default if no non-option arguments are supplied.
read builtin (see Bash Builtin Commands)
accepts a prompt string with the -p option and will use
Readline to obtain the line when given the -e or -E
options,
with the ability to insert text into the line using the -i
option.
The read builtin also has additional options to control input:
the -s option will turn off echoing of input characters as
they are read, the -t option will allow read to time out
if input does not arrive within a specified number of seconds, the
-n option will allow reading only a specified number of
characters rather than a full line, and the -d option will read
until a particular character rather than newline.
return builtin may be used to abort execution of scripts
executed with the . or source builtins
(see Bourne Shell Builtins).
set
builtin (see The Set Builtin).
shopt builtin, for finer control of shell
optional capabilities (see The Shopt Builtin), and allows these options
to be set and unset at shell invocation (see Invoking Bash).
test builtin (see Bourne Shell Builtins)
is slightly different, as it implements the POSIX algorithm,
which specifies the behavior based on the number of arguments.
trap builtin (see Bourne Shell Builtins) allows a
DEBUG pseudo-signal specification, similar to EXIT.
Commands specified with a DEBUG trap are executed before every
simple command, for command, case command,
select command, every arithmetic for command, and before
the first command executes in a shell function.
The DEBUG trap is not inherited by shell functions unless the
function has been given the trace attribute or the
functrace option has been enabled using the shopt builtin.
The extdebug shell option has additional effects on the
DEBUG trap.
The trap builtin (see Bourne Shell Builtins) allows an
ERR pseudo-signal specification, similar to EXIT and DEBUG.
Commands specified with an ERR trap are executed after a simple
command fails, with a few exceptions.
The ERR trap is not inherited by shell functions unless the
-o errtrace option to the set builtin is enabled.
The trap builtin (see Bourne Shell Builtins) allows a
RETURN pseudo-signal specification, similar to
EXIT and DEBUG.
Commands specified with a RETURN trap are executed before
execution resumes after a shell function or a shell script executed with
. or source returns.
The RETURN trap is not inherited by shell functions unless the
function has been given the trace attribute or the
functrace option has been enabled using the shopt builtin.
type builtin is more extensive and gives more information
about the names it finds (see Bash Builtin Commands).
ulimit builtin provides control over many more per-process
resources (see Bash Builtin Commands).
umask builtin uses the -p option to display
the output in the form of a umask command
that may be reused as input (see Bourne Shell Builtins).
wait builtin has a -n option to wait for the
next child to exit, possibly selecting from a list of supplied jobs,
and the -p option to store information about a terminated
child process in a shell variable.
jsh
(it turns on job control).
mldmode and priv) not present in Bash.
stop or newgrp builtins.
SHACCT variable or perform shell accounting.
sh uses a TIMEOUT variable like Bash uses
TMOUT.
More features unique to Bash may be found in Bash Features.
Since Bash is a completely new implementation, it does not suffer from many of the limitations of the SVR4.2 shell. For instance:
if or while
statement.
EOF under certain circumstances.
This can be the cause of some hard-to-find errors.
SIGSEGV.
If the shell is started from a process with
SIGSEGV blocked (e.g., by using the system() C library
function call), it misbehaves badly.
SIGSEGV,
SIGALRM, or SIGCHLD.
IFS, MAILCHECK,
PATH, PS1, or PS2 variables to be unset.
-x -v);
the SVR4.2 shell allows only one option argument (-xv).
In fact, some versions of the shell dump core if the second argument begins
with a ‘-’.
lastpipe option is enabled, and job control is not active,
Bash runs the last element of a pipeline in the current shell execution
environment.