[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes features unique to Bash.
6.1 Invoking Bash Command line options that you can give to Bash. 6.2 Bash Startup Files When and how Bash executes scripts. 6.3 Interactive Shells What an interactive shell is. 6.4 Bash Conditional Expressions Primitives used in composing expressions for the test
builtin.6.5 Shell Arithmetic Arithmetic on shell variables. 6.6 Aliases Substituting one command for another. 6.7 Arrays Array Variables. 6.8 The Directory Stack History of visited directories. 6.9 Controlling the Prompt Controlling the PS1 string. 6.10 The Restricted Shell A more controlled mode of shell execution. 6.11 Bash POSIX Mode Making Bash behave more closely to what the POSIX standard specifies.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
bash [long-opt] [-ir] [-abefhkmnptuvxdBCDHP] [-o option] [-O shopt_option] [argument ...] bash [long-opt] [-abefhkmnptuvxdBCDHP] [-o option] [-O shopt_option] -c string [argument ...] bash [long-opt] -s [-abefhkmnptuvxdBCDHP] [-o option] [-O shopt_option] [argument ...] |
In addition to the single-character shell command-line options (see section 4.3 The Set Builtin), there are several multi-character options that you can use. These options must appear on the command line before the single-character options in order for them to be recognized.
--dump-po-strings
gettext
PO (portable object) file format.
Equivalent to `-D' except for the output format.
--dump-strings
--help
--init-file filename
--rcfile filename
--login
--noediting
--noprofile
--norc
sh
.
--posix
--restricted
--verbose
--version
There are several single-character options that may be supplied at
invocation which are not available with the set
builtin.
-c string
$0
.
-i
-r
-s
-D
C
or POSIX
(see section 3.1.2.5 Locale-Specific Translation).
This implies the `-n' option; no commands will be executed.
[-+]O [shopt_option]
shopt
builtin (see section 4. Shell Builtin Commands).
If shopt_option is present, `-O' sets the value of that option;
`+O' unsets it.
If shopt_option is not supplied, the names and values of the shell
options accepted by shopt
are printed on the standard output.
If the invocation option is `+O', the output is displayed in a format
that may be reused as input.
--
--
signals the end of options and disables further option
processing.
Any arguments after the --
are treated as filenames and arguments.
A login shell is one whose first character of argument zero is `-', or one invoked with the `--login' option.
An interactive shell is one started without non-option arguments,
unless `-s' is specified,
without specifying the `-c' option, and whose input and output are both
connected to terminals (as determined by isatty(3)
), or one
started with the `-i' option. See section 6.3 Interactive Shells, for more
information.
If arguments remain after option processing, and neither the
`-c' nor the `-s'
option has been supplied, the first argument is assumed to
be the name of a file containing shell commands (see section 3.8 Shell Scripts).
When Bash is invoked in this fashion, $0
is set to the name of the file, and the positional parameters
are set to the remaining arguments.
Bash reads and executes commands from this file, then exits.
Bash's exit status is the exit status of the last command executed
in the script. If no commands are executed, the exit status is 0.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describs how Bash executes its startup files. If any of the files exist but cannot be read, Bash reports an error. Tildes are expanded in file names as described above under Tilde Expansion (see section 3.5.2 Tilde Expansion).
Interactive shells are described in 6.3 Interactive Shells.
When Bash is invoked as an interactive login shell, or as a non-interactive shell with the `--login' option, it first reads and executes commands from the file `/etc/profile', if that file exists. After reading that file, it looks for `~/.bash_profile', `~/.bash_login', and `~/.profile', in that order, and reads and executes commands from the first one that exists and is readable. The `--noprofile' option may be used when the shell is started to inhibit this behavior.
When a login shell exits, Bash reads and executes commands from the file `~/.bash_logout', if it exists.
When an interactive shell that is not a login shell is started, Bash reads and executes commands from `~/.bashrc', if that file exists. This may be inhibited by using the `--norc' option. The `--rcfile file' option will force Bash to read and execute commands from file instead of `~/.bashrc'.
So, typically, your `~/.bash_profile' contains the line
|
When Bash is started non-interactively, to run a shell script,
for example, it looks for the variable BASH_ENV
in the environment,
expands its value if it appears there, and uses the expanded value as
the name of a file to read and execute. Bash behaves as if the
following command were executed:
|
PATH
variable is not used to search for the
file name.
As noted above, if a non-interactive shell is invoked with the `--login' option, Bash attempts to read and execute commands from the login shell startup files.
sh
If Bash is invoked with the name sh
, it tries to mimic the
startup behavior of historical versions of sh
as closely as
possible, while conforming to the POSIX standard as well.
When invoked as an interactive login shell, or as a non-interactive
shell with the `--login' option, it first attempts to read
and execute commands from `/etc/profile' and `~/.profile', in
that order.
The `--noprofile' option may be used to inhibit this behavior.
When invoked as an interactive shell with the name sh
, Bash
looks for the variable ENV
, expands its value if it is defined,
and uses the expanded value as the name of a file to read and execute.
Since a shell invoked as sh
does not attempt to read and execute
commands from any other startup files, the `--rcfile' option has
no effect.
A non-interactive shell invoked with the name sh
does not attempt
to read any other startup files.
When invoked as sh
, Bash enters POSIX mode after
the startup files are read.
When Bash is started in POSIX mode, as with the
`--posix' command line option, it follows the POSIX standard
for startup files.
In this mode, interactive shells expand the ENV
variable
and commands are read and executed from the file whose name is the
expanded value.
No other startup files are read.
Bash attempts to determine when it is being run by the remote shell
daemon, usually rshd
. If Bash determines it is being run by
rshd, it reads and executes commands from `~/.bashrc', if that
file exists and is readable.
It will not do this if invoked as sh
.
The `--norc' option may be used to inhibit this behavior, and the
`--rcfile' option may be used to force another file to be read, but
rshd
does not generally invoke the shell with those options or
allow them to be specified.
If Bash is started with the effective user (group) id not equal to the
real user (group) id, and the -p
option is not supplied, no startup
files are read, shell functions are not inherited from the environment,
the SHELLOPTS
variable, if it appears in the environment, is ignored,
and the effective user id is set to the real user id.
If the -p
option is supplied at invocation, the startup behavior is
the same, but the effective user id is not reset.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
6.3.1 What is an Interactive Shell? What determines whether a shell is Interactive. 6.3.2 Is this Shell Interactive? How to tell if a shell is interactive. 6.3.3 Interactive Shell Behavior What changes in a interactive shell?
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
An interactive shell
is one started without non-option arguments, unless `-s' is
specified, without specifiying the `-c' option, and
whose input and output are both
connected to terminals (as determined by isatty(3)
),
or one started with the `-i' option.
An interactive shell generally reads from and writes to a user's terminal.
The `-s' invocation option may be used to set the positional parameters when an interactive shell is started.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To determine within a startup script whether or not Bash is
running interactively,
test the value of the `-' special parameter.
It contains i
when the shell is interactive. For example:
case "$-" in *i*) echo This shell is interactive ;; *) echo This shell is not interactive ;; esac |
Alternatively, startup scripts may examine the variable
PS1
; it is unset in non-interactive shells, and set in
interactive shells. Thus:
if [ -z "$PS1" ]; then echo This shell is not interactive else echo This shell is interactive fi |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When the shell is running interactively, it changes its behavior in several ways.
SIGTTIN
, SIGTTOU
, and SIGTSTP
.
PS1
before reading the first line
of a command, and expands and displays PS2
before reading the
second and subsequent lines of a multi-line command.
PROMPT_COMMAND
variable as a command
before printing the primary prompt, $PS1
(see section 5.2 Bash Variables).
ignoreeof
option to set -o
instead of exiting immediately when it receives an EOF
on its
standard input when reading a command (see section 4.3 The Set Builtin).
$HISTFILE
when an interactive shell exits.
SIGTERM
(see section 3.7.6 Signals).
SIGINT
is caught and handled
((see section 3.7.6 Signals).
SIGINT
will interrupt some shell builtins.
SIGHUP
to all jobs on exit
if the hupoxexit
shell option has been enabled (see section 3.7.6 Signals).
MAIL
, MAILPATH
, and MAILCHECK
shell variables
(see section 5.2 Bash Variables).
${var:?word}
expansions
(see section 3.5.3 Shell Parameter Expansion).
exec
will not cause the shell to exit
(see section 4.1 Bourne Shell Builtins).
cd
builtin is enabled by default (see the description of the cdspell
option to the shopt
builtin in 4.2 Bash Builtin Commands).
TMOUT
variable and exit
if a command is not read within the specified number of seconds after
printing $PS1
(see section 5.2 Bash Variables).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Conditional expressions are used by the [[
compound command
and the test
and [
builtin commands.
Expressions may be unary or binary. Unary expressions are often used to examine the status of a file. There are string operators and numeric comparison operators as well. If the file argument to one of the primaries is of the form `/dev/fd/N', then file descriptor N is checked. If the file argument to one of the primaries is one of `/dev/stdin', `/dev/stdout', or `/dev/stderr', file descriptor 0, 1, or 2, respectively, is checked.
-a file
-b file
-c file
-d file
-e file
-f file
-g file
-h file
-k file
-p file
-r file
-s file
-t fd
-u file
-w file
-x file
-O file
-G file
-L file
-S file
-N file
file1 -nt file2
file1 -ot file2
file1 -ef file2
-o optname
set
builtin (see section 4.3 The Set Builtin).
-z string
-n string
string
string1 == string2
string1 != string2
string1 < string2
string1 > string2
arg1 OP arg2
OP
is one of
`-eq', `-ne', `-lt', `-le', `-gt', or `-ge'.
These arithmetic binary operators return true if arg1
is equal to, not equal to, less than, less than or equal to,
greater than, or greater than or equal to arg2,
respectively. Arg1 and arg2
may be positive or negative integers.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The shell allows arithmetic expressions to be evaluated, as one of
the shell expansions or by the let
builtin.
Evaluation is done in long integers with no check for overflow, though division by 0 is trapped and flagged as an error. The operators and their precedence and associativity are the same as in the C language. The following list of operators is grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence.
id++ id--
++id --id
- +
! ~
**
* / %
+ -
<< >>
<= >= < >
== !=
&
^
|
&&
||
expr ? expr : expr
= *= /= %= += -= <<= >>= &= ^= |=
expr1 , expr2
Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. The value of a variable is evaluated as an arithmetic expression when it is referenced. A shell variable need not have its integer attribute turned on to be used in an expression.
Constants with a leading 0 are interpreted as octal numbers.
A leading `0x' or `0X' denotes hexadecimal. Otherwise,
numbers take the form [base#
]n, where base
is a decimal number between 2 and 64 representing the arithmetic
base, and n is a number in that base. If base#
is
omitted, then base 10 is used.
The digits greater than 9 are represented by the lowercase letters,
the uppercase letters, `@', and `_', in that order.
If base is less than or equal to 36, lowercase and uppercase
letters may be used interchangably to represent numbers between 10
and 35.
Operators are evaluated in order of precedence. Sub-expressions in parentheses are evaluated first and may override the precedence rules above.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Aliases allow a string to be substituted for a word when it is used
as the first word of a simple command.
The shell maintains a list of aliases that may be set and unset with
the alias
and unalias
builtin commands.
The first word of each simple command, if unquoted, is checked to see
if it has an alias.
If so, that word is replaced by the text of the alias.
The alias name and the replacement text may contain any valid
shell input, including shell metacharacters, with the exception
that the alias name may not contain `='.
The first word of the replacement text is tested for
aliases, but a word that is identical to an alias being expanded
is not expanded a second time. This means that one may alias
ls
to "ls -F"
,
for instance, and Bash does not try to recursively expand the
replacement text. If the last character of the alias value is a
space or tab character, then the next command word following the
alias is also checked for alias expansion.
Aliases are created and listed with the alias
command, and removed with the unalias
command.
There is no mechanism for using arguments in the replacement text,
as in csh
.
If arguments are needed, a shell function should be used
(see section 3.3 Shell Functions).
Aliases are not expanded when the shell is not interactive,
unless the expand_aliases
shell option is set using
shopt
(see section 4.2 Bash Builtin Commands).
The rules concerning the definition and use of aliases are
somewhat confusing. Bash
always reads at least one complete line
of input before executing any
of the commands on that line. Aliases are expanded when a
command is read, not when it is executed. Therefore, an
alias definition appearing on the same line as another
command does not take effect until the next line of input is read.
The commands following the alias definition
on that line are not affected by the new alias.
This behavior is also an issue when functions are executed.
Aliases are expanded when a function definition is read,
not when the function is executed, because a function definition
is itself a compound command. As a consequence, aliases
defined in a function are not available until after that
function is executed. To be safe, always put
alias definitions on a separate line, and do not use alias
in compound commands.
For almost every purpose, shell functions are preferred over aliases.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Bash provides one-dimensional array variables. Any variable may be used as
an array; the declare
builtin will explicitly declare an array.
There is no maximum
limit on the size of an array, nor any requirement that members
be indexed or assigned contiguously. Arrays are zero-based.
An array is created automatically if any variable is assigned to using the syntax
name[subscript]=value |
The subscript is treated as an arithmetic expression that must evaluate to a number greater than or equal to zero. To explicitly declare an array, use
declare -a name |
declare -a name[subscript] |
declare
and
readonly
builtins. Each attribute applies to all members of
an array.
Arrays are assigned to using compound assignments of the form
name=(value1 ... valuen) |
[[subscript]=]
string. If
the optional subscript is supplied, that index is assigned to;
otherwise the index of the element assigned is the last index assigned
to by the statement plus one. Indexing starts at zero.
This syntax is also accepted by the declare
builtin. Individual array elements may be assigned to using the
name[
subscript]=
value syntax introduced above.
Any element of an array may be referenced using
${name[
subscript]}
.
The braces are required to avoid
conflicts with the shell's filename expansion operators. If the
subscript is `@' or `*', the word expands to all members
of the array name. These subscripts differ only when the word
appears within double quotes. If the word is double-quoted,
${name[*]}
expands to a single word with
the value of each array member separated by the first character of the
IFS
variable, and ${name[@]}
expands each element of
name to a separate word. When there are no array members,
${name[@]}
expands to nothing. This is analogous to the
expansion of the special parameters `@' and `*'.
${#name[
subscript]}
expands to the length of
${name[
subscript]}
.
If subscript is `@' or
`*', the expansion is the number of elements in the array.
Referencing an array variable without a subscript is equivalent to
referencing element zero.
The unset
builtin is used to destroy arrays.
unset
name[subscript]
destroys the array element at index subscript.
unset
name, where name is an array, removes the
entire array. A subscript of `*' or `@' also removes the
entire array.
The declare
, local
, and readonly
builtins each accept a `-a'
option to specify an array. The read
builtin accepts a `-a'
option to assign a list of words read from the standard input
to an array, and can read values from the standard input into
individual array elements. The set
and declare
builtins display array values in a way that allows them to be
reused as input.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
6.8.1 Directory Stack Builtins Bash builtin commands to manipulate the directory stack.
The directory stack is a list of recently-visited directories. The
pushd
builtin adds directories to the stack as it changes
the current directory, and the popd
builtin removes specified
directories from the stack and changes the current directory to
the directory removed. The dirs
builtin displays the contents
of the directory stack.
The contents of the directory stack are also visible
as the value of the DIRSTACK
shell variable.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
dirs
dirs [+N | -N] [-clpv] |
pushd
command; the
popd
command removes directories from the list.
+N
dirs
when invoked without options), starting
with zero.
-N
dirs
when invoked without options), starting
with zero.
-c
-l
-p
dirs
to print the directory stack with one entry per
line.
-v
dirs
to print the directory stack with one entry per
line, prefixing each entry with its index in the stack.
popd
popd [+N | -N] [-n] |
Remove the top entry from the directory stack, and cd
to the new top directory.
When no arguments are given, popd
removes the top directory from the stack and
performs a cd
to the new top directory. The
elements are numbered from 0 starting at the first directory listed with
dirs
; i.e., popd
is equivalent to popd +0
.
+N
dirs
), starting with zero.
-N
dirs
), starting with zero.
-n
pushd
pushd [dir | +N | -N] [-n] |
Save the current directory on the top of the directory stack
and then cd
to dir.
With no arguments, pushd
exchanges the top two directories.
+N
dirs
, starting with zero) to the top of
the list by rotating the stack.
-N
dirs
, starting with zero) to the top of
the list by rotating the stack.
-n
dir
cd
dir'.
cd
s to dir.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The value of the variable PROMPT_COMMAND
is examined just before
Bash prints each primary prompt. If PROMPT_COMMAND
is set and
has a non-null value, then the
value is executed just as if it had been typed on the command line.
In addition, the following table describes the special characters which can appear in the prompt variables:
\a
\d
\e
\h
\H
\j
\l
\n
\r
\s
$0
(the portion
following the final slash).
\t
\T
\@
\A
\u
\v
\V
\w
\W
$PWD
.
\!
\#
\$
#
, otherwise $
.
\nnn
\\
\[
\]
The command number and the history number are usually different: the history number of a command is its position in the history list, which may include commands restored from the history file (see section 9.1 Bash History Facilities), while the command number is the position in the sequence of commands executed during the current shell session.
After the string is decoded, it is expanded via
parameter expansion, command substitution, arithmetic
expansion, and quote removal, subject to the value of the
promptvars
shell option (see section 4.2 Bash Builtin Commands).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
If Bash is started with the name rbash
, or the
`--restricted'
option is supplied at invocation, the shell becomes restricted.
A restricted shell is used to
set up an environment more controlled than the standard shell.
A restricted shell behaves identically to bash
with the exception that the following are disallowed:
cd
builtin.
SHELL
, PATH
,
ENV
, or BASH_ENV
variables.
.
builtin command.
hash
builtin command.
SHELLOPTS
from the shell environment at startup.
exec
builtin to replace the shell with another command.
enable
builtin.
command
builtin.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Starting Bash with the `--posix' command-line option or executing `set -o posix' while Bash is running will cause Bash to conform more closely to the POSIX 1003.2 standard by changing the behavior to match that specified by POSIX in areas where the Bash default differs.
The following list is what's changed when `POSIX mode' is in effect:
$PATH
to find the new location. This is also available with
`shopt -s checkhash'.
SIGTSTP
.
PS1
and PS2
expansions of `!' to
the history number and `!!' to `!' are enabled,
and parameter expansion is performed on the values of PS1
and
PS2
regardless of the setting of the promptvars
option.
$ENV
) rather than
the normal Bash files.
$HISTFILE
).
.
filename
is not found.
name
s. That is, they may not
contain characters other than letters, digits, and underscores, and
may not start with a digit. Declaring a function with an invalid name
causes a fatal syntax error in non-interactive shells.
cd
builtin finds a directory to change to
using $CDPATH
, the
value it assigns to the PWD
variable does not contain any
symbolic links, as if `cd -P' had been executed.
CDPATH
is set, the cd
builtin will not implicitly
append the current directory to it. This means that cd
will
fail if no valid directory name can be constructed from
any of the entries in $CDPATH
, even if the a directory with
the same name as the name given as an argument to cd
exists
in the current directory.
for
statement or the selection variable in a
select
statement is a readonly variable.
export
and readonly
builtin commands display their
output in the format required by POSIX 1003.2.
trap
builtin displays signal names without the leading
SIG
.
.
and source
builtins do not search the current directory
for the filename argument if it is not found by searching PATH
.
set
builtin is invoked without options, it does not display
shell function names and definitions.
set
builtin is invoked without options, it displays
variable values without quotes, unless they contain shell metacharacters,
even if the result contains nonprinting characters.
There is other POSIX 1003.2 behavior that Bash does not implement. Specifically:
$0
to the full pathname of
the script as found by searching $PATH
, rather than the command as
typed by the user.
$PATH
, bash
checks execute permission bits rather than read permission bits, just as
if it were searching for a command.
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |