Bash internal variables at a glance
suggest change| Variable | Details |
|---|---|
$* / $@ |
Function or script positional parameters (arguments). Unquoted, $* and $@ are the same as $1 $2 ... (it generally makes no sense to leave those unquoted). "$*" is the same as "$1 $2 ...". "$@" is the same as "$1" "$2" .... Arguments in "$*" are separated by the first character of $IFS, which does not have to be a space. |
$# |
Number of positional parameters passed to the script or function. |
$! |
Process ID of the last (rightmost, for pipelines) command in the most recent job put into the background. This is not necessarily the same as the job’s process group ID when job control is enabled. |
$$ |
ID of the process that executed bash. |
$? |
Exit status of the last command. |
$n |
Positional parameters, where n = 1, 2, 3, …, 9. |
${n} |
Positional parameters (same as above), but n can be greater than 9. |
$0 |
In scripts, the path with which the script was invoked. With bash -c 'printf "%s" "$0"' name args, it is name (the first argument after the inline script). Otherwise, it is the argv[0] that bash received. |
$_ |
Last argument of the previous command. |
$IFS |
Internal field separator. |
$PATH |
PATH environment variable used to look up executables. |
$OLDPWD |
Previous working directory. |
$PWD |
Present working directory. |
$FUNCNAME |
Array of function names in the execution call stack. |
$BASH_SOURCE |
Array containing source paths for elements in the FUNCNAME array. Can be used to get the script path. |
$BASH_ALIASES |
Associative array containing all currently defined aliases. |
$BASH_REMATCH |
Array of matches from the last regex match. |
$BASH_VERSION |
Bash version string. |
$BASH_VERSINFO |
An array of 6 elements with Bash version information. |
$BASH |
Absolute path to the currently executing Bash shell itself (heuristically determined by bash based on argv[0] and the value of $PATH; may be wrong in corner cases). |
$BASH_SUBSHELL |
Bash subshell level. |
$UID |
Real user ID (not the effective user ID, if different) of the process running bash. |
$PS1 |
Primary command line prompt. See Using the PS* Variables. |
$PS2 |
Secondary command line prompt (used for additional input). |
$PS3 |
Tertiary command line prompt (used in a select loop). |
$PS4 |
Quaternary command line prompt (prefixed to each command in a set -x trace). |
$RANDOM |
A pseudorandom integer between 0 and 32767. |
$REPLY |
Variable used by read by default when no variable is specified. Also used by select to return the user-supplied value. |
$PIPESTATUS |
Array variable that holds the exit status values of each command in the most recently executed foreground pipeline. |
Variable assignment must have no space before or after the
=:a=123, nota = 123. The latter (an equal sign surrounded by spaces) in isolation means run the commandawith the arguments=and123. The same token is also the string comparison operator, which is syntactically an argument to[or[[.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents