Appendix B: Major Differences From The Bourne Shell 117
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 1003.2 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 as the baseline reference.
• Bash is posix-conformant, even where the posix specification differs from traditional sh behavior (see
Section 6.11 [Bash POSIX Mode], page 72 ).
• Bash has multi-character invocation options (see
Section 6.1 [Invoking Bash], page 59
).
• Bash has command-line editing (see
Chapter 8 [Command Line Editing], page 79
) and the bind builtin.
• Bash provides a programmable word completion mechanism (see
Section 8.6 [Programmable Completion], page 98
), and two builtin commands, complete and compgen, to manipulate it.
• Bash has command history (see
Section 9.1 [Bash History Facilities], page 103
) and the history and fc builtins to manipulate it.
• Bash implements csh-like history expansion (see
Section 9.3 [History Interaction], page 105
).
• Bash has one-dimensional array variables (see
Section 6.7 [Arrays], page 67
), 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 3.1.2.4 [ANSI-C Quoting], page 6 ).
• 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 3.1.2.5 [Locale Translation], page 7 ).
• Bash implements the ! keyword to negate the return value of a pipeline (see
Section 3.2.2 [Pipelines], page 8 ). Very useful when an if statement needs to act only if
a test fails.
• Bash has the time reserved word and command timing (see
Section 3.2.2 [Pipelines], page 8
). The display of the timing statistics may be controlled with the TIMEFORMAT variable.
• Bash implements the for (( expr1 ; expr2 ; expr3 )) arithmetic for command, similar to the C language (see
Section 3.2.4 [Looping Constructs], page 9
).
• Bash includes the select compound command, which allows the generation of simple menus (see
Section 3.2.5 [Conditional Constructs], page 10
).
• Bash includes the [[ compound command, which makes conditional testing part of the shell grammar (see
Section 3.2.5 [Conditional Constructs], page 10
).
118 Bash Reference Manual
• Bash includes brace expansion (see
Section 3.5.1 [Brace Expansion], page 15 ) and tilde
expansion (see
Section 3.5.2 [Tilde Expansion], page 16
).
• Bash implements command aliases and the alias and unalias builtins (see
Section 6.6
[Aliases], page 67 ).
• Bash provides shell arithmetic, the (( compound command (see
Section 3.2.5 [Conditional Constructs], page 10
), and arithmetic expansion (see
Section 6.5 [Shell Arithmetic], page 66 ).
• 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 includes the posix pattern removal ‘%’, ‘#’, ‘%%’ and ‘##’ expansions to remove leading or trailing substrings from variable values (see
Section 3.5.3 [Shell Parameter
Expansion], page 17 ).
• The expansion ${#xx}, which returns the length of ${xx}, is supported (see
Section 3.5.3 [Shell Parameter Expansion], page 17
).
• The expansion ${var:offset[:length]}, which expands to the substring of var’s value of length length, beginning at offset, is present (see
Section 3.5.3 [Shell Parameter
Expansion], page 17 ).
• The expansion ${var/[/]pattern[/replacement]}, which matches pattern and replaces it with replacement in the value of var, is available (see
Section 3.5.3 [Shell Parameter
Expansion], page 17 ).
• The expansion ${!prefix}* expansion, which expands to the names of all shell variables whose names begin with prefix, is available (see
Section 3.5.3 [Shell Parameter
Expansion], page 17 ).
• Bash has indirect variable expansion using ${!word} (see
Section 3.5.3 [Shell Parameter
Expansion], page 17 ).
• Bash can expand positional parameters beyond $9 using ${num}.
• The posix $() form of command substitution is implemented (see
Section 3.5.4 [Command Substitution], page 19
), and preferred to the Bourne shell’s ‘‘ (which is also implemented for backwards compatibility).
• Bash has process substitution (see
Section 3.5.6 [Process Substitution], page 19 ).
• Bash automatically assigns variables that provide information about the current user (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
Section 5.2 [Bash Variables], page 51 , for details.
• The IFS variable is used to split only the results of expansion, not all words (see
Section 3.5.7 [Word Splitting], page 20
). This closes a longstanding shell security hole.
• Bash implements the full set of posix 1003.2 filename expansion operators, including character classes, equivalence classes, and collating symbols (see
Section 3.5.8 [Filename
Expansion], page 20 ).
• Bash implements extended pattern matching features when the extglob shell option is enabled (see
Section 3.5.8.1 [Pattern Matching], page 21
).
• It is possible to have a variable and a function with the same name; sh does not separate the two name spaces.
Appendix B: Major Differences From The Bourne Shell 119
• Bash functions are permitted to have local variables using the local builtin, and thus useful recursive functions may be written (see
Section 4.2 [Bash Builtins], page 36
).
• Variable assignments preceding commands affect only that command, even builtins and functions (see
Section 3.7.4 [Environment], page 27
). 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 3.6 [Redirections], page 22
).
• 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 3.6 [Redirections], page 22
).
• Bash treats a number of filenames specially when they are used in redirection operators
(see
Section 3.6 [Redirections], page 22
).
• Bash can open network connections to arbitrary machines and services with the redirection operators (see
Section 3.6 [Redirections], page 22
).
• The noclobber option is available to avoid overwriting existing files with output redirection (see
Section 4.3 [The Set Builtin], page 46
). The ‘>|’ redirection operator may be used to override noclobber.
• The Bash cd and pwd builtins (see
Section 4.1 [Bourne Shell Builtins], page 31
) 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 and command builtins
(see
Section 4.2 [Bash Builtins], page 36
).
• The command builtin allows selective disabling of functions when command lookup is performed (see
Section 4.2 [Bash Builtins], page 36 ).
• Individual builtins may be enabled or disabled using the enable builtin (see
Section 4.2
[Bash Builtins], page 36
).
• 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 4.1 [Bourne Shell Builtins], page 31
).
• Shell functions may be exported to children via the environment using export -f (see
Section 3.3 [Shell Functions], page 12
).
• The Bash export, readonly, and declare 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 4.1 [Bourne Shell Builtins], page 31 ).
• Bash includes a help builtin for quick reference to shell facilities (see
Section 4.2 [Bash
Builtins], page 36 ).
• The printf builtin is available to display formatted output (see
Section 4.2 [Bash
Builtins], page 36 ).
• The Bash read builtin (see
Section 4.2 [Bash Builtins], page 36
) will read a line ending in ‘\’ with the ‘-r’ option, and will use the REPLY variable as a default if no non-option
120 Bash Reference Manual arguments are supplied. The Bash read builtin also accepts a prompt string with the
‘-p’ option and will use Readline to obtain the line when given the ‘-e’ 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.
• The return builtin may be used to abort execution of scripts executed with the . or source builtins (see
Section 4.1 [Bourne Shell Builtins], page 31 ).
• Bash includes the shopt builtin, for finer control of shell optional capabilities (see
Section 4.2 [Bash Builtins], page 36 ).
• Bash has much more optional behavior controllable with the set builtin (see
Section 4.3
[The Set Builtin], page 46 ).
• The test builtin (see
Section 4.1 [Bourne Shell Builtins], page 31
) is slightly different, as it implements the posix algorithm, which specifies the behavior based on the number of arguments.
• The trap builtin (see
Section 4.1 [Bourne Shell Builtins], page 31
) allows a DEBUG pseudo-signal specification, similar to EXIT. Commands specified with a DEBUG trap are executed after every simple command. The DEBUG trap is not inherited by shell functions.
• The Bash type builtin is more extensive and gives more information about the names it finds (see
Section 4.2 [Bash Builtins], page 36
).
• The Bash umask builtin permits a ‘-p’ option to cause the output to be displayed in the form of a umask command that may be reused as input (see
Section 4.1 [Bourne
Shell Builtins], page 31 ).
• Bash implements a csh-like directory stack, and provides the pushd, popd, and dirs builtins to manipulate it (see
Section 6.8 [The Directory Stack], page 68
). Bash also makes the directory stack visible as the value of the DIRSTACK shell variable.
• Bash interprets special backslash-escaped characters in the prompt strings when interactive (see
Section 6.9 [Printing a Prompt], page 70 ).
• The Bash restricted mode is more useful (see
Section 6.10 [The Restricted Shell], page 71 ); the SVR4.2 shell restricted mode is too limited.
• The disown builtin can remove a job from the internal shell job table (see
Section 7.2
[Job Control Builtins], page 76
) or suppress the sending of SIGHUP to a job when the shell exits as the result of a SIGHUP.
• The SVR4.2 shell has two privilege-related builtins (mldmode and priv) not present in
Bash.
• Bash does not have the stop or newgrp builtins.
• Bash does not use the SHACCT variable or perform shell accounting.
• The SVR4.2 sh uses a TIMEOUT variable like Bash uses TMOUT.
More features unique to Bash may be found in
Chapter 6 [Bash Features], page 59 .