Differences between revisions 1 and 12 (spanning 11 versions)
Revision 1 as of 2023-01-22 23:20:01
Size: 6589
Comment:
Revision 12 as of 2023-01-25 20:56:47
Size: 9478
Comment:
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
== Bash ==

`$BASHPID` is the current shell's PID.

`$BASHOPTS` is a colon-delimited list of [[Bash/ShellOptions|enabled shell options]].

`$BASH_COMPAT` is the compat level of the shell. Setting to `4.2` or `42` is equivalent to using the `compat42` [[Bash/ShellOption#Compatibility_Mode|shell option.

`$BASH_ALIASES` is an associative array of [[Bash/Alias|aliases]]. `$BASH_CMDS` is an associative array of [[Bash/Builtin|builtins]].
== Bash Internals ==



=== Limits ===

`$FUNCNEST` is the maximum nesting level for [[Bash/Function|functions]].



=== Constants ===

`$BASHPID` is the current shell's PID. `$PPID` is the PID of the shell's parent.

`$BASH_VERSION` is the current shell's version number. `$BASH_VERSINFO` is an array representing this information.

`$MACHTYPE`, `$HOSTTYPE`, and `$OSTYPE` describe the system on which `bash(1)` is running.

`$EMACS` and `$INSIDE_EMACS` are used to determine if the shell is operating within [[Emacs]], in which case line editing will be disabled.



=== Parameters ===

`$RANDOM` is a random number between 0 and 32767. If set, the random number generator is seeded with that number.

`$SRANDOM` is a random 32-bit number that cannot be seeded.

`$EPOCHSECONDS` and `$EPOCHREALTIME` are the current time.

`$SECONDS` is the number of seconds since the `bash(1)` began running.



=== Files and Directories ===

`$BASH_ENV` (or `$ENV`) is a startup file used when `bash(1)` is used as a script interpretter.

`$TMPDIR` is the location where temporary files are written.



=== Not Useful ===

`$BASHOPTS` and `$SHELLOPTS` are a colon-delimited list of [[Bash/ShellOption|enabled shell options]].

`$BASH_COMPAT` is the compat level of the shell. Setting to `4.2` or `42` is equivalent to using the `compat42` [[Bash/ShellOption#Compatibility_Mode|shell option]].

`$POSIXLY_CORRECT` sets POSIX mode.

`$BASH_ALIASES` is an associative array of [[Bash/Alias|aliases]]. `$BASH_CMDS` is an associative array of [[Bash/BuiltinCommand|builtins]].

`$BASH_LOADABLES_PATH` is a colon-separated list of directories searched for dynamically loadable builtins.

`$SHELL` is the full pathname of the shell.
Line 23: Line 69:
`$BASH_SUBSHELL` is the current nesting level of subshells. Similarly, `$SHLVL` is a counter for number of concurrent shells, which in some circumstances is a shell nesting level.
Line 25: Line 73:



`$BASH_ARGV` is an array containing all the arguments in the current shell call stack, while `$BASH_ARGC` is the count of those arguments. These are only set if [[Bash/ShellOptions#Extended_Debugging_Mode|extended debugging mode]] is on.
If `bash(1)` is called like `bash -c '...'`, `$BASH_EXECUTION_STRING` is set to the command string.

`$BASH_ARGV` is an array containing all the arguments in the current shell call stack, while `$BASH_ARGC` is the count of those arguments. These are only set if [[Bash/ShellOption#Extended_Debugging_Mode|extended debugging mode]] is on.
Line 32: Line 79:





    The value is used to set the shell’s compatibility level. See Shell Compatibility Mode, for a description of the various compatibility levels and their effects. The value may be a decimal number (e.g., 4.2) or an integer (e.g., 42) corresponding to the desired compatibility level. If BASH_COMPAT is unset or set to the empty string, the compatibility level is set to the default for the current version. If BASH_COMPAT is set to a value that is not one of the valid compatibility levels, the shell prints an error message and sets the compatibility level to the default for the current version. The valid values correspond to the compatibility levels described below (see Shell Compatibility Mode). For example, 4.2 and 42 are valid values that correspond to the compat42 shopt option and set the compatibility level to 42. The current version is also a valid value.
BASH_ENV

    If this variable is set when Bash is invoked to execute a shell script, its value is expanded and used as the name of a startup file to read before executing the script. See Bash Startup Files.
BASH_EXECUTION_STRING

    The command argument to the -c invocation option.
BASH_LINENO

    An array variable whose members are the line numbers in source files where each corresponding member of FUNCNAME was invoked. ${BASH_LINENO[$i]} is the line number in the source file (${BASH_SOURCE[$i+1]}) where ${FUNCNAME[$i]} was called (or ${BASH_LINENO[$i-1]} if referenced within another shell function). Use LINENO to obtain the current line number.
BASH_LOADABLES_PATH

    A colon-separated list of directories in which the shell looks for dynamically loadable builtins specified by the enable command.
BASH_REMATCH

    An array variable whose members are assigned by the ‘=~’ binary operator to the [[ conditional command (see Conditional Constructs). The element with index 0 is the portion of the string matching the entire regular expression. The element with index n is the portion of the string matching the nth parenthesized subexpression.
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_SUBSHELL

    Incremented by one within each subshell or subshell environment when the shell begins executing in that environment. The initial value is 0. If BASH_SUBSHELL is unset, it loses its special properties, even if it is subsequently reset.
BASH_VERSINFO

    A readonly array variable (see Arrays) 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.

BASH_VERSION

    The version number of the current instance of Bash.
BASH_XTRACEFD

    If set to an integer corresponding to a valid file descriptor, Bash will write the trace output generated when ‘set -x’ is enabled to that file descriptor. This allows tracing output to be separated from diagnostic and error messages. The file descriptor is closed when BASH_XTRACEFD is unset or assigned a new value. Unsetting BASH_XTRACEFD or assigning it the empty string causes the trace output to be sent to the standard error. Note that setting BASH_XTRACEFD to 2 (the standard error file descriptor) and then unsetting it will result in the standard error being closed.

----



== Directory Stack ==

`$DIRSTACK` is an array containing the directory stack.
`$LINENO` is the line number of the currently executing command.

`$FUNCNAME` is an array of functions currently in the call stack. `$BASH_SOURCE` is an array of source filenames corresponding to `$FUNCNAME`. Function `${FUNCNAME[i]}` is defined in file `${BASH_SOURCE[i]}`. Similarly, `$BASH_LINENO` is an array of line numbers that corresponds to `$FUNCNAME`. Function `${FUNCNAME[i]}` is called from line number `${BASH_LINENO[i]}`.

`$COLUMNS` and `$LINES` are used to indicate screen size.

`$CHILD_MAX` is the number of child process exit statuses to remember.

`$PIPESTATUS` is an array of exit statuses from each process in the most recent [[Bash/Pipeline|pipeline]].

`$IGNOREEOF` is an integer representing the number of `EOF` characters that can be received before the shell exits. If set to a value that is not an integer, the interpretted value is 10. If not set, the default value is 1.

----



== Completion ==

The following are all only useful for programming [[Bash/Completion|completion]].

`$COMP_LINE` is the current command line prompt. `$COMP_POINT` is the current location of the cursor within that line.

`$COMP_WORDS` is an array of the current command line prompt, tokenized. `$COMP_CWORD` is the index of `$COMP_WORDS` where the cursor currently is located. `$COMP_WORDBREAKS` is the separators that are being used to tokenize the command line prompt.

`$COMP_TYPE` indicates the type of operation that trigger completion. `$COMP_KEY` is the key used to trigger completion.

`$COMPREPLY` is used to pass completions back to the shell.

`$READLINE_ARGUMENT`, `$READLINE_LINE`, `$READLINE_MARK`, and `$READLINE_POINT` are used to pass information into `readline(3)`.

`$INPUTRC` is the `readline(3)` initialization file. The default is `~/.inputrc`.

----



== Coprocesses ==

If a [[Bash/Coprocess|coprocess]] name is not specified for the `coproc` [[Bash/BuiltinCommands#Coproc|builtin]], ithe file descriptor is set into the `$COPROC` array.
 
----



== Fix Command ==

`$FCEDIT` is the editor to use with the `fc` [[Bash/BuiltinCommands#Fc|builtin]].

----



== Filename Expansion ==

`$FIGNORE` is a colon-delimited list of suffixes to ignore when evaluating filename expansion.

`$GLOBIGNORE` s a colon-delimited list of patterns to ignore when evaluating filename expansion.
Line 103: Line 143:
The `getopts` builtin sets two variables: The `getopts` [[Bash/BuiltinCommands#GetOpts|builtin]] sets two variables:
Line 108: Line 148:
----



== Home ==
If `$OPTERR` is set to 1, error messages will propogate from the `getopts` builtin.

----



== History ==

`$HISTFILE` is the `bash(1)` history filename.

`$HISTFILESIZE` is the maximum file size for `$HISTFILE`, while `$HISTSIZE` the maximum number of commands for `$HISTFILE`.

`$HISTCONTROL` and `$HISTIGNORE` configure which commands are recorded in the history.

`$HISTCMD` is the current command's history number.

If `$HISTTIMEFORMAT` is set, commands are recorded in the history with a timestamp formatted with this value.

`$histchars` configure the history expansion.

----



== Inter Field Separator ==

`$IFS` defines the characters that should be interpreted as separating tokens.

Frequently, this is only manipulated when writing a pipeline that specially delimits items with the null byte.

----



== Locale ==

`$LC_COLLATE`, `$LC_CTYPE`, `$LC_MESSAGES`, `$LC_NUMERIC`, and `$LC_TIME` are all used in different contexts to determine the current locale.

`$LANG` is the fallback value. `$LC_ALL` overrides `$LANG` and all of the above if set.

----



== Mail ==

If `$MAIL` is set, `bash(1)` monitors it (assuming it is a mail file or Maildir directory) and informs the user of changes.

Similarly, `$MAILPATH` is a colon-delimited list of locations to monitor. If set, `$MAIL` is not monitored.

`$MAILCHECK` is the number of seconds `bash(1)` will wait between checks. The default is 60.

----



== Map File ==

If a variable name is not specified for the `mapfile` [[Bash/BuiltinCommands#MapFile|builtin]], `$MAPFILE` is set as an array with that output.

----



== Name Resolution ==

`$HOSTNAME` is the name of the current system.

`$HOSTFILE` is the hosts file that should be referenced for resolving names.

----



== Paths ==

`$PWD` is the current working directory. `$OLDPWD` is the previous working directory.

`$PATH` is a colon-delimited list of directories searched for commands.

`$EXECIGNORE` is a a colon-delimited list of patterns to ignore when searching for commands.

`$CDPATH` is similarly searched by the `cd` builtin.

`$DIRSTACK` is an array containing the directory stack, as manipulated by the `popd` and `pushd` [[Bash/BuiltinCommands#PushD|builtins]].

----



== Prompts ==

`$PS1` is printed at the beginning of every command prompt.

`$PS2` is printed at the beginning of run-on lines.

`$PS3` is printed at the prompt in [[Bash/Select|select menus]].

`$PS0`

`$PS4`

`$PROMPT_COMMAND` is either a command string or an array of command strings. These commands are executed before printing the prompt.

`$PROMPT_DIRTRIM` sets the number of directory names to retain when expanding the `\w` and `\W` escapes in a prompt.

----



== Read ==

If a variable name is not specified for the `read` [[Bash/BuiltinCommands#Read|builtin]], `$REPLY` is set with that output.

`$TMOUT` sets a timeout for the `read` and `select` builtins.

----



== Regular Expressions ==

`$BASH_REMATCH` is an array set by the `=~` operator. The first item is the substring matching the entire regular expression. `${BASH_REMATCH[i]}` is the substring matching the ''i''th subexpression.

----



== Time ==

`$TIMEFORMAT` sets the format for how the `time` [[Bash/BuiltinCommands#Time|builtin]] will display.

----



== Tracing ==

`$BASH_XTRACEFD` will, if set to an integer corresponding to a valid [[Bash/FileDescriptor|file descriptor]], cause trace output to be written to that file descriptor.

----



== User ==

`$UID` is the user's ID. `$EUID` is the effective EID.

`$GROUPS` is an array of groups the user is a member of.
Line 116: Line 300:
----



== Inter Field Separator ==

`$IFS` defines the characters that should be interpreted as separating tokens.

Frequently, this is only manipulated when writing a pipeline that specially delimits items with the null byte.

----



== Mail ==

If `$MAIL` is set, `bash(1)` monitors it (assuming it is a mail file or Maildir directory) and informs the user of changes.

Similarly, `$MAILPATH` is a colon-delimited list of locations to monitor. If set, `$MAIL` is not monitored.

----



== Path ==

`$PATH` is a colon-delimited list of directories searched for commands.

`$CDPATH` is similarly searched by the `cd` builtin.

----



== Prompts ==

`$PS1` is printed at the beginning of every command prompt.

`$PS2` is printed at the beginning of run-on lines.

`$PS3` is printed at the prompt in [[Bash/Select|select menus]].

Bash Shell Variables

This is a list of special variables that are set in bash(1) to configure it's behavior, or by bash(1) to indicate state. For programming variables, including special ones, see here.


Bash Internals

Limits

$FUNCNEST is the maximum nesting level for functions.

Constants

$BASHPID is the current shell's PID. $PPID is the PID of the shell's parent.

$BASH_VERSION is the current shell's version number. $BASH_VERSINFO is an array representing this information.

$MACHTYPE, $HOSTTYPE, and $OSTYPE describe the system on which bash(1) is running.

$EMACS and $INSIDE_EMACS are used to determine if the shell is operating within Emacs, in which case line editing will be disabled.

Parameters

$RANDOM is a random number between 0 and 32767. If set, the random number generator is seeded with that number.

$SRANDOM is a random 32-bit number that cannot be seeded.

$EPOCHSECONDS and $EPOCHREALTIME are the current time.

$SECONDS is the number of seconds since the bash(1) began running.

Files and Directories

$BASH_ENV (or $ENV) is a startup file used when bash(1) is used as a script interpretter.

$TMPDIR is the location where temporary files are written.

Not Useful

$BASHOPTS and $SHELLOPTS are a colon-delimited list of enabled shell options.

$BASH_COMPAT is the compat level of the shell. Setting to 4.2 or 42 is equivalent to using the compat42 shell option.

$POSIXLY_CORRECT sets POSIX mode.

$BASH_ALIASES is an associative array of aliases. $BASH_CMDS is an associative array of builtins.

$BASH_LOADABLES_PATH is a colon-separated list of directories searched for dynamically loadable builtins.

$SHELL is the full pathname of the shell.

$_ is, at first, the pathname used to call the shell. Thereafter every time a command is called, $_ is the last argument to that command. Furthermore, while a command is executed, $_ is set to the pathname used to call that command. This is exposed when a command inspects its environment.

$BASH_SUBSHELL is the current nesting level of subshells. Similarly, $SHLVL is a counter for number of concurrent shells, which in some circumstances is a shell nesting level.

$BASH_COMMAND is the command currently executing. If bash(1) is executing a trap, $BASH_COMMAND will show the command that triggered the trap.

If bash(1) is called like bash -c '...', $BASH_EXECUTION_STRING is set to the command string.

$BASH_ARGV is an array containing all the arguments in the current shell call stack, while $BASH_ARGC is the count of those arguments. These are only set if extended debugging mode is on.

In a similar manner, $BASH_ARGV0 is the name of the shell. It is identical to $0.

$LINENO is the line number of the currently executing command.

$FUNCNAME is an array of functions currently in the call stack. $BASH_SOURCE is an array of source filenames corresponding to $FUNCNAME. Function ${FUNCNAME[i]} is defined in file ${BASH_SOURCE[i]}. Similarly, $BASH_LINENO is an array of line numbers that corresponds to $FUNCNAME. Function ${FUNCNAME[i]} is called from line number ${BASH_LINENO[i]}.

$COLUMNS and $LINES are used to indicate screen size.

$CHILD_MAX is the number of child process exit statuses to remember.

$PIPESTATUS is an array of exit statuses from each process in the most recent pipeline.

$IGNOREEOF is an integer representing the number of EOF characters that can be received before the shell exits. If set to a value that is not an integer, the interpretted value is 10. If not set, the default value is 1.


Completion

The following are all only useful for programming completion.

$COMP_LINE is the current command line prompt. $COMP_POINT is the current location of the cursor within that line.

$COMP_WORDS is an array of the current command line prompt, tokenized. $COMP_CWORD is the index of $COMP_WORDS where the cursor currently is located. $COMP_WORDBREAKS is the separators that are being used to tokenize the command line prompt.

$COMP_TYPE indicates the type of operation that trigger completion. $COMP_KEY is the key used to trigger completion.

$COMPREPLY is used to pass completions back to the shell.

$READLINE_ARGUMENT, $READLINE_LINE, $READLINE_MARK, and $READLINE_POINT are used to pass information into readline(3).

$INPUTRC is the readline(3) initialization file. The default is ~/.inputrc.


Coprocesses

If a coprocess name is not specified for the coproc builtin, ithe file descriptor is set into the $COPROC array.


Fix Command

$FCEDIT is the editor to use with the fc builtin.


Filename Expansion

$FIGNORE is a colon-delimited list of suffixes to ignore when evaluating filename expansion.

$GLOBIGNORE s a colon-delimited list of patterns to ignore when evaluating filename expansion.


Get Opts

The getopts builtin sets two variables:

  • $OPTARG is the most recent options's value

  • $OPTIND is the most recent option's index

If $OPTERR is set to 1, error messages will propogate from the getopts builtin.


History

$HISTFILE is the bash(1) history filename.

$HISTFILESIZE is the maximum file size for $HISTFILE, while $HISTSIZE the maximum number of commands for $HISTFILE.

$HISTCONTROL and $HISTIGNORE configure which commands are recorded in the history.

$HISTCMD is the current command's history number.

If $HISTTIMEFORMAT is set, commands are recorded in the history with a timestamp formatted with this value.

$histchars configure the history expansion.


Inter Field Separator

$IFS defines the characters that should be interpreted as separating tokens.

Frequently, this is only manipulated when writing a pipeline that specially delimits items with the null byte.


Locale

$LC_COLLATE, $LC_CTYPE, $LC_MESSAGES, $LC_NUMERIC, and $LC_TIME are all used in different contexts to determine the current locale.

$LANG is the fallback value. $LC_ALL overrides $LANG and all of the above if set.


Mail

If $MAIL is set, bash(1) monitors it (assuming it is a mail file or Maildir directory) and informs the user of changes.

Similarly, $MAILPATH is a colon-delimited list of locations to monitor. If set, $MAIL is not monitored.

$MAILCHECK is the number of seconds bash(1) will wait between checks. The default is 60.


Map File

If a variable name is not specified for the mapfile builtin, $MAPFILE is set as an array with that output.


Name Resolution

$HOSTNAME is the name of the current system.

$HOSTFILE is the hosts file that should be referenced for resolving names.


Paths

$PWD is the current working directory. $OLDPWD is the previous working directory.

$PATH is a colon-delimited list of directories searched for commands.

$EXECIGNORE is a a colon-delimited list of patterns to ignore when searching for commands.

$CDPATH is similarly searched by the cd builtin.

$DIRSTACK is an array containing the directory stack, as manipulated by the popd and pushd builtins.


Prompts

$PS1 is printed at the beginning of every command prompt.

$PS2 is printed at the beginning of run-on lines.

$PS3 is printed at the prompt in select menus.

$PS0

$PS4

$PROMPT_COMMAND is either a command string or an array of command strings. These commands are executed before printing the prompt.

$PROMPT_DIRTRIM sets the number of directory names to retain when expanding the \w and \W escapes in a prompt.


Read

If a variable name is not specified for the read builtin, $REPLY is set with that output.

$TMOUT sets a timeout for the read and select builtins.


Regular Expressions

$BASH_REMATCH is an array set by the =~ operator. The first item is the substring matching the entire regular expression. ${BASH_REMATCH[i]} is the substring matching the ith subexpression.


Time

$TIMEFORMAT sets the format for how the time builtin will display.


Tracing

$BASH_XTRACEFD will, if set to an integer corresponding to a valid file descriptor, cause trace output to be written to that file descriptor.


User

$UID is the user's ID. $EUID is the effective EID.

$GROUPS is an array of groups the user is a member of.

$HOME is the user's home directory.


CategoryRicottone

Bash/ShellVariables (last edited 2023-01-30 02:10:05 by DominicRicottone)