[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
4.1 Bourne Shell Builtins Builtin commands inherited from the Bourne Shell. 4.2 Bash Builtin Commands Table of builtins specific to Bash. 4.3 The Set Builtin This builtin is so overloaded it deserves its own section. 4.4 Special Builtins Builtin commands classified specially by POSIX.2.
Builtin commands are contained within the shell itself. When the name of a builtin command is used as the first word of a simple command (see section 3.2.1 Simple Commands), the shell executes the command directly, without invoking another program. Builtin commands are necessary to implement functionality impossible or inconvenient to obtain with separate utilities.
This section briefly the builtins which Bash inherits from the Bourne Shell, as well as the builtin commands which are unique to or have been extended in Bash.
Several builtin commands are described in other chapters: builtin commands which provide the Bash interface to the job control facilities (see section 7.2 Job Control Builtins), the directory stack (see section 6.8.1 Directory Stack Builtins), the command history (see section 9.2 Bash History Builtins), and the programmable completion facilities (see section 8.7 Programmable Completion Builtins).
Many of the builtins have been extended by POSIX or Bash.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following shell builtin commands are inherited from the Bourne Shell. These commands are implemented as specified by the POSIX 1003.2 standard.
: (a colon)
: [arguments] |
. (a period)
. filename [arguments] |
PATH
variable is used to find filename.
When Bash is not in POSIX mode, 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.
This builtin is equivalent to source
.
break
break [n] |
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] |
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] |
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] |
eval
.
If there are no arguments or only empty arguments, the return status is
zero.
exec
exec [-cl] [-a name] [command [arguments]] |
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
is executed before the shell terminates.
export
export [-fn] [-p] [name[=value]] |
getopts
getopts optstring name [args] |
getopts
is used by shell scripts to parse positional parameters.
optstring contains the option characters to be recognized; if a
character is followed by a colon, the option is expected to have an
argument, which should be separated from it by white space.
The colon (`:') and question mark (`?') may not be
used as option characters.
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] [-t] [name] |
$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 the `-t' option is supplied, the full pathname to which each
name corresponds is printed. If multiple name arguments are
supplied with `-t' the name is printed before the hashed
full pathname.
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] |
readonly
readonly [-apf] [name] ... |
return
return [n] |
.
(or source
) 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 non-zero if return
is used outside a function
and not during the execution of a script by .
or source
.
shift
shift [n] |
$#
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.
If n is not supplied, it is assumed to be 1.
The return status is zero unless n is greater than $#
or
less than zero, non-zero otherwise.
test
[
When the [
form is used, the last argument to the command must
be a ]
.
Expressions may be combined using the following operators, listed in decreasing order of precedence.
! expr
( expr )
expr1 -a expr2
expr1 -o expr2
The test
and [
builtins evaluate conditional
expressions using a set of rules based on the number of arguments.
times
times |
trap
trap [-lp] [arg] [sigspec ...] |
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.
If a sigspec is ERR
, the command arg
is executed whenever a simple command has a non-zero exit status.
The ERR
trap is not executed if the failed command is part of an
until
or while
loop, part of an if
statement,
part of a &&
or ||
list, or if the command's return
status is being inverted using !
.
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] |
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.
Note that when the mode is interpreted as an octal number, each number
of the umask is subtracted from 7
. Thus, a umask of 022
results in permissions of 755
.
unset
unset [-fv] [name] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes builtin commands which are unique to or have been extended in Bash. Some of these commands are specified in the POSIX 1003.2 standard.
alias
alias [ |
Without arguments or with the `-p' option, alias
prints
the list of aliases on the standard output in a form that allows
them to be reused as input.
If arguments are supplied, an alias is defined for each name
whose value is given. If no value is given, the name
and value of the alias is printed.
Aliases are described in 6.6 Aliases.
bind
bind [-m keymap] [-lpsvPSV] bind [-m keymap] [-q function] [-u function] [-r keyseq] bind [-m keymap] -f filename bind [-m keymap] -x keyseq:shell-command bind [-m keymap] keyseq:function-name |
Display current Readline (see section 8. 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 a Readline initialization file (see section 8.3 Readline Init File), but each binding must be passed as a separate argument: e.g., `"\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
-p
-P
-v
-V
-s
-S
-f filename
-q function
-u function
-r keyseq
-x keyseq:shell-command
The return status is zero unless an invalid option is supplied or an error occurs.
builtin
builtin [shell-builtin [args]] |
command
command [-pVv] command [arguments ...] |
PATH
are executed.
If there is a shell function named ls
, running `command ls'
within the function will execute the external command ls
instead of calling the function recursively.
The `-p' option means to use a default value for PATH
that is guaranteed to find all of the standard utilities.
The return status in this case is 127 if command cannot be
found or an error occurred, and the exit status of command
otherwise.
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 displayed; the `-V' option produces a more verbose description. In this case, the return status is zero if command is found, and non-zero if not.
declare
declare [-afFrxi] [-p] [name[=value]] |
Declare variables and give them attributes. If no names are given, then display the values of variables instead.
The `-p' option will display the attributes and values of each name. When `-p' is used, additional options are ignored. The `-F' option inhibits the display of function definitions; only the function name and attributes are printed. `-F' implies `-f'. The following options can be used to restrict output to variables with the specified attributes or to give variables attributes:
-a
-f
-i
-r
-x
Using `+' instead of `-' turns off the attribute instead.
When used in a function, declare
makes each name local,
as with the local
command.
The return status is zero unless an invalid option is encountered, an attempt is made to define a function using `-f foo=bar', an attempt is made to assign a value to a readonly variable, an attempt is made to assign a value to an array variable without using the compound assignment syntax (see section 6.7 Arrays), one of the names is not a valid shell variable name, an attempt is made to turn off readonly status for a readonly variable, an attempt is made to turn off array status for an array variable, or an attempt is made to display a non-existent function with `-f'.
echo
echo [-neE] [arg ...] |
xpg_echo
shell option may be used to
dynamically determine whether or not echo
expands these
escape characters by default.
echo
interprets the following escape sequences:
\a
\b
\c
\e
\f
\n
\r
\t
\v
\\
\nnn
\xHH
enable
enable [-n] [-p] [-f filename] [-ads] [name ...] |
test
binary
found via $PATH
instead of the shell builtin version, type
`enable -n test'.
If the `-p' option is supplied, or no name arguments appear, a list of shell builtins is printed. With no other arguments, the list consists of all enabled shell builtins. The `-a' option means to list each builtin with an indication of whether or not it is enabled.
The `-f' option means to load the new builtin command name from shared object filename, on systems that support dynamic loading. The `-d' option will delete a builtin loaded with `-f'.
If there are no options, a list of the shell builtins is displayed.
The `-s' option restricts enable
to the POSIX special
builtins. If `-s' is used with `-f', the new builtin becomes
a special builtin (see section 4.4 Special Builtins).
The return status is zero unless a name is not a shell builtin or there is an error loading a new builtin from a shared object.
help
help [-s] [pattern] |
help
gives detailed help
on all commands matching pattern, otherwise a list of
the builtins is printed.
The `-s' option restricts the information displayed to a short
usage synopsis.
The return status is zero unless no command matches pattern.
let
let expression [expression] |
let
builtin allows arithmetic to be performed on shell
variables. Each expression is evaluated according to the
rules given below in 6.5 Shell Arithmetic. If the
last expression evaluates to 0, let
returns 1;
otherwise 0 is returned.
local
local [option] name[=value] |
declare
.
local
can only be used within a function; it makes the variable
name have a visible scope restricted to that function and its
children. The return status is zero unless local
is used outside
a function, an invalid name is supplied, or name is a
readonly variable.
logout
logout [n] |
printf
|
printf(1)
formats, `%b' causes
printf
to expand backslash escape sequences in the corresponding
argument, and `%q' causes printf
to output the
corresponding argument in a format that can be reused as shell input.
The format is reused as necessary to consume all of the arguments. If the format requires more arguments than are supplied, the extra format specifications behave as if a zero value or null string, as appropriate, had been supplied. The return value is zero on success, non-zero on failure.
read
read [-ers] [-a aname] [-p prompt] [-t timeout] [-n nchars] [-d delim] [name ...] |
IFS
variable
are used to split the line into words.
The backslash character `\' may be used to remove any special
meaning for the next character read and for line continuation.
If no names are supplied, the line read is assigned to the
variable REPLY
.
The return code is zero, unless end-of-file is encountered or read
times out.
Options, if supplied, have the following meanings:
-a aname
-d delim
-e
-n nchars
read
returns after reading nchars characters rather than
waiting for a complete line of input.
-p prompt
-r
-s
-t timeout
read
to time out and return failure if a complete line of
input is not read within timeout seconds.
This option has no effect if read
is not reading input from the
terminal or a pipe.
shopt
shopt [-pqsu] [-o] [optname ...] |
-s
-u
-q
-o
set
builtin (see section 4.3 The Set Builtin).
If either `-s' or `-u' is used with no optname arguments, the display is limited to those options which are set or unset, respectively.
Unless otherwise noted, the shopt
options are disabled (off)
by default.
The return status when listing options is zero if all optnames are enabled, non-zero otherwise. When setting or unsetting options, the return status is zero unless an optname is not a valid shell option.
The list of shopt
options is:
cdable_vars
cd
builtin command that
is not a directory is assumed to be the name of a variable whose
value is the directory to change to.
cdspell
cd
command will be corrected.
The errors checked for are transposed characters,
a missing character, and a character too many.
If a correction is found, the corrected path is printed,
and the command proceeds.
This option is only used by interactive shells.
checkhash
checkwinsize
LINES
and COLUMNS
.
cmdhist
dotglob
execfail
exec
builtin command. An interactive shell does not exit if exec
fails.
expand_aliases
extglob
histappend
HISTFILE
variable when the shell exits, rather than overwriting the file.
histreedit
histverify
hostcomplete
huponexit
SIGHUP
to all jobs when an interactive
login shell exits (see section 3.7.6 Signals).
interactive_comments
lithist
cmdhist
option is enabled, multi-line commands are saved to the history with
embedded newlines rather than using semicolon separators where possible.
login_shell
mailwarn
"The mail in mailfile has been read"
is displayed.
no_empty_cmd_completion
PATH
for possible completions when completion is attempted
on an empty line.
nocaseglob
nullglob
progcomp
promptvars
restricted_shell
shift_verbose
shift
builtin prints an error message when the shift count exceeds the
number of positional parameters.
sourcepath
source
builtin uses the value of PATH
to find the directory containing the file supplied as an argument.
This option is enabled by default.
xpg_echo
echo
builtin expands backslash-escape sequences
by default.
The return status when listing options is zero if all optnames are enabled, non-zero otherwise. When setting or unsetting options, the return status is zero unless an optname is not a valid shell option.
source
source filename |
.
(see section 4.1 Bourne Shell Builtins).
type
type [-atp] [name ...] |
If the `-t' option is used, type
prints 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 name is not found, then nothing is printed, and
type
returns a failure status.
If the `-p' option is used, type
either returns the name
of the disk file that would be executed, or nothing if `-t'
would not return `file'.
If the `-a' option is used, type
returns all of the places
that contain an executable named file.
This includes aliases and functions, if and only if the `-p' option
is not also used.
The return status is zero if any of the names are found, non-zero if none are found.
typeset
typeset [-afFrxi] [-p] [name[=value]] |
typeset
command is supplied for compatibility with the Korn
shell; however, it has been deprecated in favor of the declare
builtin command.
ulimit
ulimit [-acdflmnpstuvSH] [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
-a
-c
-d
-f
-l
-m
-n
-p
-s
-t
-u
-v
If limit is given, it is the new value of the specified resource;
the special limit values hard
, soft
, and
unlimited
stand for the current hard limit, the current soft limit,
and no limit, respectively.
Otherwise, the current value of the soft limit for the specified resource
is printed, unless the `-H' option is supplied.
When setting new limits, if neither `-H' nor `-S' is supplied,
both the hard and soft limits are set.
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 return status is zero unless an invalid option or argument is supplied, or an error occurs while setting a new limit.
unalias
unalias [-a] [name ... ] |
Remove each name from the list of aliases. If `-a' is supplied, all aliases are removed. Aliases are described in 6.6 Aliases.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This builtin is so complicated that it deserves its own section.
set
set [--abefhkmnptuvxBCHP] [-o option] [argument ...] |
If no options or arguments are supplied, set
displays the names
and values of all shell variables and functions, sorted according to the
current locale, in a format that may be reused as input.
When options are supplied, they set or unset shell attributes. Options, if specified, have the following meanings:
-a
-b
-e
until
or while
loop, part of an if
statement,
part of a &&
or ||
list, or if the command's return
status is being inverted using !
.
A trap on ERR
, if set, is executed before the shell exits.
-f
-h
-k
-m
-n
-o option-name
Set the option corresponding to option-name:
allexport
-a
.
braceexpand
-B
.
emacs
emacs
-style line editing interface (see section 8. Command Line Editing).
errexit
-e
.
hashall
-h
.
histexpand
-H
.
history
ignoreeof
keyword
-k
.
monitor
-m
.
noclobber
-C
.
noexec
-n
.
noglob
-f
.
nolog
notify
-b
.
nounset
-u
.
onecmd
-t
.
physical
-P
.
posix
privileged
-p
.
verbose
-v
.
vi
vi
-style line editing interface.
xtrace
-x
.
-p
$BASH_ENV
and $ENV
files are not
processed, shell functions are not inherited from the environment,
and the SHELLOPTS
variable, if it appears in the environment,
is ignored.
If the shell is started with the effective user (group) id not equal to the
real user (group) id, and the -p
option is not supplied, these actions
are taken and the effective user id is set to the real user id.
If the -p
option is supplied at startup, the effective user id is
not reset.
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
-B
-C
-H
-P
cd
which change the current directory. The physical directory
is used instead. By default, Bash follows
the logical chain of directories when performing commands
which change the current directory.
For example, if `/usr/sys' is a symbolic link to `/usr/local/sys' then:
$ cd /usr/sys; echo $PWD /usr/sys $ cd ..; pwd /usr |
If set -P
is on, then:
$ cd /usr/sys; echo $PWD /usr/local/sys $ cd ..; pwd /usr/local |
--
-
Using `+' rather than `-' causes these options to be
turned off. The options can also be used upon invocation of the
shell. The current set of options may be found in $-
.
The remaining N arguments are positional parameters and are
assigned, in order, to $1
, $2
, ... $N
.
The special parameter #
is set to N.
The return status is always zero unless an invalid option is supplied.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
For historical reasons, the POSIX 1003.2 standard has classified several builtin commands as special. When Bash is executing in POSIX mode, the special builtins differ from other builtin commands in three respects:
When Bash is not executing in POSIX mode, these builtins behave no differently than the rest of the Bash builtin commands. The Bash POSIX mode is described in 6.11 Bash POSIX Mode.
These are the POSIX special builtins:
break : . continue eval exec exit export readonly return set shift trap unset |
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |