Bash Reference Manual - 4. Bourne Shell Style Features

Go to the first, previous, next, last section, table of contents.

# 4. Bourne Shell Style Features

This section briefly summarizes things which Bash inherits from the Bourne Shell: builtins, variables, and other features. It also lists the significant differences between Bash and the Bourne Shell. Many of the builtins have been extended by POSIX or Bash.

## 4.1 Bourne Shell Builtins

The following shell builtin commands are inherited from the Bourne Shell. These commands are implemented as specified by the POSIX 1003.2 standard.

`:`

      : [arguments]

  Do nothing beyond expanding arguments and performing redirections. The return status is zero.
`.`

      . filename [arguments]

  Read and execute commands from the filename argument in the current shell context. If filename does not contain a slash, the `$PATH` variable is used to find filename. The current directory is searched if filename is not found in `$PATH`. If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise the positional parameters are unchanged. The return status is the exit status of the last command executed, or zero if no commands are executed. If filename is not found, or cannot be read, the return status is non-zero.
`break`

      break [n]

  Exit from a `for`, `while`, `until`, or `select` loop. If n is supplied, the nth enclosing loop is exited. n must be greater than or equal to 1. The return status is zero unless n is not greater than or equal to 1.
`cd`

      cd [-LP] [directory]

  Change the current working directory to directory. If directory is not given, the value of the `HOME` shell variable is used. If the shell variable `CDPATH` exists, it is used as a search path. If directory begins with a slash, `CDPATH` is not used. The ``-P'` option means to not follow symbolic links; symbolic links are followed by default or with the ``-L'` option. If directory is ``-'`, it is equivalent to `$OLDPWD`. The return status is zero if the directory is successfully changed, non-zero otherwise.
`continue`

      continue [n]

  Resume the next iteration of an enclosing `for`, `while`, `until`, or `select` loop. If n is supplied, the execution of the nth enclosing loop is resumed. n must be greater than or equal to 1. The return status is zero unless n is not greater than or equal to 1.
`eval`

      eval [arguments]

  The arguments are concatenated together into a single command, which is then read and executed, and its exit status returned as the exit status of `eval`. If there are no arguments or only empty arguments, the return status is zero.
`exec`

      exec [-cl] [-a name] [command [arguments]]

  If command is supplied, it replaces the shell without creating a new process. If the ``-l'` option is supplied, the shell places a dash in the zeroth arg passed to command. This is what the `login` program does. The ``-c'` option causes command to be executed with an empty environment. If ``-a'` is supplied, the shell passes name as the zeroth argument to command. If no command is specified, redirections may be used to affect the current shell environment. If there are no redirection errors, the return status is zero; otherwise the return status is non-zero.
`exit`

      exit [n]

  Exit the shell, returning a status of n to the shell's parent. Any trap on `EXIT` is executed before the shell terminates.
`export`

      export [-fn] [-p] [name[=value]]

  Mark each name to be passed to child processes in the environment. If the ``-f'` option is supplied, the names refer to shell functions; otherwise the names refer to shell variables. The ``-n'` option means to no longer mark each name for export. If no names are supplied, or if the ``-p'` option is given, a list of exported names is displayed. The ``-p'` option displays output in a form that may be reused as input. The return status is zero unless an invalid option is supplied, one of the names is not a valid shell variable name, or ``-f'` is supplied with a name that is not a shell function.
`getopts`

      getopts optstring name [args]

  `getopts` is used by shell scripts to parse positional parameters. optstring contains the option letters to be recognized; if a letter is followed by a colon, the option is expected to have an argument, which should be separated from it by white space. Each time it is invoked, `getopts` places the next option in the shell variable name, initializing name if it does not exist, and the index of the next argument to be processed into the variable `OPTIND`. `OPTIND` is initialized to 1 each time the shell or a shell script is invoked. When an option requires an argument, `getopts` places that argument into the variable `OPTARG`. The shell does not reset `OPTIND` automatically; it must be manually reset between multiple calls to `getopts` within the same shell invocation if a new set of parameters is to be used. When the end of options is encountered, `getopts` exits with a return value greater than zero. `OPTIND` is set to the index of the first non-option argument, and `name` is set to ``?'`. `getopts` normally parses the positional parameters, but if more arguments are given in args, `getopts` parses those instead. `getopts` can report errors in two ways. If the first character of optstring is a colon, silent error reporting is used. In normal operation diagnostic messages are printed when invalid options or missing option arguments are encountered. If the variable `OPTERR` is set to 0, no error messages will be displayed, even if the first character of `optstring` is not a colon. If an invalid option is seen, `getopts` places ``?'` into name and, if not silent, prints an error message and unsets `OPTARG`. If `getopts` is silent, the option character found is placed in `OPTARG` and no diagnostic message is printed. If a required argument is not found, and `getopts` is not silent, a question mark (``?'`) is placed in name, `OPTARG` is unset, and a diagnostic message is printed. If `getopts` is silent, then a colon (``:'`) is placed in name and `OPTARG` is set to the option character found.
`hash`

      hash [-r] [-p filename] [name]

  Remember the full pathnames of commands specified as name arguments, so they need not be searched for on subsequent invocations. The commands are found by searching through the directories listed in `$PATH`. The ``-p'` option inhibits the path search, and filename is used as the location of name. The ``-r'` option causes the shell to forget all remembered locations. If no arguments are given, information about remembered commands is printed. The return status is zero unless a name is not found or an invalid option is supplied.
`pwd`

      pwd [-LP]

  Print the current working directory. If the ``-P'` option is supplied, the path printed will not contain symbolic links. If the ``-L'` option is supplied, the path printed may contain symbolic links. The return status is zero unless an error is encountered while determining the name of the current directory or an invalid option is supplied.
`readonly`

      readonly [-apf] [name] ...

  Mark each name as readonly. The values of these names may not be changed by subsequent assignment. If the ``-f'` option is supplied, each name refers to a shell function. The ``-a'` option means each name refers to an array variable. If no name arguments are given, or if the ``-p'` option is supplied, a list of all readonly names is printed. The ``-p'` option causes output to be displayed in a format that may be reused as input. The return status is zero unless an invalid option is supplied, one of the name arguments is not a valid shell variable or function name, or the ``-f'` option is supplied with a name that is not a shell function.
`return`

      return [n]

  Cause a shell function to exit with the return value n. This may also be used to terminate execution of a script being executed with the `.` builtin, returning either n or the exit status of the last command executed within the script as the exit status of the script. The return status is false if `return` is used outside a function and not during the execution of a script by ``.'`.
`shift`

      shift [n]

  Shift the positional parameters to the left by n. The positional parameters from n+1 ... `$#` are renamed to `$1` ... `$#`-n+1. Parameters represented by the numbers `$#` to n+1 are unset. n must be a non-negative number less than or equal to `$#`. If n is zero or greater than `$#`, the positional parameters are not changed. The return status is zero unless n is greater than `$#` or less than zero, non-zero otherwise.
`test`
`[`
  Evaluate a conditional expression expr. Each operator and operand must be a separate argument. Expressions are composed of the primaries described below in section 5.6 Bash Conditional Expressions. Expressions may be combined using the following operators, listed in decreasing order of precedence.
  `! expr`
    True if expr is false.
  `( expr )`
    Returns the value of expr. This may be used to override the normal precedence of operators.
  `expr1 -a expr2`
    True if both expr1 and expr2 are true.
  `expr1 -o expr2`
    True if either expr1 or expr2 is true.
  The `test` and `[` builtins evaluate conditional expressions using a set of rules based on the number of arguments.
  0 arguments
    The expression is false.
  1 argument
    The expression is true if and only if the argument is not null.
  2 arguments
    If the first argument is ``!'`, the expression is true if and only if the second argument is null. If the first argument is one of the unary conditional operators (see section 5.6 Bash Conditional Expressions), the expression is true if the unary test is true. If the first argument is not a valid unary operator, the expression is false.
  3 arguments
    If the second argument is one of the binary conditional operators (see section 5.6 Bash Conditional Expressions), the result of the expression is the result of the binary test using the first and third arguments as operands. If the first argument is ``!'`, the value is the negation of the two-argument test using the second and third arguments. If the first argument is exactly ``('` and the third argument is exactly ``)'`, the result is the one-argument test of the second argument. Otherwise, the expression is false. The ``-a'` and ``-o'` operators are considered binary operators in this case.
  4 arguments
    If the first argument is ``!'`, the result is the negation of the three-argument expression composed of the remaining arguments. Otherwise, the expression is parsed and evaluated according to precedence using the rules listed above.
  5 or more arguments
    The expression is parsed and evaluated according to precedence using the rules listed above.
`times`

      times

  Print out the user and system times used by the shell and its children. The return status is zero.
`trap`

      trap [-lp] [arg] [sigspec ...]

  The commands in arg are to be read and executed when the shell receives signal sigspec. If arg is absent or equal to ``-'`, all specified signals are reset to the values they had when the shell was started. If arg is the null string, then the signal specified by each sigspec is ignored by the shell and commands it invokes. If arg is ``-p'`, the shell displays the trap commands associated with each sigspec. If no arguments are supplied, or only ``-p'` is given, `trap` prints the list of commands associated with each signal number in a form that may be reused as shell input. Each sigspec is either a signal name such as `SIGINT` (with or without the `SIG` prefix) or a signal number. If a sigspec is `0` or `EXIT`, arg is executed when the shell exits. If a sigspec is `DEBUG`, the command arg is executed after every simple command. The ``-l'` option causes the shell to print a list of signal names and their corresponding numbers. Signals ignored upon entry to the shell cannot be trapped or reset. Trapped signals are reset to their original values in a child process when it is created. The return status is zero unless a sigspec does not specify a valid signal.
`umask`

      umask [-p] [-S] [mode]

  Set the shell process's file creation mask to mode. If mode begins with a digit, it is interpreted as an octal number; if not, it is interpreted as a symbolic mode mask similar to that accepted by the `chmod` command. If mode is omitted, the current value of the mask is printed. If the ``-S'` option is supplied without a mode argument, the mask is printed in a symbolic format. If the ``-p'` option is supplied, and mode is omitted, the output is in a form that may be reused as input. The return status is zero if the mode is successfully changed or if no mode argument is supplied, and non-zero otherwise.
`unset`

      unset [-fv] [name]

  Each variable or function name is removed. If no options are supplied, or the ``-v'` option is given, each name refers to a shell variable. If the ``-f'` option is given, the names refer to shell functions, and the function definition is removed. Readonly variables and functions may not be unset. The return status is zero unless a name does not exist or is readonly.

## 4.2 Bourne Shell Variables

Bash uses certain shell variables in the same way as the Bourne shell. In some cases, Bash assigns a default value to the variable.

`CDPATH`
  A colon-separated list of directories used as a search path for the `cd` builtin command.
`HOME`
  The current user's home directory; the default for the `cd` builtin command. The value of this variable is also used by tilde expansion (see section 3.5.2 Tilde Expansion).
`IFS`
  A list of characters that separate fields; used when the shell splits words as part of expansion.
`MAIL`
  If this parameter is set to a filename and the `MAILPATH` variable is not set, Bash informs the user of the arrival of mail in the specified file.
`MAILPATH`
  A colon-separated list of filenames which the shell periodically checks for new mail. Each list entry can specify the message that is printed when new mail arrives in the mail file by separating the file name from the message with a ``?'`. When used in the text of the message, `$_` expands to the name of the current mail file.
`OPTARG`
  The value of the last option argument processed by the `getopts` builtin.
`OPTIND`
  The index of the last option argument processed by the `getopts` builtin.
`PATH`
  A colon-separated list of directories in which the shell looks for commands.
`PS1`
  The primary prompt string. The default value is ``\s-\v\$ '`.
`PS2`
  The secondary prompt string. The default value is ``> '`.

## 4.3 Other Bourne Shell Features

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 subsequent sections.

### 4.3.1 Major Differences From The SVR4.2 Bourne Shell

- Bash is POSIX-conformant, even where the POSIX specification differs from traditional `sh` behavior.
- Bash has multi-character invocation options (see section 5.1 Invoking Bash).
- Bash has command-line editing (see section 8. Command Line Editing) and the `bind` builtin.
- Bash has command history (see section 7.1 Bash History Facilities) and the `history` and `fc` builtins to manipulate it.
- Bash implements `csh`-like history expansion (see section 7.3 History Expansion).
- Bash has one-dimensional array variables (see section 5.10 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 3.1.2.4 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 3.1.2.5 Locale-Specific Translation).
- Bash implements the `!` keyword to negate the return value of a pipeline (see section 3.2.2 Pipelines). 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). The display of the timing statistics may be controlled with the `TIMEFORMAT` variable.
- Bash includes the `select` compound command, which allows the generation of simple menus (see section 3.2.5 Conditional Constructs).
- Bash includes the `[[` compound command, which makes conditional testing part of the shell grammar (see section 3.2.5 Conditional Constructs).
- Bash includes brace expansion (see section 3.5.1 Brace Expansion) and tilde expansion (see section 3.5.2 Tilde Expansion).
- Bash implements command aliases and the `alias` and `unalias` builtins (see section 5.9 Aliases).
- Bash provides shell arithmetic, the `((` compound command (see section 3.2.5 Conditional Constructs), and arithmetic expansion (see section 5.8 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 includes the POSIX pattern removal ``%'`, ``#'`, ``%%'` and ``##'` expansions to remove leading or trailing substrings from variable values (see section 3.5.3 Shell Parameter Expansion).
- The expansion `${#xx}`, which returns the length of `${xx}`, is supported (see section 3.5.3 Shell Parameter Expansion).
- 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).
- 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).
- Bash has indirect variable expansion using `${!word}` (see section 3.5.3 Shell Parameter Expansion).
- Bash can expand positional parameters beyond `$9` using `${num}`.
- The POSIX `$()` form of command substitution is implemented (see section 3.5.4 Command Substitution), 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).
- 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.7 Bash Variables, for details.
- The `IFS` variable is used to split only the results of expansion, not all words (see section 3.5.7 Word Splitting). This closes a longstanding shell security hole.
- Bash implements the full set of POSIX.2 filename expansion operators, including character classes, equivalence classes, and collating symbols (see section 3.5.8 Filename Expansion).
- Bash implements extended pattern matching features when the `extglob` shell option is enabled (see section 3.5.8.1 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.
- Variable assignments preceding commands affect only that command, even builtins and functions (see section 3.7.4 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.
- 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).
- The `noclobber` option is available to avoid overwriting existing files with output redirection (see section 5.5 The Set Builtin). The ``>|'` redirection operator may be used to override `noclobber`.
- The Bash `cd` and `pwd` builtins (see section 4.1 Bourne Shell Builtins) each take ``-L'` and ``-P'` builtins 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 5.4 Bash Builtin Commands).
- The `command` builtin allows selective disabling of functions when command lookup is performed (see section 5.4 Bash Builtin Commands).
- Individual builtins may be enabled or disabled using the `enable` builtin (see section 5.4 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 4.1 Bourne Shell Builtins).
- Shell functions may be exported to children via the environment using `export -f` (see section 3.3 Shell Functions).
- 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).
- Bash includes a `help` builtin for quick reference to shell facilities (see section 5.4 Bash Builtin Commands).
- The `printf` builtin is available to display formatted output (see section 5.4 Bash Builtin Commands).
- The Bash `read` builtin (see section 5.4 Bash Builtin Commands) will read a line ending in ``\'` with the ``-r'` option, and will use the `REPLY` variable as a default if no 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 `return` builtin may be used to abort execution of scripts executed with the `.` or `source` builtins (see section 4.1 Bourne Shell Builtins).
- Bash includes the `shopt` builtin, for finer control of shell optional capabilities (see section 5.4 Bash Builtin Commands).
- Bash has much more optional behavior controllable with the `set` builtin (see section 5.5 The Set Builtin).
- The `test` builtin (see section 4.1 Bourne Shell Builtins) 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) 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 5.4 Bash Builtin Commands).
- 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).
- Bash implements a `csh`-like directory stack, and provides the `pushd`, `popd`, and `dirs` builtins to manipulate it (see section 5.11 The Directory Stack). 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 5.12 Controlling the Prompt).
- The Bash restricted mode is more useful (see section 5.13 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 6.2 Job Control Builtins) 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 section 5. Bash Features.

### 4.3.2 Implementation Differences From The SVR4.2 Shell

Since Bash is a completely new implementation, it does not suffer from many of the limitations of the SVR4.2 shell. For instance:

- Bash does not fork a subshell when redirecting into or out of a shell control structure such as an `if` or `while` statement.
- Bash does not allow unbalanced quotes. The SVR4.2 shell will silently insert a needed closing quote at `EOF` under certain circumstances. This can be the cause of some hard-to-find errors.
- The SVR4.2 shell uses a baroque memory management scheme based on trapping `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.
- In a questionable attempt at security, the SVR4.2 shell, when invoked without the ``-p'` option, will alter its real and effective UID and GID if they are less than some magic threshold value, commonly 100. This can lead to unexpected results.
- The SVR4.2 shell does not allow users to trap `SIGSEGV`, `SIGALRM`, or `SIGCHLD`.
- The SVR4.2 shell does not allow the `IFS`, `MAILCHECK`, `PATH`, `PS1`, or `PS2` variables to be unset.
- The SVR4.2 shell treats ``^'` as the undocumented equivalent of ``|'`.
- Bash allows multiple option arguments when it is invoked (`-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 ``-'`.
- The SVR4.2 shell exits a script if any builtin fails; Bash exits a script only if one of the POSIX.2 special builtins fails, and only for certain failures, as enumerated in the POSIX.2 standard.
- The SVR4.2 shell behaves differently when invoked as `jsh` (it turns on job control).

Go to the first, previous, next, last section, table of contents.