Bourne Shell Builtins (Bash Reference Manual)
Next: Bash Builtins, Up: Shell Builtin Commands [Contents][Index]
4.1 Bourne Shell Builtins
The following shell builtin commands are inherited from the Bourne Shell. These commands are implemented as specified by the POSIX standard.
: (a colon): [arguments]
Do nothing beyond expanding
argumentsand performing redirections. The return status is zero.. (a period). filename [arguments]
Read and execute commands from the
filenameargument in the current shell context. Iffilenamedoes not contain a slash, thePATHvariable is used to findfilename. When Bash is not in POSIX mode, the current directory is searched iffilenameis not found in$PATH. If anyargumentsare supplied, they become the positional parameters whenfilenameis executed. Otherwise the positional parameters are unchanged. If the-Toption is enabled,sourceinherits any trap onDEBUG; if it is not, anyDEBUGtrap string is saved and restored around the call tosource, andsourceunsets theDEBUGtrap while it executes. If-Tis not set, and the sourced file changes theDEBUGtrap, the new value is retained whensourcecompletes. The return status is the exit status of the last command executed, or zero if no commands are executed. Iffilenameis not found, or cannot be read, the return status is non-zero. This builtin is equivalent tosource.breakbreak [n]
Exit from a
for,while,until, orselectloop. Ifnis supplied, thenth enclosing loop is exited.nmust be greater than or equal to 1. The return status is zero unlessnis not greater than or equal to 1.cdcd [-L|[-P [-e]] [-@] [directory]
Change the current working directory to
directory. Ifdirectoryis not supplied, the value of theHOMEshell variable is used. Any additional arguments followingdirectoryare ignored. If the shell variableCDPATHexists, it is used as a search path: each directory name inCDPATHis searched fordirectory, with alternative directory names inCDPATHseparated by a colon (‘:’). Ifdirectorybegins with a slash,CDPATHis not used.The
-Poption means to not follow symbolic links: symbolic links are resolved whilecdis traversingdirectoryand before processing an instance of ‘..’ indirectory.By default, or when the
-Loption is supplied, symbolic links indirectoryare resolved aftercdprocesses an instance of ‘..’ indirectory.If ‘
..’ appears indirectory, it is processed by removing the immediately preceding pathname component, back to a slash or the beginning ofdirectory.If the
-eoption is supplied with-Pand the current working directory cannot be successfully determined after a successful directory change,cdwill return an unsuccessful status.On systems that support it, the
-@option presents the extended attributes associated with a file as a directory.If
directoryis ‘-’, it is converted to$OLDPWDbefore the directory change is attempted.If a non-empty directory name from
CDPATHis used, or if ‘-’ is the first argument, and the directory change is successful, the absolute pathname of the new working directory is written to the standard output.The return status is zero if the directory is successfully changed, non-zero otherwise.
continuecontinue [n]
Resume the next iteration of an enclosing
for,while,until, orselectloop. Ifnis supplied, the execution of thenth enclosing loop is resumed.nmust be greater than or equal to 1. The return status is zero unlessnis not greater than or equal to 1.evaleval [arguments]
The arguments are concatenated together into a single command, which is then read and executed, and its exit status returned as the exit status of
eval. If there are no arguments or only empty arguments, the return status is zero.execexec [-cl] [-a name] [command [arguments]]
If
commandis supplied, it replaces the shell without creating a new process. If the-loption is supplied, the shell places a dash at the beginning of the zeroth argument passed tocommand. This is what theloginprogram does. The-coption causescommandto be executed with an empty environment. If-ais supplied, the shell passesnameas the zeroth argument tocommand. Ifcommandcannot be executed for some reason, a non-interactive shell exits, unless theexecfailshell option is enabled. In that case, it returns failure. An interactive shell returns failure if the file cannot be executed. A subshell exits unconditionally ifexecfails. If nocommandis 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.exitexit [n]
Exit the shell, returning a status of
nto the shell’s parent. Ifnis omitted, the exit status is that of the last command executed. Any trap onEXITis executed before the shell terminates.exportexport [-fn] [-p] [name[=value]]
Mark each
nameto be passed to child processes in the environment. If the-foption is supplied, thenames refer to shell functions; otherwise the names refer to shell variables. The-noption means to no longer mark eachnamefor export. If nonamesare supplied, or if the-poption is given, a list of names of all exported variables is displayed. The-poption displays output in a form that may be reused as input. If a variable name is followed by =value, the value of the variable is set tovalue.The return status is zero unless an invalid option is supplied, one of the names is not a valid shell variable name, or
-fis supplied with a name that is not a shell function.getoptsgetopts optstring name [arg …]
getoptsis used by shell scripts to parse positional parameters.optstringcontains 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 whitespace. The colon (‘:’) and question mark (‘?’) may not be used as option characters. Each time it is invoked,getoptsplaces the next option in the shell variablename, initializingnameif it does not exist, and the index of the next argument to be processed into the variableOPTIND.OPTINDis initialized to 1 each time the shell or a shell script is invoked. When an option requires an argument,getoptsplaces that argument into the variableOPTARG. The shell does not resetOPTINDautomatically; it must be manually reset between multiple calls togetoptswithin the same shell invocation if a new set of parameters is to be used.When the end of options is encountered,
getoptsexits with a return value greater than zero.OPTINDis set to the index of the first non-option argument, andnameis set to ‘?’.getoptsnormally parses the positional parameters, but if more arguments are supplied asargvalues,getoptsparses those instead.getoptscan report errors in two ways. If the first character ofoptstringis a colon,silenterror reporting is used. In normal operation, diagnostic messages are printed when invalid options or missing option arguments are encountered. If the variableOPTERRis set to 0, no error messages will be displayed, even if the first character ofoptstringis not a colon.If an invalid option is seen,
getoptsplaces ‘?’ intonameand, if not silent, prints an error message and unsetsOPTARG. Ifgetoptsis silent, the option character found is placed inOPTARGand no diagnostic message is printed.If a required argument is not found, and
getoptsis not silent, a question mark (‘?’) is placed inname,OPTARGis unset, and a diagnostic message is printed. Ifgetoptsis silent, then a colon (‘:’) is placed innameandOPTARGis set to the option character found.hashhash [-r] [-p filename] [-dt] [name]
Each time
hashis invoked, it remembers the full pathnames of the commands specified asnamearguments, so they need not be searched for on subsequent invocations. The commands are found by searching through the directories listed in$PATH. Any previously-remembered pathname is discarded. The-poption inhibits the path search, andfilenameis used as the location ofname. The-roption causes the shell to forget all remembered locations. The-doption causes the shell to forget the remembered location of eachname. If the-toption is supplied, the full pathname to which eachnamecorresponds is printed. If multiplenamearguments are supplied with-t, thenameis printed before the hashed full pathname. The-loption causes output to be displayed in a format that may be reused as input. If no arguments are given, or if only-lis supplied, information about remembered commands is printed. The return status is zero unless anameis not found or an invalid option is supplied.pwdpwd [-LP]
Print the absolute pathname of the current working directory. If the
-Poption is supplied, the pathname printed will not contain symbolic links. If the-Loption is supplied, the pathname printed may contain symbolic links. The return status is zero unless an error is encountered while determining the name of the current directory or an invalid option is supplied.readonlyreadonly [-aAf] [-p] [name[=value]] …
Mark each
nameas readonly. The values of these names may not be changed by subsequent assignment. If the-foption is supplied, eachnamerefers to a shell function. The-aoption means eachnamerefers to an indexed array variable; the-Aoption means eachnamerefers to an associative array variable. If both options are supplied,-Atakes precedence. If nonamearguments are given, or if the-poption is supplied, a list of all readonly names is printed. The other options may be used to restrict the output to a subset of the set of readonly names. The-poption causes output to be displayed in a format that may be reused as input. If a variable name is followed by =value, the value of the variable is set tovalue. The return status is zero unless an invalid option is supplied, one of thenamearguments is not a valid shell variable or function name, or the-foption is supplied with a name that is not a shell function.returnreturn [n]
Cause a shell function to stop executing and return the value
nto its caller. Ifnis not supplied, the return value is the exit status of the last command executed in the function. Ifreturnis executed by a trap handler, the last command used to determine the status is the last command executed before the trap handler. Ifreturnis executed during aDEBUGtrap, the last command used to determine the status is the last command executed by the trap handler beforereturnwas invoked.returnmay also be used to terminate execution of a script being executed with the.(source) builtin, returning eithernor the exit status of the last command executed within the script as the exit status of the script. Ifnis supplied, the return value is its least significant 8 bits. Any command associated with theRETURNtrap is executed before execution resumes after the function or script. The return status is non-zero ifreturnis supplied a non-numeric argument or is used outside a function and not during the execution of a script by.orsource.shiftshift [n]
Shift the positional parameters to the left by
n. The positional parameters fromn+1 …$#are renamed to$1…$#-n. Parameters represented by the numbers$#down to$#-n+1 are unset.nmust be a non-negative number less than or equal to$#. Ifnis zero or greater than$#, the positional parameters are not changed. Ifnis not supplied, it is assumed to be 1. The return status is zero unlessnis greater than$#or less than zero, non-zero otherwise.test
[test expr
Evaluate a conditional expression
exprand return a status of 0 (true) or 1 (false). Each operator and operand must be a separate argument. Expressions are composed of the primaries described below in Bash Conditional Expressions.testdoes not accept any options, nor does it accept and ignore an argument of--as signifying the end of options.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. The evaluation depends on the number of arguments; see below. Operator precedence is used when there are five or more arguments.
! exprTrue if
expris false.( expr )Returns the value of
expr. This may be used to override the normal precedence of operators.expr1 -a expr2True if both
expr1andexpr2are true.expr1 -o expr2True if either
expr1orexpr2is true.
The
testand[builtins evaluate conditional expressions using a set of rules based on the number of arguments.- 0 arguments
The expression is false.
- 1 argument
The expression is true if, and only if, the argument is not null.
- 2 arguments
If the first argument is ‘
!’, the expression is true if and only if the second argument is null. If the first argument is one of the unary conditional operators (see Bash Conditional Expressions), the expression is true if the unary test is true. If the first argument is not a valid unary operator, the expression is false.- 3 arguments
The following conditions are applied in the order listed.
- If the second argument is one of the binary conditional operators (see Bash Conditional Expressions), the result of the expression is the result of the binary test using the first and third arguments as operands. The ‘
-a’ and ‘-o’ operators are considered binary operators when there are three arguments. - If the first argument is ‘
!’, the value is the negation of the two-argument test using the second and third arguments. - If the first argument is exactly ‘
(’ and the third argument is exactly ‘)’, the result is the one-argument test of the second argument. - Otherwise, the expression is false.
- If the second argument is one of the binary conditional operators (see Bash Conditional Expressions), the result of the expression is the result of the binary test using the first and third arguments as operands. The ‘
- 4 arguments
If the first argument is ‘
!’, the result is the negation of the three-argument expression composed of the remaining arguments. Otherwise, the expression is parsed and evaluated according to precedence using the rules listed above.- 5 or more arguments
The expression is parsed and evaluated according to precedence using the rules listed above.
When used with
testor ‘[’, the ‘<’ and ‘>’ operators sort lexicographically using ASCII ordering.timestimes
Print out the user and system times used by the shell and its children. The return status is zero.
traptrap [-lp] [arg] [sigspec …]
The commands in
argare to be read and executed when the shell receives signalsigspec. Ifargis absent (and there is a singlesigspec) or equal to ‘-’, each specified signal’s disposition is reset to the value it had when the shell was started. Ifargis the null string, then the signal specified by eachsigspecis ignored by the shell and commands it invokes. Ifargis not present and-phas been supplied, the shell displays the trap commands associated with eachsigspec. If no arguments are supplied, or only-pis given,trapprints the list of commands associated with each signal number in a form that may be reused as shell input. The-loption causes the shell to print a list of signal names and their corresponding numbers. Eachsigspecis either a signal name or a signal number. Signal names are case insensitive and theSIGprefix is optional.If a
sigspecis0orEXIT,argis executed when the shell exits. If asigspecisDEBUG, the commandargis executed before every simple command,forcommand,casecommand,selectcommand, every arithmeticforcommand, and before the first command executes in a shell function. Refer to the description of theextdebugoption to theshoptbuiltin (see The Shopt Builtin) for details of its effect on theDEBUGtrap. If asigspecisRETURN, the commandargis executed each time a shell function or a script executed with the.orsourcebuiltins finishes executing.If a
sigspecisERR, the commandargis executed whenever a pipeline (which may consist of a single simple command), a list, or a compound command returns a non-zero exit status, subject to the following conditions. TheERRtrap is not executed if the failed command is part of the command list immediately following anuntilorwhilekeyword, part of the test following theiforelifreserved words, part of a command executed in a&&or||list except the command following the final&&or||, any command in a pipeline but the last, or if the command’s return status is being inverted using!. These are the same conditions obeyed by theerrexit(-e) option.Signals ignored upon entry to the shell cannot be trapped or reset. Trapped signals that are not being ignored are reset to their original values in a subshell or subshell environment when one is created.
The return status is zero unless a
sigspecdoes not specify a valid signal.umaskumask [-p] [-S] [mode]
Set the shell process’s file creation mask to
mode. Ifmodebegins with a digit, it is interpreted as an octal number; if not, it is interpreted as a symbolic mode mask similar to that accepted by thechmodcommand. Ifmodeis omitted, the current value of the mask is printed. If the-Soption is supplied without amodeargument, the mask is printed in a symbolic format. If the-poption is supplied, andmodeis 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 nomodeargument 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 of022results in permissions of755.unsetunset [-fnv] [name]
Remove each variable or function
name. If the-voption is given, eachnamerefers to a shell variable and that variable is removed. If the-foption is given, thenames refer to shell functions, and the function definition is removed. If the-noption is supplied, andnameis a variable with thenamerefattribute,namewill be unset rather than the variable it references.-nhas no effect if the-foption is supplied. If no options are supplied, eachnamerefers to a variable; if there is no variable by that name, a function with that name, if any, is unset. Readonly variables and functions may not be unset. Some shell variables lose their special behavior if they are unset; such behavior is noted in the description of the individual variables. The return status is zero unless anameis readonly.
Next: Bash Builtins, Up: Shell Builtin Commands [Contents][Index]