Penguin

BASH MAN PAGE (Part 2)

Navigation
This man page has been split into 7 pages:
bash(1)

  • NAME
  • SYNOPSIS
  • COPYRIGHT
  • DESCRIPTION
  • OPTIONS
  • ARGUMENTS
  • INVOCATION

Part 2

  • DEFINITIONS
  • RESERVED WORDS
  • SHELL GRAMMAR
  • COMMENTS
  • QUOTING

bash(1)Part3

  • PARAMETERS

bash(1)Part4

  • EXPANSION

bash(1)Part5

  • REDIRECTION
  • ALIASES
  • FUNCTIONS
  • ARITHMETIC EVALUATION
  • CONDITIONAL EXPRESSIONS
  • SIMPLE COMMAND EXPANSION
  • COMMAND EXECUTION
  • COMMAND EXECUTION ENVIRONMENT
  • ENVIRONMENT
  • EXIT STATUS
  • SIGNALS
  • JOB CONTROL
  • PROMPTING

bash(1)Part6

  • READLINE
  • HISTORY
  • HISTORY EXPANSION

bash(1)Part7

  • SHELL BUILTIN COMMANDS
  • RESTRICTED SHELL
  • SEE ALSO
  • FILES
  • AUTHORS
  • BUG REPORTS
  • BUGS

DEFINITIONS

The following definitions are used throughout the rest of this document.

blank
A space or tab.
word
A sequence of characters considered as a single unit by the shell. Also known as a token .
name
A word consisting only of alphanumeric characters and underscores, and beginning with an alphabetic character or an underscore. Also referred to as an identifier .
metacharacter
A character that, when unquoted, separates words. One of the following:

| & ; ( ) < > space tab

control operator
A token that performs a control function. It is one of the following symbols:

|| & && ; ;; ( ) | <newline>

RESERVED WORDS

Reserved words are words that have a special meaning to the shell. The following words are recognized as reserved when unquoted and either the first word of a simple command (see SHELL GRAMMAR below) or the third word of a case or for command:

.B

