Bash Built-In Variables
External
- GNU manual, bash variables: https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#Bash-Variables
- bash Internal Variables http://www.tldp.org/LDP/abs/html/internalvariables.html
- http://answers.oreilly.com/topic/1498-built-in-bash-shell-variables/
Internal
Overview
This page documents the variables that have a special meaning to shell and that are expanded to execution environment-dependedent values. They area also known as internal variables.
IFS
IFS is the internal field separator. This variable determines how bash recognizes fields (word boundaries) when it interprets character strings. IFS defaults to whitespace (space, tab and newline). This is the proof:
echo "$IFS" | cat -vte ^I$ $
IFS can be changed. For example, this is how you set IFS so for iterates over lines:
IFS="$(printf '\n\r')"
- Note you must set IFS back to whitespace after setting it to something else, so the basic shell function work as expected. This is done as shown below: restoring the default IFS value.
Restoring the default IFS value
IFS="$(printf ' \t\n')"
IFS and for
for honors the value of IFS (default the space). If you set IFS to something else, before the for statement, for will use that as field separator while iterating over the list.
- Note be extremely careful when setting IFS before a for loop, even if you restore the default value after the loop: everything inside the loop will use the non-standard IFS value and it may not work as expected.
Also see:
IFS and read
IFS characters are used to split the line processed by read into words.
$*
$* expands to the positional parameters, starting from one. When the expansion occurs within double quotes, $* coalesces all individual parameters into a single parameter, by expanding them to a single word with the value of each parameter separated by the first character of the IFS special variable: "$*" 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.
Also see:
$@
$@ 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 individually quoted "$1" "$2" ...
If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing and they are removed.
man bash | grep -C2 '$@'
$?
Exit value of last executed command or function, in the interactive shell or within a script.
$$
The pid of the current process. Also see $BASHPID, which differs from $$ under certain circumstances, such as subshells that do not require bash to be re-initialized.
$PPID
The process ID of the shell's parent. This variable is readonly.
$#
The number of command line or a function's arguments.
$-
Options currently in effect (supplied on command line or to set). The shell sets some options automatically.
$
Process number of the shell.
$!
Process number (PID) of last background command.
$0
First word; that is, the command name. This will have the full pathname if it was found via a PATH search.
Positional Parameters $1, $2, ...
Individual arguments on command line (positional parameters).
bash allows only nine parameters to be referenced directly (n = 1–9) as in
$1
For n values greater than 9, the command line arguments must be specified as ${n}:
${10}
The positional parameters can be shifted left with the bash built-in command shift.
The positional parameters initialized on the command line of the process can be re-initialized with set.
Iterating over Positional Paramenters
Solution 1
while [ -n "$1" ]; do ... shift done
Solution 2
$_
Temporary variable; initialized to pathname of script or program being executed. Later, stores the last argument of previous command. Also stores name of matching MAIL file during mail checks.
Quoted String Expansion $'...'
This construct expands single or multiple escaped octal or hex values into ASCII or Unicode characters.
The following are equivalent:
$'\012' # Octal value $'\x0a' # Hexadecimal value $'\n'
The octal and hexadecimal values for ASCII characters are available here:
BASH_SOURCE
An array variable whose members are the source filenames where the corresponding shell function names in the FUNCNAME array variable are defined. The shell function ${FUNCNAME[$i]} is defined in the file ${BASH_SOURCE[$i]} and called from ${BASH_SOURCE[$i+1]}
${BASH_SOURCE[0]} is equivalent with $0, with the observation that ${BASH_SOURCE[0]} contains the (potentially relative) path of the containing script in all invocation scenarios, notably also when the script is sourced, which is not true for $0.
When the script is sourced, $0 contains "-bash", while ${BASH_SOURCE[0]} contains the path of the script.