Bash Built-In Variables: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 10: Line 10:
=Overview=
=Overview=


This page documents [[Bash_Parameters_and_Variables#Variables|variables]] that have a special meaning to the shell and are known as internal, keyword or built-in variables.
This page documents [[Bash_Parameters_and_Variables#Variables|variables]] that have a special meaning to the shell and are known as internal, keyword or built-in variables. For a list of special parameters, see <span id='.240'></span><span id='.24.2A'></span><span id='.24.40'></span><span id='.24.24'></span><span id='.24.3F'></span><span id='.24.23'></span><span id='.24-'></span><span id='.24'></span><span id='.24.21'></span><span id='.24_2'>[[Bash Special Parameters|bash Special Parameters]].


=BASH=
=BASH=

Revision as of 21:43, 18 September 2019

External

Internal

Overview

This page documents variables that have a special meaning to the shell and are known as internal, keyword or built-in variables. For a list of special parameters, see bash Special Parameters.

BASH

The path to the bash binary.

BASH_ENV

An environment variable pointing to a bash startup file to read when the script is invoked.

BASH_SUBSHELL

BASHPID

The process ID of the current instance of bash. It is different from $$ in that $$ always stay the same for a bash script execution, representing the process ID of the top level bash process, the script itself, even if invoked from sub-shells (...), whereas BASHPID has different values, depending on the sub-shell it is dereferenced into.

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.

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.

PPID

The process ID of the shell's parent. This variable is readonly.

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'

Example:

echo "something"$'\047'"something else"$'\047'

The octal and hexadecimal values for ASCII characters are available here:

ASCII

$!... Variable Indirection

bash Variable Indirection (Indirect Expansion)