Bash Built-In Variables: Difference between revisions
(→PPID) |
(→PPID) |
||
Line 81: | Line 81: | ||
IFS characters are used to split the line processed by [[bash read|read]] into words. | IFS characters are used to split the line processed by [[bash read|read]] into words. | ||
=PIPESTATUS= | |||
See: {{Internal|Bash_Concepts#Pipeline|Pipeline}} | |||
=<span id='.24PPID'></span>PPID= | =<span id='.24PPID'></span>PPID= | ||
Revision as of 06:46, 31 December 2020
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
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_VERSION
BASH_ENV
An environment variable pointing to a bash startup file to read when the script is invoked.
BASH_SUBSHELL
A variable indicating the subshell level. Introduced in bash 3. The main bash process reports 0. If a function is invoked with $(...), it will report BASH_SUBSHELL=1. If another function is invoked with $(...) from it, it will report BASH_SUBSHELL=2, and so on.
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 subshells with (...), whereas BASHPID has different values, depending on the subshell 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.
PIPESTATUS
See:
PPID
The process ID of the shell's parent. This variable is readonly.
PS1
FUNCNAME
An array containing the names of all shell functions currently in the execution call stack. Invoked as ${FUNCNAME} returns the name of the current function. See:
OLDPWD
Contains directory before the last cd command.
cd directory echo $OLDPWD /home/user