BASH MAN PAGE (Part 3)
Navigation
This man page has been split into 7 pages:
bash(1)
- NAME
- SYNOPSIS
- COPYRIGHT
- DESCRIPTION
- OPTIONS
- ARGUMENTS
- INVOCATION
bash(1)Part2
- DEFINITIONS
- RESERVED WORDS
- SHELL GRAMMAR
- COMMENTS
- QUOTING
Part 3
bash(1)Part4
bash(1)Part5
- REDIRECTION
- ALIASES
- FUNCTIONS
- ARITHMETIC EVALUATION
- CONDITIONAL EXPRESSIONS
- SIMPLE COMMAND EXPANSION
- COMMAND EXECUTION
- COMMAND EXECUTION ENVIRONMENT
- ENVIRONMENT
- EXIT STATUS
- SIGNALS
- JOB CONTROL
- PROMPTING
bash(1)Part6
- READLINE
- HISTORY
- HISTORY EXPANSION
bash(1)Part7
- SHELL BUILTIN COMMANDS
- RESTRICTED SHELL
- SEE ALSO
- FILES
- AUTHORS
- BUG REPORTS
- BUGS
PARAMETERS
A parameter is an entity that stores values. It can be a name , a number, or one of the special characters listed below under Special Parameters . For the shell's purposes, a variable is a parameter denoted by a name .
A parameter is set if it has been assigned a value. The null string is a valid value. Once a variable is set, it may be unset only by using the unset builtin command (see SHELL BUILTIN COMMANDS below).
A variable may be assigned to by a statement of the form
name=[''value''?
If value is not given, the variable is assigned the null string. All values undergo tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and quote removal (see EXPANSION below). If the variable has its integer attribute set (see declare below in SHELL BUILTIN COMMANDS ) then value is subject to arithmetic expansion even if the $((...)) expansion is not used (see Arithmetic Expansion below). Word splitting is not performed, with the exception of "$@" as explained below under Special Parameters . Pathname expansion is not performed.
Positional Parameters
A positional parameter is a parameter denoted by one or more digits, other than the single digit 0. Positional parameters are assigned from the shell's arguments when it is invoked, and may be reassigned using the set builtin command. Positional parameters may not be assigned to with assignment statements. The positional parameters are temporarily replaced when a shell function is executed (see FUNCTIONS below).
When a positional parameter consisting of more than a single digit is expanded, it must be enclosed in braces (see EXPANSION below).
Special Parameters
The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.
- *
- Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.
- @
- Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).
- #
- Expands to the number of positional parameters in decimal.
- ?
- Expands to the status of the most recently executed foreground pipeline.
- -
- Expands to the current option flags as specified upon invocation, by the set builtin command, or those set by the shell itself (such as the -i option).
- $
- Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the subshell.
- !
- Expands to the process ID of the most recently executed background (asynchronous) command.
- 0
- Expands to the name of the shell or shell script. This is set at shell initialization. If bash is invoked with a file of commands, $0 is set to the name of that file. If bash is started with the -c option, then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the file name used to invoke bash , as given by argument zero.
- _
- At shell startup, set to the absolute file name of the shell or shell script being executed as passed in the argument list. Subsequently, expands to the last argument to the previous command, after expansion. Also set to the full file name of each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file currently being checked.
Shell Variables
The following variables are set by the shell:
- PPID
- The process ID of the shell's parent. This variable is readonly.
- PWD
- The current working directory as set by the cd command.
- OLDPWD
- The previous working directory as set by the cd command.
- REPLY
- Set to the line of input read by the read builtin command when no arguments are supplied.
- UID
- Expands to the user ID of the current user, initialized at shell startup. This variable is readonly.
- EUID
- Expands to the effective user ID of the current user, initialized at shell startup. This variable is readonly.
- GROUPS
- An array variable containing the list of groups of which the current user is a member. Assignments to GROUPS have no effect and return an error status. If GROUPS is unset, it loses its special properties, even if it is subsequently reset.
- BASH
- Expands to the full file name used to invoke this instance of bash .
- BASH_VERSION
- Expands to a string describing the version of this instance of bash .
- BASH_VERSINFO
- A readonly array variable whose members hold version information for this instance of bash . The values assigned to the array members are as follows:
- BASH_VERSINFO[0?
- The major version number (the release).
- BASH_VERSINFO[1?
- The minor version number (the version).
- BASH_VERSINFO[2?
- The patch level.
- BASH_VERSINFO[3?
- The build version.
- BASH_VERSINFO[4?
- The release status (e.g., beta1).
- BASH_VERSINFO[5?
- The value of MACHTYPE.
- SHLVL
- Incremented by one each time an instance of bash is started.
- RANDOM
- Each time this parameter is referenced, a random integer between 0 and 32767 is generated. The sequence of random numbers may be initialized by assigning a value to RANDOM . If RANDOM is unset, it loses its special properties, even if it is subsequently reset.
- SECONDS
- Each time this parameter is referenced, the number of seconds since shell invocation is returned. If a value is assigned to SECONDS , the value returned upon subsequent references is the number of seconds since the assignment plus the value assigned. If SECONDS is unset, it loses its special properties, even if it is subsequently reset.
- LINENO
- Each time this parameter is referenced, the shell substitutes a decimal number representing the current sequential line number (starting with 1) within a script or function. When not in a script or function, the value substituted is not guaranteed to be meaningful. If LINENO is unset, it loses its special properties, even if it is subsequently reset.
- 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.
- FUNCNAME
- The name of any currently-executing shell function. This variable exists only when a shell function is executing. Assignments to FUNCNAME have no effect and return an error status. If FUNCNAME is unset, it loses its special properties, even if it is subsequently reset.
- DIRSTACK
- An array variable (see Arrays below) containing the current contents of the directory stack. Directories appear in the stack in the order they are displayed by the dirs builtin. Assigning to members of this array variable may be used to modify directories already in the stack, but the pushd and popd builtins must be used to add and remove directories. Assignment to this variable will not change the current directory. If DIRSTACK is unset, it loses its special properties, even if it is subsequently reset.
- PIPESTATUS
- An array variable (see Arrays below) containing a list of exit status values from the processes in the most-recently-executed foreground pipeline (which may contain only a single command).
- OPTARG
- The value of the last option argument processed by the getopts builtin command (see SHELL BUILTIN COMMANDS below).
- OPTIND
- The index of the next argument to be processed by the getopts builtin command (see SHELL BUILTIN COMMANDS below).
- HOSTNAME
- Automatically set to the name of the current host.
- HOSTTYPE
- Automatically set to a string that uniquely describes the type of machine on which bash is executing. The default is system-dependent.
- OSTYPE
- Automatically set to a string that describes the operating system on which bash is executing. The default is system-dependent.
- MACHTYPE
- Automatically set to a string that fully describes the system type on which bash is executing, in the standard GNU cpu-company-system format. The default is system-dependent.
- SHELLOPTS
- A colon-separated list of enabled shell options. Each word in the list is a valid argument for the -o option to the set builtin command (see SHELL BUILTIN COMMANDS below). The options appearing in SHELLOPTS are those reported as on by set -o. If this variable is in the environment when bash starts up, each shell option in the list will be enabled before reading any startup files. This variable is read-only.
- COMP_WORDS
- An array variable (see Arrays below) consisting of the individual words in the current command line. This variable is available only in shell functions invoked by the programmable completion facilities (see Programmable Completion below).
- COMP_CWORD
- An index into ${COMP_WORDS} of the word containing the current cursor position. This variable is available only in shell functions invoked by the programmable completion facilities (see Programmable Completion below).
- COMP_LINE
- The current command line. This variable is available only in shell functions and external commands invoked by the programmable completion facilities (see Programmable Completion below).
- COMP_POINT
- The index of the current cursor position relative to the beginning of the current command. If the current cursor position is at the end of the current command, the value of this variable is equal to ${#COMP_LINE}. This variable is available only in shell functions and external commands invoked by the programmable completion facilities (see Programmable Completion below).
The following variables are used by the shell. In some cases, bash assigns a default value to a variable; these cases are noted below.
- IFS
- The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is ``<space><tab><newline>".
- PATH
- The search path for commands. It is a colon-separated list of directories in which the shell looks for commands (see COMMAND EXECUTION below). The default path is system-dependent, and is set by the administrator who installs bash . A common value is
"/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin:.".
- HOME
- The home directory of the current user; the default argument for the cd builtin command. The value of this variable is also used when performing tilde expansion.
- CDPATH
- The search path for the cd command. This is a colon-separated list of directories in which the shell looks for destination directories specified by the cd command. A sample value is ".::/usr".
- BASH_ENV
- If this parameter is set when bash is executing a shell script, its value is interpreted as a filename containing commands to initialize the shell, as in /.bashrc . The value of BASH_ENV is subjected to parameter expansion, command substitution, and arithmetic expansion before being interpreted as a file name. PATH is not used to search for the resultant file name.
- MAIL
- If this parameter is set to a file name and the MAILPATH variable is not set, bash informs the user of the arrival of mail in the specified file.
- MAILCHECK
- Specifies how often (in seconds) bash checks for mail. The default is 60 seconds. When it is time to check for mail, the shell does so before displaying the primary prompt. If this variable is unset, or set to a value that is not a number greater than or equal to zero, the shell disables mail checking.
- MAILPATH
- A colon-separated list of file names to be checked for mail. The message to be printed when mail arrives in a particular file may be specified by separating the file name from the message with a `?'. When used in the text of the message, $_ expands to the name of the current mailfile. Example:
MAILPATH='/var/mail/bfox?"You have mail":/shell-mail?"$_ has mail!"'
Bash supplies a default value for this variable, but the location of the user mail files that it uses is system dependent (e.g., /var/mail/$USER).
- PS1
- The value of this parameter is expanded (see PROMPTING below) and used as the primary prompt string. The default value is ``\s-\v\$ ".
- PS2
- The value of this parameter is expanded as with PS1 and used as the secondary prompt string. The default is ``> ".
- PS3
- The value of this parameter is used as the prompt for the select command (see SHELL GRAMMAR above).
- PS4
- The value of this parameter is expanded as with PS1 and the value is printed before each command bash displays during an execution trace. The first character of PS4 is replicated multiple times, as necessary, to indicate multiple levels of indirection. The default is "+ ".
- TIMEFORMAT
- The value of this parameter is used as a format string specifying how the timing information for pipelines prefixed with the time reserved word should be displayed. The % character introduces an escape sequence that is expanded to a time value or other information. The escape sequences and their meanings are as follows; the braces denote optional portions.
- %%
- A literal %.
- %[''p''?[l?R
- The elapsed time in seconds.
- %[''p''?[l?U
- The number of CPU seconds spent in user mode.
- %[''p''?[l?S
- The number of CPU seconds spent in system mode.
- %P
- The CPU percentage, computed as (%U + %S) / %R.
- The optional p is a digit specifying the precision, the number of fractional digits after a decimal point. A value of 0 causes no decimal point or fraction to be output. At most three places after the decimal point may be specified; values of p greater than 3 are changed to 3. If p is not specified, the value 3 is used.
- The optional l specifies a longer format, including minutes, of the form MMmSS.FFs. The value of p determines whether or not the fraction is included.
- If this variable is not set, bash acts as if it had the value $'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'. If the value is null, no timing information is displayed. A trailing newline is added when the format string is displayed.
- HISTSIZE
- The number of commands to remember in the command history (see HISTORY below). The default value is 500.
- HISTFILE
- The name of the file in which command history is saved (see HISTORY below). The default value is /.bash_history. If unset, the command history is not saved when an interactive shell exits.
- HISTFILESIZE
- The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated, if necessary, to contain no more than that number of lines. The default value is 500. The history file is also truncated to this size after writing it when an interactive shell exits.
- OPTERR
- If set to the value 1, bash displays error messages generated by the getopts builtin command (see SHELL BUILTIN COMMANDS below). OPTERR is initialized to 1 each time the shell is invoked or a shell script is executed.
- LANG
- Used to determine the locale category for any category not specifically selected with a variable starting with LC_.
- LC_ALL
- This variable overrides the value of LANG and any other LC_ variable specifying a locale category.
- LC_COLLATE
- This variable determines the collation order used when sorting the results of pathname expansion, and determines the behavior of range expressions, equivalence classes, and collating sequences within pathname expansion and pattern matching.
- LC_CTYPE
- This variable determines the interpretation of characters and the behavior of character classes within pathname expansion and pattern matching.
- LC_MESSAGES
- This variable determines the locale used to translate double-quoted strings preceded by a $.
- LC_NUMERIC
- This variable determines the locale category used for number formatting.
- LINES
- Used by the select builtin command to determine the column length for printing selection lists. Automatically set upon receipt of a SIGWINCH.
- COLUMNS
- Used by the select builtin command to determine the terminal width when printing selection lists. Automatically set upon receipt of a SIGWINCH.
- PROMPT_COMMAND
- If set, the value is executed as a command prior to issuing each primary prompt.
- IGNOREEOF
- Controls the action of an interactive shell on receipt of an EOF character as the sole input. If set, the value is the number of consecutive EOF characters which must be typed as the first characters on an input line before bash exits. If the variable exists but does not have a numeric value, or has no value, the default value is 10. If it does not exist, EOF signifies the end of input to the shell.
- TMOUT
- If set to a value greater than zero, the value is interpreted as the number of seconds to wait for input after issuing the primary prompt. Bash terminates after waiting for that number of seconds if input does not arrive.
- FCEDIT
- The default editor for the fc builtin command.
- FIGNORE
- A colon-separated list of suffixes to ignore when performing filename completion (see READLINE below). A filename whose suffix matches one of the entries in FIGNORE is excluded from the list of matched filenames. A sample value is ".o:".
- GLOBIGNORE
- A colon-separated list of patterns defining the set of filenames to be ignored by pathname expansion. If a filename matched by a pathname expansion pattern also matches one of the patterns in GLOBIGNORE , it is removed from the list of matches.
- INPUTRC
- The filename for the readline startup file, overriding the default of /.inputrc (see READLINE below).
- HISTCONTROL
- If set to a value of ignorespace , lines which begin with a space character are not entered on the history list. If set to a value of ignoredups , lines matching the last history line are not entered. A value of ignoreboth combines the two options. If unset, or if set to any other value than those above, all lines read by the parser are saved on the history list, subject to the value of HISTIGNORE . This variable's function is superseded by HISTIGNORE . The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of HISTCONTROL .
- HISTIGNORE
- A colon-separated list of patterns used to decide which command lines should be saved on the history list. Each pattern is anchored at the beginning of the line and must match the complete line (no implicit `*' is appended). Each pattern is tested against the line after the checks specified by HISTCONTROL are applied. In addition to the normal shell pattern matching characters, `&' matches the previous history line. `&' may be escaped using a backslash; the backslash is removed before attempting a match. The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of HISTIGNORE .
- histchars
- The two or three characters which control history expansion and tokenization (see HISTORY EXPANSION below). The first character is the history expansion character, the character which signals the start of a history expansion, normally `!'. The second character is the quick substitution character, which is used as shorthand for re-running the previous command entered, substituting one string for another in the command. The default is `^'. The optional third character is the character which indicates that the remainder of the line is a comment when found as the first character of a word, normally `#'. 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.
- 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. The list of possible hostname completions may be changed while the shell is running; the next time hostname completion is attempted after the value is changed, bash adds the contents of the new file to the existing list. If HOSTFILE is set, but has no value, bash attempts to read /etc/hosts to obtain the list of possible hostname completions. When HOSTFILE is unset, the hostname list is cleared.
- auto_resume
- This variable controls how the shell interacts with the user and job control. If this variable is set, single word simple commands without redirections are treated as candidates for resumption of an existing stopped job. There is no ambiguity allowed; if there is more than one job beginning with the string typed, the job most recently accessed is selected. The name of a stopped job, in this context, is the command line used to start it. If set to the value exact , the string supplied must match the name of a stopped job exactly; if set to substring , the string supplied needs to match a substring of the name of a stopped job. The substring value provides functionality analogous to the %? job identifier (see JOB CONTROL below). If set to any other value, the supplied string must be a prefix of a stopped job's name; this provides functionality analogous to the % job identifier.
- COMPREPLY
- An array variable from which bash reads the possible completions generated by a shell function invoked by the programmable completion facility (see Programmable Completion below).
Arrays
Bash provides one-dimensional array variables. Any variable may be used as an array; the declare builtin will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Arrays are indexed using integers and are zero-based.
An array is created automatically if any variable is assigned to using the syntax name[''subscript''?=value. The subscript is treated as an arithmetic expression that must evaluate to a number greater than or equal to zero. To explicitly declare an array, use declare -a name (see SHELL BUILTIN COMMANDS below). declare -a name[''subscript''? is also accepted; the subscript is ignored. Attributes may be specified for an array variable using the declare and readonly builtins. Each attribute applies to all members of an array.
Arrays are assigned to using compound assignments of the form name(value1 ... valuen), where each value is of the form [''subscript''?string. Only string is required. If the optional brackets and subscript are supplied, that index is assigned to; otherwise the index of the element assigned is the last index assigned to by the statement plus one. Indexing starts at zero. This syntax is also accepted by the declare builtin. Individual array elements may be assigned to using the name[''subscript''?=value syntax introduced above.
Any element of an array may be referenced using ${name[''subscript''?}. The braces are required to avoid conflicts with pathname expansion. If subscript is @ or , the word expands to all members of name. These subscripts differ only when the word appears within double quotes. If the word is double-quoted, ${name[*?} expands to a single word with the value of each array member separated by the first character of the IFS special variable, and ${name[@?} expands each element of name to a separate word. When there are no array members, ${name[@?} expands to nothing. This is analogous to the expansion of the special parameters and @ (see Special Parameters above). ${#name[''subscript''?} expands to the length of ${name[''subscript''?}. If subscript is * or @, the expansion is the number of elements in the array. Referencing an array variable without a subscript is equivalent to referencing element zero.
The unset builtin is used to destroy arrays. unset name[''subscript''? destroys the array element at index subscript. unset name, where name is an array, or unset name[''subscript''?, where subscript is * or @, removes the entire array.
The declare , local , and readonly builtins each accept a -a option to specify an array. The read builtin accepts a -a option to assign a list of words read from the standard input to an array. The set and declare builtins display array values in a way that allows them to be reused as assignments.