| GNU Bash Reference Manual by Chet Ramey and Brian Fox Paperback (6"x9"), 180 pages ISBN 0954161777 RRP £19.95 ($29.95) "An essential resource .... the most detailed coverage available for all aspects of Bash" --- Linux User and Developer Magazine (Issue 37, Mar 2004) Get a printed copy>>> |
4.2 Bash Builtin Commands
This section describes builtin commands which are unique to or have been extended in Bash. Some of these commands are specified in the POSIX standard.
alias-
alias [
Without arguments or with the-p] [name[=value] ...]-poption,aliasprints 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 section 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 bind readline-command
Display current Readline (see section 8 Command Line Editing) key and function bindings, bind a key sequence to a Readline function or macro, or set a Readline variable. Each non-option argument is a command as it would appear in a Readline initialization file (see section 8.3 Readline Init File), but each binding or command 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-
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, andvi-insert.viis equivalent tovi-command;emacsis equivalent toemacs-standard. -l- List the names of all Readline functions.
-p- Display Readline function names and bindings in such a way that they can be used as input or in a Readline initialization file.
-P- List current Readline function names and bindings.
-v- Display Readline variable names and values in such a way that they can be used as input or in a Readline initialization file.
-V- List current Readline variable names and values.
-s- Display Readline key sequences bound to macros and the strings they output in such a way that they can be used as input or in a Readline initialization file.
-S- Display Readline key sequences bound to macros and the strings they output.
-f filename- Read key bindings from filename.
-q function- Query about which keys invoke the named function.
-u function- Unbind all keys bound to the named function.
-r keyseq- Remove any current binding for keyseq.
-x keyseq:shell-command- Cause shell-command to be executed whenever keyseq is entered.
builtin-
builtin [shell-builtin [args]]
Run a shell builtin, passing it args, and return its exit status. This is useful when defining a shell function with the same name as a shell builtin, retaining the functionality of the builtin within the function. The return status is non-zero if shell-builtin is not a shell builtin command. caller-
caller [expr]
Returns the context of any active subroutine call (a shell function or a script executed with the.orsourcebuiltins). Without expr,callerdisplays the line number and source filename of the current subroutine call. If a non-negative integer is supplied as expr,callerdisplays the line number, subroutine name, and source file corresponding to that position in the current execution call stack. This extra information may be used, for example, to print a stack trace. The current frame is frame 0. The return value is 0 unless the shell is not executing a subroutine call or expr does not correspond to a valid position in the call stack. command-
command [-pVv] command [arguments ...]
Runs command with arguments ignoring any shell function named command. Only shell builtin commands or commands found by searching thePATHare executed. If there is a shell function namedls, running ‘command ls’ within the function will execute the external commandlsinstead of calling the function recursively. The-poption means to use a default value forPATHthat 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-Vor-voption is supplied, a description of command is printed. The-voption causes a single word indicating the command or file name used to invoke command to be displayed; the-Voption produces a more verbose description. In this case, the return status is zero if command is found, and non-zero if not. declare-
declare [-afFirtx] [-p] [name[=value] ...]
Declare variables and give them attributes. If no names are given, then display the values of variables instead. The-poption will display the attributes and values of each name. When-pis used, additional options are ignored. The-Foption inhibits the display of function definitions; only the function name and attributes are printed. If theextdebugshell option is enabled usingshopt(see section 4.2 Bash Builtin Commands), the source file name and line number where the function is defined are displayed as well.-Fimplies-f. The following options can be used to restrict output to variables with the specified attributes or to give variables attributes:-a- Each name is an array variable (see section 6.7 Arrays).
-f- Use function names only.
-i- The variable is to be treated as an integer; arithmetic evaluation (see section 6.5 Shell Arithmetic) is performed when the variable is assigned a value.
-r- Make names readonly. These names cannot then be assigned values by subsequent assignment statements or unset.
-t-
Give each name the
traceattribute. Traced functions inherit theDEBUGandRETURNtraps from the calling shell. The trace attribute has no special meaning for variables. -x- Mark each name for export to subsequent commands via the environment.
declaremakes each name local, as with thelocalcommand. If a variable name is followed by =value, the value of the variable is set to value. 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 ...]
Output the args, separated by spaces, terminated with a newline. The return status is always 0. If-nis specified, the trailing newline is suppressed. If the-eoption is given, interpretation of the following backslash-escaped characters is enabled. The-Eoption disables the interpretation of these escape characters, even on systems where they are interpreted by default. Thexpg_echoshell option may be used to dynamically determine whether or notechoexpands these escape characters by default.echodoes not interpret--to mean the end of options.echointerprets the following escape sequences:\a- alert (bell)
\b- backspace
\c- suppress trailing newline
\e- escape
\f- form feed
\n- new line
\r- carriage return
\t- horizontal tab
\v- vertical tab
\\- backslash
\0nnn- the eight-bit character whose value is the octal value nnn (zero to three octal digits)
\xHH- the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
enable-
enable [-n] [-p] [-f filename] [-ads] [name ...]
Enable and disable builtin shell commands. Disabling a builtin allows a disk command which has the same name as a shell builtin to be executed without specifying a full pathname, even though the shell normally searches for builtins before disk commands. If-nis used, the names become disabled. Otherwise names are enabled. For example, to use thetestbinary found via$PATHinstead of the shell builtin version, type ‘enable -n test’. If the-poption 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-aoption means to list each builtin with an indication of whether or not it is enabled. The-foption means to load the new builtin command name from shared object filename, on systems that support dynamic loading. The-doption will delete a builtin loaded with-f. If there are no options, a list of the shell builtins is displayed. The-soption restrictsenableto the POSIX special builtins. If-sis 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]
Display helpful information about builtin commands. If pattern is specified,helpgives detailed help on all commands matching pattern, otherwise a list of the builtins is printed. The-soption restricts the information displayed to a short usage synopsis. The return status is zero unless no command matches pattern. let-
let expression [expression]
Theletbuiltin allows arithmetic to be performed on shell variables. Each expression is evaluated according to the rules given below in section 6.5 Shell Arithmetic. If the last expression evaluates to 0,letreturns 1; otherwise 0 is returned. local-
local [option] name[=value] ...
For each argument, a local variable named name is created, and assigned value. The option can be any of the options accepted bydeclare.localcan 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 unlesslocalis used outside a function, an invalid name is supplied, or name is a readonly variable. logout-
logout [n]
Exit a login shell, returning a status of n to the shell's parent. printf-
Write the formatted arguments to the standard output under the control of the format. The format is a character string which contains three types of objects: plain characters, which are simply copied to standard output, character escape sequences, which are converted and copied to the standard output, and format specifications, each of which causes printing of the next successive argument. In addition to the standardprintf[-v var] format [arguments]printf(1)formats, ‘%b’ causesprintfto expand backslash escape sequences in the corresponding argument, (except that ‘\c’ terminates output, backslashes in ‘\'’, ‘\"’, and ‘\?’ are not removed, and octal escapes beginning with ‘\0’ may contain up to four digits), and ‘%q’ causesprintfto output the corresponding argument in a format that can be reused as shell input. The-voption causes the output to be assigned to the variable var rather than being printed to the standard output. 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] [-d delim] [-n nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
One line is read from the standard input, or from the file descriptor fd supplied as an argument to the-uoption, and the first word is assigned to the first name, the second word to the second name, and so on, with leftover words and their intervening separators assigned to the last name. If there are fewer words read from the input stream than names, the remaining names are assigned empty values. The characters in the value of theIFSvariable 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 variableREPLY. The return code is zero, unless end-of-file is encountered,readtimes out, or an invalid file descriptor is supplied as the argument to-u. Options, if supplied, have the following meanings:-a aname- The words are assigned to sequential indices of the array variable aname, starting at 0. All elements are removed from aname before the assignment. Other name arguments are ignored.
-d delim- The first character of delim is used to terminate the input line, rather than newline.
-e- Readline (see section 8 Command Line Editing) is used to obtain the line.
-n nchars-
readreturns after reading nchars characters rather than waiting for a complete line of input. -p prompt- Display prompt, without a trailing newline, before attempting to read any input. The prompt is displayed only if input is coming from a terminal.
-r- If this option is given, backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslash-newline pair may not be used as a line continuation.
-s- Silent mode. If input is coming from a terminal, characters are not echoed.
-t timeout-
Cause
readto time out and return failure if a complete line of input is not read within timeout seconds. This option has no effect ifreadis not reading input from the terminal or a pipe. -u fd- Read input from file descriptor fd.
shopt-
shopt [-pqsu] [-o] [optname ...]
Toggle the values of variables controlling optional shell behavior. With no options, or with the-poption, a list of all settable options is displayed, with an indication of whether or not each is set. The-poption causes output to be displayed in a form that may be reused as input. Other options have the following meanings:-s- Enable (set) each optname.
-u- Disable (unset) each optname.
-q-
Suppresses normal output; the return status
indicates whether the optname is set or unset.
If multiple optname arguments are given with
-q, the return status is zero if all optnames are enabled; non-zero otherwise. -o-
Restricts the values of
optname to be those defined for the
-ooption to thesetbuiltin (see section 4.3 The Set Builtin).
-sor-uis used with no optname arguments, the display is limited to those options which are set or unset, respectively. Unless otherwise noted, theshoptoptions 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 ofshoptoptions is:cdable_vars-
If this is set, an argument to the
cdbuiltin command that is not a directory is assumed to be the name of a variable whose value is the directory to change to. cdspell-
If set, minor errors in the spelling of a directory component in a
cdcommand 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- If this is set, Bash checks that a command found in the hash table exists before trying to execute it. If a hashed command no longer exists, a normal path search is performed.
checkwinsize-
If set, Bash checks the window size after each command
and, if necessary, updates the values of
LINESandCOLUMNS. cmdhist- If set, Bash attempts to save all lines of a multiple-line command in the same history entry. This allows easy re-editing of multi-line commands.
dotglob- If set, Bash includes filenames beginning with a `.' in the results of filename expansion.
execfail-
If this is set, a non-interactive shell will not exit if
it cannot execute the file specified as an argument to the
execbuiltin command. An interactive shell does not exit ifexecfails. expand_aliases- If set, aliases are expanded as described below under Aliases, section 6.6 Aliases. This option is enabled by default for interactive shells.
extdebug-
If set, behavior intended for use by debuggers is enabled:
-
The
-Foption to thedeclarebuiltin (see section 4.2 Bash Builtin Commands) displays the source file name and line number corresponding to each function name supplied as an argument. -
If the command run by the
DEBUGtrap returns a non-zero value, the next command is skipped and not executed. -
If the command run by the
DEBUGtrap returns a value of 2, and the shell is executing in a subroutine (a shell function or a shell script executed by the.orsourcebuiltins), a call toreturnis simulated. -
BASH_ARGCandBASH_ARGVare updated as described in their descriptions (see section 5.2 Bash Variables). -
Function tracing is enabled: command substitution, shell functions, and
subshells invoked with
( command )inherit theDEBUGandRETURNtraps. -
Error tracing is enabled: command substitution, shell functions, and
subshells invoked with
( command )inherit theERRORtrap.
-
The
extglob- If set, the extended pattern matching features described above (see section 3.5.8.1 Pattern Matching) are enabled.
extquote-
If set,
$'string'and$"string"quoting is performed within${parameter}expansions enclosed in double quotes. This option is enabled by default. failglob- If set, patterns which fail to match filenames during pathname expansion result in an expansion error.
force_fignore-
If set, the suffixes specified by the
FIGNOREshell variable cause words to be ignored when performing word completion even if the ignored words are the only possible completions. See section 5.2 Bash Variables, for a description ofFIGNORE. This option is enabled by default. gnu_errfmt- If set, shell error messages are written in the standard GNU error message format.
histappend-
If set, the history list is appended to the file named by the value
of the
HISTFILEvariable when the shell exits, rather than overwriting the file. histreedit- If set, and Readline is being used, a user is given the opportunity to re-edit a failed history substitution.
histverify- If set, and Readline is being used, the results of history substitution are not immediately passed to the shell parser. Instead, the resulting line is loaded into the Readline editing buffer, allowing further modification.
hostcomplete- If set, and Readline is being used, Bash will attempt to perform hostname completion when a word containing a ‘@’ is being completed (see section 8.4.6 Letting Readline Type For You). This option is enabled by default.
huponexit-
If set, Bash will send
SIGHUPto all jobs when an interactive login shell exits (see section 3.7.6 Signals). interactive_comments- Allow a word beginning with ‘#’ to cause that word and all remaining characters on that line to be ignored in an interactive shell. This option is enabled by default.
lithist-
If enabled, and the
cmdhistoption is enabled, multi-line commands are saved to the history with embedded newlines rather than using semicolon separators where possible. login_shell- The shell sets this option if it is started as a login shell (see section 6.1 Invoking Bash). The value may not be changed.
mailwarn-
If set, and a file that Bash is checking for mail has been
accessed since the last time it was checked, the message
"The mail in mailfile has been read"is displayed. no_empty_cmd_completion-
If set, and Readline is being used, Bash will not attempt to search
the
PATHfor possible completions when completion is attempted on an empty line. nocaseglob- If set, Bash matches filenames in a case-insensitive fashion when performing filename expansion.
nocasematch-
If set, Bash matches patterns in a case-insensitive fashion when
performing matching while executing
caseor[[conditional commands. nullglob- If set, Bash allows filename patterns which match no files to expand to a null string, rather than themselves.
progcomp- If set, the programmable completion facilities (see section 8.6 Programmable Completion) are enabled. This option is enabled by default.
promptvars- If set, prompt strings undergo parameter expansion, command substitution, arithmetic expansion, and quote removal after being expanded as described below (see section 6.9 Controlling the Prompt). This option is enabled by default.
restricted_shell- The shell sets this option if it is started in restricted mode (see section 6.10 The Restricted Shell). The value may not be changed. This is not reset when the startup files are executed, allowing the startup files to discover whether or not a shell is restricted.
shift_verbose-
If this is set, the
shiftbuiltin prints an error message when the shift count exceeds the number of positional parameters. sourcepath-
If set, the
sourcebuiltin uses the value ofPATHto find the directory containing the file supplied as an argument. This option is enabled by default. xpg_echo-
If set, the
echobuiltin expands backslash-escape sequences by default.
source-
source filename
A synonym for.(see section 4.1 Bourne Shell Builtins). type-
type [-afptP] [name ...]
For each name, indicate how it would be interpreted if used as a command name. If the-toption is used,typeprints 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, andtypereturns a failure status. If the-poption is used,typeeither returns the name of the disk file that would be executed, or nothing if-twould not return ‘file’. The-Poption forces a path search for each name, even if-twould not return ‘file’. If a command is hashed,-pand-Pprint the hashed value, not necessarily the file that appears first in$PATH. If the-aoption is used,typereturns all of the places that contain an executable named file. This includes aliases and functions, if and only if the-poption is not also used. If the-foption is used,typedoes not attempt to find shell functions, as with thecommandbuiltin. The return status is zero if any of the names are found, non-zero if none are found. typeset-
typeset [-afFrxi] [-p] [name[=value] ...]
Thetypesetcommand is supplied for compatibility with the Korn shell; however, it has been deprecated in favor of thedeclarebuiltin command. ulimit-
ulimit [-acdefilmnpqrstuvxSH] [limit]
ulimitprovides 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.
-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.
-e- The maximum scheduling priority ("nice").
-f- The maximum size of files written by the shell and its children.
-i- The maximum number of pending signals.
-l- The maximum size that may be locked into memory.
-m- The maximum resident set size.
-n- The maximum number of open file descriptors.
-p- The pipe buffer size.
-q- The maximum number of bytes in POSIX message queues.
-r- The maximum real-time scheduling priority.
-s- The maximum stack size.
-t- The maximum amount of cpu time in seconds.
-u- The maximum number of processes available to a single user.
-v- The maximum amount of virtual memory available to the process.
-x- The maximum number of file locks.
hard,soft, andunlimitedstand 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-Hoption is supplied. When setting new limits, if neither-Hnor-Sis supplied, both the hard and soft limits are set. If no option is given, then-fis assumed. Values are in 1024-byte increments, except for-t, which is in seconds,-p, which is in units of 512-byte blocks, and-nand-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-ais supplied, all aliases are removed. Aliases are described in section 6.6 Aliases.
| ISBN 0954161777 | GNU Bash Reference Manual | See the print edition |