case do done elif else esac fi for function if in select then until while { } time [[[?]

SHELL GRAMMAR

Simple Commands

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed. The remaining words are passed as arguments to the invoked command.

The return value of a simple command is its exit status, or 128+n if the command is terminated by signal n .

Pipelines

A pipeline is a sequence of one or more commands separated by the character | . The format for a pipeline is
[__time__ [[__-p__?] [ !? command [ __?

The standard output of command is connected to the standard input of command2 . This connection is performed before any redirections specified by the command (see REDIRECTION below).

If the reserved word ! precedes a pipeline, the exit status of that pipeline is the logical NOT of the exit status of the last command. Otherwise, the status of the pipeline is the exit status of the last command. The shell waits for all commands in the pipeline to terminate before returning a value.

If the time reserved word precedes a pipeline, the elapsed as well as user and system time consumed by its execution are reported when the pipeline terminates. The -p option changes the output format to that specified by POSIX. The TIMEFORMAT variable may be set to a format string that specifies how the timing information should be displayed; see the description of TIMEFORMAT under Shell Variables below.

Each command in a pipeline is executed as a separate process (i.e., in a subshell).

Lists

A list is a sequence of one or more pipelines separated by one of the operators ; , & , && , or || , and optionally terminated by one of ; , & , or <newline> .

Of these list operators, && and || have equal precedence, followed by ; and &, which have equal precedence.

If a command is terminated by the control operator & , the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.

The control operators && and || denote AND lists and OR lists, respectively. An AND list has the form

command1 && command2

command2 is executed if, and only if, command1 returns an exit status of zero.

An OR list has the form

command1 || command2

command2 is executed if and only if command1 returns a non-zero exit status. The return status of AND and OR lists is the exit status of the last command executed in the list.

Compound Commands

A compound command is one of the following:

(list)
list is executed in a subshell. Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes. The return status is the exit status of list.
{ list; }
list is simply executed in the current shell environment. list must be terminated with a newline or semicolon. This is known as a group command. The return status is the exit status of list.
((expression))
The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION . If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to let "expression".
[[[__ ''expression'' __?]
Return a status of 0 or 1 depending on the evaluation of the conditional expression expression. Expressions are composed of the primaries described below under CONDITIONAL EXPRESSIONS . Word splitting and pathname expansion are not performed on the words between the [[[__ and __?]; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed.

.sp 1When the == and != operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below under Pattern Matching. The return value is 0 if the string matches or does not match the pattern, respectively, and 1 otherwise. Any part of the pattern may be quoted to force it to be matched as a string. .sp 1Expressions may be combined using the following operators, listed in decreasing order of precedence: .sp 1

( expression )
Returns the value of expression. This may be used to override the normal precedence of operators.
! expression
True if expression is false.
expression1 && expression2
True if both expression1 and expression2 are true.
expression1 || expression2
True if either expression1 or expression2 is true.

The && and

|| operators do not execute expression2 if the value of expression1 is sufficient to determine the return value of the entire conditional expression.

for name [ __in__ ''word''? ; do list ; done
The list of words following in is expanded, generating a list of items. The variable name is set to each element of this list in turn, and list is executed each time. If the in word is omitted, the for command executes list once for each positional parameter that is set (see PARAMETERS below). The return status is the exit status of the last command that executes. If the expansion of the items following in results in an empty list, no commands are executed, and the return status is 0.
for (( expr1 ; expr2 ; expr3 )) ; do list ; done
First, the arithmetic expression expr1 is evaluated according to the rules described below under ARITHMETIC EVALUATION . The arithmetic expression expr2 is then evaluated repeatedly until it evaluates to zero. Each time expr2 evaluates to a non-zero value, list is executed and the arithmetic expression expr3 is evaluated. If any expression is omitted, it behaves as if it evaluates to 1. The return value is the exit status of the last command in list that is executed, or false if any of the expressions is invalid.
select name [ __in__ ''word''? ; do list ; done
The list of words following in is expanded, generating a list of items. The set of expanded words is printed on the standard error, each preceded by a number. If the in word is omitted, the positional parameters are printed (see PARAMETERS below). The PS3 prompt is then displayed and a line read from the standard input. If the line consists of a number corresponding to one of the displayed words, then the value of name is set to that word. If the line is empty, the words and prompt are displayed again. If EOF is read, the command completes. Any other value read causes name to be set to null. The line read is saved in the variable REPLY . The list is executed after each selection until a break or return command is executed. The exit status of select is the exit status of the last command executed in list , or zero if no commands were executed.
case word in [ [[(? pattern [ __? ... ) list ;; ] ... esac
A case command first expands word, and tries to match it against each pattern in turn, using the same matching rules as for pathname expansion (see Pathname Expansion below). When a match is found, the corresponding list is executed. After the first match, no subsequent matches are attempted. The exit status is zero if no pattern matches. Otherwise, it is the exit status of the last command executed in list.
if list; then list; \
[ __elif__ ''list''; __then__ ''list'';? ... \ [ __else__ ''list'';? fi The if list is executed. If its exit status is zero, the then list is executed. Otherwise, each elif list is executed in turn, and if its exit status is zero, the corresponding then list is executed and the command completes. Otherwise, the else list is executed, if present. The exit status is the exit status of the last command executed, or zero if no condition tested true.

while list; do list; done

until list; do list; done
The while command continuously executes the do list as long as the last command in list returns an exit status of zero. The until command is identical to the while command, except that the test is negated; the do list is executed as long as the last command in list returns a non-zero exit status. The exit status of the while and until commands is the exit status of the last do list command executed, or zero if none was executed.
[ __function__? name () { list; }
This defines a function named name. The body of the function is the list of commands between { and }. This list is executed whenever name is specified as the name of a simple command. The exit status of a function is the exit status of the last command executed in the body. (See FUNCTIONS below.)

COMMENTS

In a non-interactive shell, or an interactive shell in which the interactive_comments option to the shopt builtin is enabled (see SHELL BUILTIN COMMANDS below), a word beginning with # causes that word and all remaining characters on that line to be ignored. An interactive shell without the interactive_comments option enabled does not allow comments. The interactive_comments option is on by default in interactive shells.

QUOTING

Quoting is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.

Each of the metacharacters listed above under DEFINITIONS has special meaning to the shell and must be quoted if it is to represent itself.

When the command history expansion facilities are being used, the history expansion character, usually !, must be quoted to prevent history expansion.

There are three quoting mechanisms: the escape character , single quotes, and double quotes.

A non-quoted backslash (\) is the escape character . It preserves the literal value of the next character that follows, with the exception of <newline>. If a \<newline> pair appears, and the backslash is not itself quoted, the \<newline> is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).

Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $ , ` , and \ . The characters $ and ` retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the following characters: $ , ` , ", \ , or <newline> . A double quote may be quoted within double quotes by preceding it with a backslash.

The special parameters * and @ have special meaning when in double quotes (see PARAMETERS below).

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specifed by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:

\a
alert (bell)
\b
backspace
\e
an escape character
\f
form feed
\n
new line
\r
carriage return
\t
horizontal tab
\v
vertical tab
\\e
backslash
\'
single quote
\nnn
the character whose ASCII code is the octal value nnn (one to three digits)
\xnnn
the character whose ASCII code is the hexadecimal value nnn (one to three digits)

The expanded result is single-quoted, as if the dollar sign had not been present.

A double-quoted string preceded by a dollar sign ($) will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.

This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.