This section describes the features unique to Bash.
In addition to the single-character shell command-line options (see section 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 to be recognized.
-norc
sh
.
-rcfile filename
-noprofile
-version
-login
csh
. If you wanted to replace your
current login shell with a Bash login shell, you would say
`exec bash -login'.
-nobraceexpansion
-nolineediting
-posix
There are several single-character options you can give which are
not available with the set
builtin.
-c string
-i
-s
An interactive shell is one whose input and output are both
connected to terminals (as determined by isatty()
), or one
started with the -i
option.
When and how Bash executes startup files.
For Login shells (subject to the -noprofile option): On logging in: If `/etc/profile' exists, then source it. If `~/.bash_profile' exists, then source it, else if `~/.bash_login' exists, then source it, else if `~/.profile' exists, then source it. On logging out: If `~/.bash_logout' exists, source it. For non-login interactive shells (subject to the -norc and -rcfile options): On starting up: If `~/.bashrc' exists, then source it. For non-interactive shells: On starting up: If the environment variableENV
is non-null, expand the variable and source the file named by the value. If Bash is not started in Posix mode, it looks forBASH_ENV
beforeENV
.
So, typically, your ~/.bash_profile
contains the line
if [ -f ~/.bashrc
]; then source ~/.bashrc
; fi
after (or before) any login specific initializations.
If Bash is invoked as sh
, it tries to mimic the behavior of
sh
as closely as possible. For a login shell, it attempts to
source only `/etc/profile' and `~/.profile', in that order.
The -noprofile
option may still be used to disable this behavior.
A shell invoked as sh
does not attempt to source any other
startup files.
When Bash is started in POSIX mode, as with the
-posix
command line option, it follows the Posix 1003.2
standard for startup files. In this mode, the ENV
variable is expanded and that file sourced; no other startup files
are read.
You may wish to determine within a startup script whether Bash is
running interactively or not. To do this, 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
You can ask an interactive Bash to not run your `~/.bashrc' file
with the -norc
flag. You can change the name of the
`~/.bashrc' file to any other file name with -rcfile
filename
. You can ask Bash to not run your
`~/.bash_profile' file with the -noprofile
flag.
This section describes builtin commands which are unique to or have been extended in Bash.
builtin
builtin [shell-builtin [args]]Run a shell builtin. This is useful when you wish to rename a shell builtin to be a function, but need the functionality of the builtin within the function itself.
bind
bind [-m keymap] [-lvd] [-q name] bind [-m keymap] -f filename bind [-m keymap] keyseq:function-nameDisplay current Readline (see section Command Line Editing) key and function bindings, or bind a key sequence to a Readline function or macro. The binding syntax accepted is identical to that of `.inputrc' (see section Readline Init File), but each binding must be passed as a separate argument: `"\C-x\C-r":re-read-init-file'. Options, if supplied, have the following meanings:
-m keymap
emacs
,
emacs-standard
,
emacs-meta
,
emacs-ctlx
,
vi
,
vi-move
,
vi-command
, and
vi-insert
.
vi
is equivalent to vi-command
;
emacs
is equivalent to emacs-standard
.
-l
-v
-d
-f filename
-q
command
command [-pVv] command [args ...]Runs command with arg ignoring shell functions. If you have a shell function called
ls
, and you wish to call
the command ls
, you can say `command ls'. The
-p
option means to use a default value for $PATH
that is guaranteed to find all of the standard utilities.
If either the -V
or -v
option is supplied, a
description of command is printed. The -v
option
causes a single word indicating the command or file name used to
invoke command to be printed; the -V
option produces
a more verbose description.
declare
declare [-frxi] [name[=value]]Declare variables and/or give them attributes. If no names are given, then display the values of variables instead.
-f
means to use function names only. -r
says to
make names readonly. -x
says to mark names
for export. -i
says that the variable is to be treated as
an integer; arithmetic evaluation (see section Shell Arithmetic) is
performed when the variable is assigned a value. Using +
instead of -
turns off the attribute instead. When used in
a function, declare
makes names local, as with the
local
command.
enable
enable [-n] [-a] [name ...]Enable and disable builtin shell commands. This allows you to use a disk command which has the same name as a shell builtin. If
-n
is used, the names become disabled. Otherwise
names are enabled. For example, to use the test
binary
found via $PATH
instead of the shell builtin version, type
`enable -n test'. The -a
option means to list
each builtin with an indication of whether or not it is enabled.
help
help [pattern]Display helpful information about builtin commands. If pattern is specified,
help
gives detailed help
on all commands matching pattern, otherwise a list of
the builtins is printed.
local
local name[=value]For each argument, create a local variable called name, and give it value.
local
can only be used within a function; it makes the variable
name have a visible scope restricted to that function and its
children.
type
type [-all] [-type | -path] [name ...]For each name, indicate how it would be interpreted if used as a command name. If the
-type
flag is used, type
returns a single word
which is one of "alias", "function", "builtin", "file" or
"keyword", if name is an alias, shell function, shell builtin,
disk file, or shell reserved word, respectively.
If the -path
flag is used, type
either returns the name
of the disk file that would be executed, or nothing if -type
would not return "file".
If the -all
flag is used, returns all of the places that contain
an executable named file. This includes aliases and functions,
if and only if the -path
flag is not also used.
Type
accepts -a
, -t
, and -p
as equivalent to
-all
, -type
, and -path
, respectively.
ulimit
ulimit [-acdmstfpnuvSH] [limit]
Ulimit
provides control over the resources available to processes
started by the shell, on systems that allow such control. If an
option is given, it is interpreted as follows:
-S
-H
option is not given).
-H
-a
-c
-d
-m
-s
-t
-f
-p
-n
-u
-v
This builtin is so overloaded that it deserves its own section.
set
set [-abefhkmnptuvxldCHP] [-o option] [argument ...]
-a
-b
-e
-f
-h
-k
-m
-n
-o option-name
allexport
-a
.
braceexpand
emacs
errexit
-e
.
histexpand
-H
.
ignoreeof
interactive-comments
monitor
-m
.
noclobber
-C
.
noexec
-n
.
noglob
-f
.
nohash
-d
.
notify
-b
.
nounset
-u
.
physical
-P
.
posix
privileged
-p
.
verbose
-v
.
vi
vi
-style line editing interface.
xtrace
-x
.
-p
$ENV
file is not processed, and shell functions
are not inherited from the environment. This is enabled automatically
on startup if the effective user (group) id is not equal to the real
user (group) id. Turning this option off causes the effective user
and group ids to be set to the real user and group ids.
-t
-u
-v
-x
-l
for
command.
-d
-C
-H
-P
cd
which change the current directory. The physical directory
is used instead.
--
-
.
-
-x
and -v
options are turned off.
If there are no arguments, the positional parameters remain unchanged.
$-
. The
remaining N arguments are positional parameters and are
assigned, in order, to $1
, $2
, .. $N
. If
no arguments are given, all shell variables are printed.
These variables are set or used by bash, but other shells do not normally treat them specially.
HISTCONTROL
history_control
HISTFILE
HISTSIZE
histchars
HISTCMD
HISTCMD
is unset, it loses its special properties,
even if it is subsequently reset.
hostname_completion_file
HOSTFILE
MAILCHECK
MAILPATH
.
PROMPT_COMMAND
$PS1
).
UID
EUID
HOSTTYPE
OSTYPE
FIGNORE
FIGNORE
is excluded from the list of matched file names. A sample
value is `.o:~'
INPUTRC
BASH_VERSION
IGNOREEOF
EOF
character
as the sole input. If set, then the value of it is the number
of consecutive EOF
characters that can be read as the
first characters on an input line
before the shell will exit. If the variable exists but does not
have a numeric value (or has no value) then the default is 10.
If the variable does not exist, then EOF
signifies the end of
input to the shell. This is only in effect for interactive shells.
no_exit_on_failed_exec
exec
command.
nolinks
cd
which change the current directory.
For example, if `/usr/sys' is a link to `/usr/local/sys' then:
$ cd /usr/sys; echo $PWD /usr/sys $ cd ..; pwd /usrIf
nolinks
exists, then:
$ cd /usr/sys; echo $PWD /usr/local/sys $ cd ..; pwd /usr/localSee also the description of the
-P
option to the set
builtin, section The Set Builtin.
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 following list of operators is grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence.
- +
! ~
* / %
+ -
<< >>
<= >= < >
== !=
&
^
|
&&
||
= *= /= %= += -= <<= >>= &= ^= |=
Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. The value of a parameter is coerced to a long integer within an expression. 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 36 representing the arithmetic
base, and n is a number in that base. If base is
omitted, then base 10 is used.
Operators are evaluated in order of precedence. Sub-expressions in parentheses are evaluated first and may override the precedence rules above.
Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. There are two formats for arithmetic expansion:
$[ expression ] $(( expression ))
The expression is treated as if it were within double quotes, but a double quote inside the braces or parentheses is not treated specially. All tokens in the expression undergo parameter expansion, command substitution, and quote removal. Arithmetic substitutions may be nested.
The evaluation is performed according to the rules listed above. If the expression is invalid, Bash prints a message indicating failure and no substitution occurs.
let
let expression [expression]The
let
builtin allows arithmetic to be performed on shell
variables. Each expression is evaluated according to the
rules given previously (see section Arithmetic Evaluation). If the
last expression evaluates to 0, let
returns 1;
otherwise 0 is returned.
The value of the variable $PROMPT_COMMAND
is examined just before
Bash prints each primary prompt. If it is set and non-null, then the
value is executed just as if you had typed it on the command line.
In addition, the following table describes the special characters which
can appear in the PS1
variable:
\t
\d
\n
\s
$0
(the portion
following the final slash).
\w
\W
$PWD
.
\u
\h
\#
\!
\nnn
nnn
.
\$
#
, otherwise $
.
\\
\[
\]