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

Bash Specific Features

This section describes the features unique to Bash.

Invoking 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
Don't read the `~/.bashrc' initialization file in an interactive shell. This is on by default if the shell is invoked as sh.
-rcfile filename
Execute commands from filename (instead of `~/.bashrc') in an interactive shell.
-noprofile
Don't load the system-wide startup file `/etc/profile' or any of the personal initialization files `~/.bash_profile', `~/.bash_login', or `~/.profile' when bash is invoked as a login shell.
-version
Display the version number of this shell.
-login
Make this shell act as if it were directly invoked from login. This is equivalent to `exec - bash' but can be issued from another shell, such as csh. If you wanted to replace your current login shell with a Bash login shell, you would say `exec bash -login'.
-nobraceexpansion
Do not perform curly brace expansion (see section Brace Expansion).
-nolineediting
Do not use the GNU Readline library (see section Command Line Editing) to read interactive command lines.
-posix
Change the behavior of Bash where the default operation differs from the Posix 1003.2 standard to match the standard. This is intended to make Bash behave as a strict superset of that standard.

There are several single-character options you can give which are not available with the set builtin.

-c string
Read and execute commands from string after processing the options, then exit.
-i
Force the shell to run interactively.
-s
If this flag is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell.

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.

Bash Startup Files

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 variable ENV is non-null, expand the
       variable and source the file named by the value.  If Bash is
       not started in Posix mode, it looks for BASH_ENV before
       ENV.

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.

Is This Shell Interactive?

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.

Bash Builtin Commands

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-name
Display 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
Use keymap as the keymap to be affected by the subsequent bindings. Acceptable keymap names are 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
List the names of all readline functions
-v
List current function names and bindings
-d
Dump function names and bindings in such a way that they can be re-read
-f filename
Read key bindings from filename
-q
Query about which keys invoke the named function
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
change and report the soft limit associated with a resource (the default if the -H option is not given).
-H
change and report the hard limit associated with a resource.
-a
all current limits are reported.
-c
the maximum size of core files created.
-d
the maximum size of a process's data segment.
-m
the maximum resident set size.
-s
the maximum stack size.
-t
the maximum amount of cpu time in seconds.
-f
the maximum size of files created by the shell.
-p
the pipe buffer size.
-n
the maximum number of open file descriptors.
-u
the maximum number of processes available to a single user.
-v
the maximum amount of virtual memory available to the process.
If limit is given, it is the new value of the specified resource. Otherwise, the current value of the specified resource is printed. If no option is given, then `-f' is assumed. Values are in 1024-byte increments, except for `-t', which is in seconds, `-p', which is in units of 512-byte blocks, and `-n' and `-u', which are unscaled values.

The Set Builtin

This builtin is so overloaded that it deserves its own section.

set
set [-abefhkmnptuvxldCHP] [-o option] [argument ...]
-a
Mark variables which are modified or created for export.
-b
Cause the status of terminated background jobs to be reported immediately, rather than before printing the next primary prompt.
-e
Exit immediately if a command exits with a non-zero status.
-f
Disable file name generation (globbing).
-h
Locate and remember (hash) commands as functions are defined, rather than when the function is executed.
-k
All keyword arguments are placed in the environment for a command, not just those that precede the command name.
-m
Job control is enabled (see section Job Control).
-n
Read commands but do not execute them.
-o option-name
Set the flag corresponding to option-name:
allexport
same as -a.
braceexpand
the shell will perform brace expansion (see section Brace Expansion).
emacs
use an emacs-style line editing interface (see section Command Line Editing).
errexit
same as -e.
histexpand
same as -H.
ignoreeof
the shell will not exit upon reading EOF.
interactive-comments
allow a word beginning with a `#' to cause that word and all remaining characters on that line to be ignored in an interactive shell.
monitor
same as -m.
noclobber
same as -C.
noexec
same as -n.
noglob
same as -f.
nohash
same as -d.
notify
same as -b.
nounset
same as -u.
physical
same as -P.
posix
change the behavior of Bash where the default operation differs from the Posix 1003.2 standard to match the standard. This is intended to make Bash behave as a strict superset of that standard.
privileged
same as -p.
verbose
same as -v.
vi
use a vi-style line editing interface.
xtrace
same as -x.
-p
Turn on privileged mode. In this mode, the $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
Exit after reading and executing one command.
-u
Treat unset variables as an error when substituting.
-v
Print shell input lines as they are read.
-x
Print commands and their arguments as they are executed.
-l
Save and restore the binding of the name in a for command.
-d
Disable the hashing of commands that are looked up for execution. Normally, commands are remembered in a hash table, and once found, do not have to be looked up again.
-C
Disallow output redirection to existing files.
-H
Enable ! style history substitution. This flag is on by default.
-P
If set, do not follow symbolic links when performing commands such as cd which change the current directory. The physical directory is used instead.
--
If no arguments follow this flag, then the positional parameters are unset. Otherwise, the positional parameters are set to the arguments, even if some of them begin with a -.
-
Signal the end of options, cause all remaining arguments to be assigned to the positional parameters. The -x and -v options are turned off. If there are no arguments, the positional parameters remain unchanged.
Using `+' rather than `-' causes these flags to be turned off. The flags can also be used upon invocation of the shell. The current set of flags may be found in $-. 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.

Bash Variables

These variables are set or used by bash, but other shells do not normally treat them specially.

HISTCONTROL
history_control
Set to a value of `ignorespace', it means don't enter lines which begin with a space or tab into the history list. Set to a value of `ignoredups', it means don't enter lines which match the last entered line. A value of `ignoreboth' combines the two options. Unset, or set to any other value than those above, means to save all lines on the history list.
HISTFILE
The name of the file to which the command history is saved.
HISTSIZE
If set, this is the maximum number of commands to remember in the history.
histchars
Up to three characters which control history expansion, quick substitution, and tokenization (see section History Interaction). The first character is the history-expansion-char, that is, the character which signifies the start of a history expansion, normally `!'. The second character is the character which signifies `quick substitution' when seen as the first character on a line, normally `^'. The optional third character is the character which signifies the remainder of the line is a comment, when found as the first character of a word, usually `#'. The history comment character causes history substitution to be skipped for the remaining words on the line. It does not necessarily cause the shell parser to treat the rest of the line as a comment.
HISTCMD
The history number, or index in the history list, of the current command. If HISTCMD is unset, it loses its special properties, even if it is subsequently reset.
hostname_completion_file
HOSTFILE
Contains the name of a file in the same format as `/etc/hosts' that should be read when the shell needs to complete a hostname. You can change the file interactively; the next time you attempt to complete a hostname, Bash will add the contents of the new file to the already existing database.
MAILCHECK
How often (in seconds) that the shell should check for mail in the files specified in MAILPATH.
PROMPT_COMMAND
If present, this contains a string which is a command to execute before the printing of each primary prompt ($PS1).
UID
The numeric real user id of the current user.
EUID
The numeric effective user id of the current user.
HOSTTYPE
A string describing the machine Bash is running on.
OSTYPE
A string describing the operating system Bash is running on.
FIGNORE
A colon-separated list of suffixes to ignore when performing filename completion A file name whose suffix matches one of the entries in FIGNORE is excluded from the list of matched file names. A sample value is `.o:~'
INPUTRC
The name of the Readline startup file, overriding the default of `~/.inputrc'.
BASH_VERSION
The version number of the current instance of Bash.
IGNOREEOF
Controls the action of the shell on receipt of an 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
If this variable exists, the shell will not exit in the case that it couldn't execute the file specified in the exec command.
nolinks
If present, says not to follow symbolic links when doing commands that change the current working directory. By default, bash follows the logical chain of directories when performing commands such as 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
/usr
If nolinks exists, then:
$ cd /usr/sys; echo $PWD
/usr/local/sys
$ cd ..; pwd
/usr/local
See also the description of the -P option to the set builtin, section The Set Builtin.

Shell Arithmetic

Arithmetic Evaluation

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.

- +
unary minus and plus
! ~
logical and bitwise negation
* / %
multiplication, division, remainder
+ -
addition, subtraction
<< >>
left and right bitwise shifts
<= >= < >
comparison
== !=
equality and inequality
&
bitwise AND
^
bitwise exclusive OR
|
bitwise OR
&&
logical AND
||
logical OR
= *= /= %= += -= <<= >>= &= ^= |=
assignment

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

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.

Arithmetic Builtins

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.

Controlling the Prompt

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
the time, in HH:MM:SS format.
\d
the date, in "Weekday Month Date" format (e.g. "Tue May 26").
\n
newline.
\s
the name of the shell, the basename of $0 (the portion following the final slash).
\w
the current working directory.
\W
the basename of $PWD.
\u
your username.
\h
the hostname.
\#
the command number of this command.
\!
the history number of this command.
\nnn
the character corresponding to the octal number nnn.
\$
if the effective uid is 0, #, otherwise $.
\\
a backslash.
\[
begin a sequence of non-printing characters. This could be used to embed a terminal control sequence into the prompt.
\]
end a sequence of non-printing characters.

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