Bash Built-In Variables: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(30 intermediate revisions by the same user not shown)
Line 7: Line 7:


* [[bash Parameters and Variables]]
* [[bash Parameters and Variables]]
* [[Bash Parameter and Variable Expansion|bash Parameter and Variable Expansion]]


=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=


The path to the bash binary.
The path to the bash binary.
=BASH_VERSION=


=BASH_ENV=
=BASH_ENV=
Line 21: Line 24:


=BASH_SUBSHELL=
=BASH_SUBSHELL=
A variable indicating the [[bash Concepts#Subshell|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=
=BASHPID=


The process ID of the current instance of bash. It is different from [[#.24.24|$$]] in that [[#.24.24|$$]] 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.
The process ID of the current instance of bash. It is different from [[#.24.24|$$]] in that [[#.24.24|$$]] 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 [[Bash_Concepts#Subshell|subshells]] with (...), whereas BASHPID has different values, depending on the [[Bash_Concepts#Subshell|subshell]] it is dereferenced into.


=BASH_SOURCE=
=<tt>BASH_SOURCE</tt>=


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]}
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]}
Line 34: Line 38:
When the script is sourced, $0 contains "-bash", while ${BASH_SOURCE[0]} contains the path of the script.
When the script is sourced, $0 contains "-bash", while ${BASH_SOURCE[0]} contains the path of the script.


=IFS=
=<tt>DIRSTACK</tt>=
{{Internal|Bash Directory Manipulation Built-in Commands#DIRSTACK|Directory Manipulation Built-in Commands}}
 
=<tt>IFS</tt>=


<tt>IFS</tt> is the internal field separator. This variable determines how bash recognizes ''fields'' (word boundaries) when it interprets character strings. <tt>IFS</tt> defaults to whitespace (space, tab and newline). This is the proof:
<tt>IFS</tt> is the internal field separator. This variable determines how bash recognizes ''fields'' (word boundaries) when it interprets character strings. <tt>IFS</tt> defaults to whitespace (space, tab and newline). This is the proof:
Line 53: Line 60:
:'''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]].<br>
:'''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]].<br>
</blockquote>
</blockquote>
<font color=darkkhaki>To research, <code>IFS=</code> seems equivalent with <code>IFS="$(printf '\n\r')"</code> in that it recognizes '\n' and '\r' as separators but not the space ' '. It did not work for me, but why it worked in examples?</font>


==Restoring the default IFS value==
==Restoring the default IFS value==


<pre>
<pre>
IFS="$(printf ' \t\n')"
IFS="$(printf ' \t\n\r')"
</pre>
</pre>


Line 76: Line 85:


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.
=PATH=
{{Internal|PATH#Overview|PATH}}
=PIPESTATUS=
See: {{Internal|Bash_Concepts#Pipeline|Pipeline}}


=<span id='.24PPID'></span>PPID=
=<span id='.24PPID'></span>PPID=
Line 81: Line 96:
The process ID of the shell's parent.  This variable is readonly.
The process ID of the shell's parent.  This variable is readonly.


=$*=
=PS1=
 
{{Internal|Bash Prompt|Bash Prompt}}
$* 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: {{Internal|Bash_set#set_and_Positional_Parameters|bash set and Positional Parameters}}
 
=$@=
 
$@ expands to the [[#Positional_Parameters_.241.2C_.242.2C_...|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 no double quotes are set around $@, then each space-bordered component is passed individually to the target function.
 
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 [[Bash_Functions#Exit_Status|function]], in the interactive shell or within a script.
 
=$$=
 
The pid of the current process. Also see [[#BASHPID|BASHPID]], which differs from $$ under certain circumstances, such as subshells that do not require bash to be re-initialized.
 
=$#=
 
The number of command line or a [[Bash_Functions#Arguments|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.
 
=$_=
 
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:
=FUNCNAME=


$'\012' # Octal value
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: {{Internal|Bash_Functions#The_FUNCNAME_Array|The FUNCNAME Array}}
$'\x0a' # Hexadecimal value
$'\n'


Example:
=OLDPWD=


echo "something"$'\047'"something else"$'\047'
Contains directory before the last cd command.


The octal and hexadecimal values for ASCII characters are available here: {{Internal|Common ASCII Codes#Overview|ASCII}}
cd directory
echo $OLDPWD
/home/user


=$!... Variable Indirection=
=LD_LIBRARY_PATH=
{{External|https://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html}}
{{Internal|Linux Shared Libraries|Linux Shared Libraries}}


{{Internal|bash Parameters and Variables#.24.21..._Variable_Indirection.2C_Indirect_Expansion|bash Variable Indirection (Indirect Expansion)}}
=LD_PRELOAD=
{{External|https://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html}}
{{Internal|Linux Shared Libraries|Linux Shared Libraries}}

Latest revision as of 00:08, 26 July 2023

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_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.

DIRSTACK

Directory Manipulation Built-in Commands

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.

To research, IFS= seems equivalent with IFS="$(printf '\n\r')" in that it recognizes '\n' and '\r' as separators but not the space ' '. It did not work for me, but why it worked in examples?

Restoring the default IFS value

IFS="$(printf ' \t\n\r')"

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.

PATH

PATH

PIPESTATUS

See:

Pipeline

PPID

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

PS1

Bash Prompt

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:

The FUNCNAME Array

OLDPWD

Contains directory before the last cd command.

cd directory
echo $OLDPWD
/home/user

LD_LIBRARY_PATH

https://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
Linux Shared Libraries

LD_PRELOAD

https://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
Linux Shared Libraries