[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Appendix B Major Differences From The Bourne Shell
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. 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.
-
Bash is POSIX-conformant, even where the POSIX specification
differs from traditional
sh
behavior (see section Bash POSIX Mode). - Bash has multi-character invocation options (see section Invoking Bash).
-
Bash has command-line editing (@pxref{Command Line Editing}) and
the
bind
builtin. -
Bash provides a programmable word completion mechanism
(@pxref{Programmable Completion}), and builtin commands
complete
,compgen
, andcompopt
, to manipulate it. -
Bash has command history (@pxref{Bash History Facilities}) and the
history
andfc
builtins to manipulate it. The Bash history list maintains timestamp information and uses the value of theHISTTIMEFORMAT
variable to display it. -
Bash implements
csh
-like history expansion (@pxref{History Interaction}). - Bash has one-dimensional array variables (see section Arrays), and the appropriate variable expansions and assignment syntax to use them. Several of the Bash builtins take options to act on arrays. Bash provides a number of built-in array variables.
-
The
$'…'
quoting syntax, which expands ANSI-C backslash-escaped characters in the text between the single quotes, is supported (see section ANSI-C Quoting). -
Bash supports the
$"…"
quoting syntax to do 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 section Locale-Specific Translation). -
Bash implements the
!
keyword to negate the return value of a pipeline (see section Pipelines). Very useful when anif
statement needs to act only if a test fails. The Bash ‘-o pipefail’ option toset
will cause a pipeline to return a failure status if any command fails. -
Bash has the
time
reserved word and command timing (see section Pipelines). The display of the timing statistics may be controlled with theTIMEFORMAT
variable. -
Bash implements the
for (( expr1 ; expr2 ; expr3 ))
arithmetic for command, similar to the C language (see section Looping Constructs). -
Bash includes the
select
compound command, which allows the generation of simple menus (see section Conditional Constructs). -
Bash includes the
[[
compound command, which makes conditional testing part of the shell grammar (see section Conditional Constructs), including optional regular expression matching. -
Bash provides optional case-insensitive matching for the
case
and[[
constructs. - Bash includes brace expansion (see section Brace Expansion) and tilde expansion (see section Tilde Expansion).
-
Bash implements command aliases and the
alias
andunalias
builtins (see section Aliases). -
Bash provides shell arithmetic, the
((
compound command (see section Conditional Constructs), and arithmetic expansion (see section Shell Arithmetic). -
Variables present in the shell’s initial environment are automatically
exported to child processes. The Bourne shell does not normally do
this unless the variables are explicitly marked using the
export
command. - Bash supports the ‘+=’ assignment operator, which appends to the value of the variable named on the left hand side.
- Bash includes the POSIX pattern removal ‘%’, ‘#’, ‘%%’ and ‘##’ expansions to remove leading or trailing substrings from variable values (see section Shell Parameter Expansion).
-
The expansion
${#xx}
, which returns the length of${xx}
, is supported (see section Shell Parameter Expansion). -
The expansion
${var:
offset[:
length]}
, which expands to the substring ofvar
’s value of length length, beginning at offset, is present (see section Shell Parameter Expansion). -
The expansion
${var/[/]
pattern[/
replacement]}
, which matches pattern and replaces it with replacement in the value ofvar
, is available (see section Shell Parameter Expansion). -
The expansion
${!prefix*}
expansion, which expands to the names of all shell variables whose names begin with prefix, is available (see section Shell Parameter Expansion). -
Bash has indirect variable expansion using
${!word}
(see section Shell Parameter Expansion). -
Bash can expand positional parameters beyond
$9
using${num}
. -
The POSIX
$()
form of command substitution is implemented (see section Command Substitution), and preferred to the Bourne shell’s``
(which is also implemented for backwards compatibility). - Bash has process substitution (see section Process Substitution).
-
Bash automatically assigns variables that provide information about the
current user (
UID
,EUID
, andGROUPS
), the current host (HOSTTYPE
,OSTYPE
,MACHTYPE
, andHOSTNAME
), and the instance of Bash that is running (BASH
,BASH_VERSION
, andBASH_VERSINFO
). See section Bash Variables, for details. -
The
IFS
variable is used to split only the results of expansion, not all words (see section Word Splitting). This closes a longstanding shell security hole. - The filename expansion bracket expression code uses ‘!’ and ‘^’ to negate the set of characters between the brackets. The Bourne shell uses only ‘!’.
- Bash implements the full set of POSIX filename expansion operators, including character classes, equivalence classes, and collating symbols (see section Filename Expansion).
-
Bash implements extended pattern matching features when the
extglob
shell option is enabled (see section Pattern Matching). -
It is possible to have a variable and a function with the same name;
sh
does not separate the two name spaces. -
Bash functions are permitted to have local variables using the
local
builtin, and thus useful recursive functions may be written (see section Bash Builtin Commands). -
Variable assignments preceding commands affect only that command, even
builtins and functions (see section Environment).
In
sh
, all variable assignments preceding commands are global unless the command is executed from the file system. - Bash performs filename expansion on filenames specified as operands to input and output redirection operators (see section Redirections).
- Bash contains the ‘<>’ redirection operator, allowing a file to be opened for both reading and writing, and the ‘&>’ redirection operator, for directing standard output and standard error to the same file (see section Redirections).
- Bash includes the ‘<<<’ redirection operator, allowing a string to be used as the standard input to a command.
- Bash implements the ‘[n]<&word’ and ‘[n]>&word’ redirection operators, which move one file descriptor to another.
- Bash treats a number of filenames specially when they are used in redirection operators (see section Redirections).
- Bash can open network connections to arbitrary machines and services with the redirection operators (see section Redirections).
-
The
noclobber
option is available to avoid overwriting existing files with output redirection (see section The Set Builtin). The ‘>|’ redirection operator may be used to overridenoclobber
. -
The Bash
cd
andpwd
builtins (see section Bourne Shell Builtins) each take ‘-L’ and ‘-P’ options to switch between logical and physical modes. -
Bash allows a function to override a builtin with the same name, and provides
access to that builtin’s functionality within the function via the
builtin
andcommand
builtins (see section Bash Builtin Commands). -
The
command
builtin allows selective disabling of functions when command lookup is performed (see section Bash Builtin Commands). -
Individual builtins may be enabled or disabled using the
enable
builtin (see section Bash Builtin Commands). -
The Bash
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 section Bourne Shell Builtins). -
Shell functions may be exported to children via the environment
using
export -f
(see section Shell Functions). -
The Bash
export
,readonly
, anddeclare
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. -
The Bash
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 section Bourne Shell Builtins). -
Bash includes a
help
builtin for quick reference to shell facilities (see section Bash Builtin Commands). -
The
printf
builtin is available to display formatted output (see section Bash Builtin Commands). -
The Bash
read
builtin (see section Bash Builtin Commands) will read a line ending in ‘\’ with the ‘-r’ option, and will use theREPLY
variable as a default if no non-option arguments are supplied. The Bashread
builtin also accepts a prompt string with the ‘-p’ option and will use Readline to obtain the line when given the ‘-e’ option. Theread
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 allowread
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. -
The
return
builtin may be used to abort execution of scripts executed with the.
orsource
builtins (see section Bourne Shell Builtins). -
Bash includes the
shopt
builtin, for finer control of shell optional capabilities (see section The Shopt Builtin), and allows these options to be set and unset at shell invocation (see section Invoking Bash). -
Bash has much more optional behavior controllable with the
set
builtin (see section The Set Builtin). - The ‘-x’ (‘xtrace’) option displays commands other than simple commands when performing an execution trace (see section The Set Builtin).
-
The
test
builtin (see section Bourne Shell Builtins) is slightly different, as it implements the POSIX algorithm, which specifies the behavior based on the number of arguments. -
Bash includes the
caller
builtin, which displays the context of any active subroutine call (a shell function or a script executed with the.
orsource
builtins). This supports the bash debugger. -
The
trap
builtin (see section Bourne Shell Builtins) allows aDEBUG
pseudo-signal specification, similar toEXIT
. Commands specified with aDEBUG
trap are executed before every simple command,for
command,case
command,select
command, every arithmeticfor
command, and before the first command executes in a shell function. TheDEBUG
trap is not inherited by shell functions unless the function has been given thetrace
attribute or thefunctrace
option has been enabled using theshopt
builtin. Theextdebug
shell option has additional effects on theDEBUG
trap.The
trap
builtin (see section Bourne Shell Builtins) allows anERR
pseudo-signal specification, similar toEXIT
andDEBUG
. Commands specified with anERR
trap are executed after a simple command fails, with a few exceptions. TheERR
trap is not inherited by shell functions unless the-o errtrace
option to theset
builtin is enabled.The
trap
builtin (see section Bourne Shell Builtins) allows aRETURN
pseudo-signal specification, similar toEXIT
andDEBUG
. Commands specified with anRETURN
trap are executed before execution resumes after a shell function or a shell script executed with.
orsource
returns. TheRETURN
trap is not inherited by shell functions unless the function has been given thetrace
attribute or thefunctrace
option has been enabled using theshopt
builtin. -
The Bash
type
builtin is more extensive and gives more information about the names it finds (see section Bash Builtin Commands). -
The Bash
umask
builtin permits a ‘-p’ option to cause the output to be displayed in the form of aumask
command that may be reused as input (see section Bourne Shell Builtins). -
Bash implements a
csh
-like directory stack, and provides thepushd
,popd
, anddirs
builtins to manipulate it (see section The Directory Stack). Bash also makes the directory stack visible as the value of theDIRSTACK
shell variable. - Bash interprets special backslash-escaped characters in the prompt strings when interactive (see section Controlling the Prompt).
- The Bash restricted mode is more useful (see section The Restricted Shell); the SVR4.2 shell restricted mode is too limited.
-
The
disown
builtin can remove a job from the internal shell job table (see section Job Control Builtins) or suppress the sending ofSIGHUP
to a job when the shell exits as the result of aSIGHUP
. - Bash includes a number of features to support a separate debugger for shell scripts.
-
The SVR4.2 shell has two privilege-related builtins
(
mldmode
andpriv
) not present in Bash. -
Bash does not have the
stop
ornewgrp
builtins. -
Bash does not use the
SHACCT
variable or perform shell accounting. -
The SVR4.2
sh
uses aTIMEOUT
variable like Bash usesTMOUT
.
More features unique to Bash may be found in Bash Features.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on February 28, 2014 using texi2html 5.0.