Linux Command Reference ยท Bash Syntax
Bash's own variables
Reference for variables that Bash sets itself: RANDOM, SECONDS, BASH_VERSION, LINENO, FUNCNAME, BASH_SOURCE, EPOCHSECONDS, PIPESTATUS, REPLY and more.
Besides the variables you define, Bash maintains a set of its own. They tell you which version is running, which line of the script is executing, how long the shell has been up, what each stage of a pipeline returned, and they give you random numbers and the current time without starting another program.
Some of them lose their special meaning if you assign to them (LINENO, FUNCNAME), while others become a counter you can reset (SECONDS) or a generator you can seed (RANDOM).
echo "$BASH_VERSION line $LINENO pid $BASHPID"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
${NAME} # read NAME=value # only some of these may be assigned
| Variable | Description |
|---|---|
BASH_VERSION |
Version string, for example 5.0.3(1)-release. |
BASH_VERSINFO |
Array: major, minor, patch, build, release status, machine type. Compare ${BASH_VERSINFO[0]} for feature checks. |
BASHPID |
PID of the current Bash process, correct inside subshells (unlike $$). |
RANDOM |
A new integer from 0 to 32767 on every read. Assigning a value seeds the generator. |
SECONDS |
Whole seconds since the shell started. Assigning n makes it count from n. |
EPOCHSECONDS, EPOCHREALTIME |
Seconds since 1970-01-01 UTC; the second form has microseconds (Bash 5.0 and later). |
LINENO |
Line number of the command being executed. |
FUNCNAME |
Array of active function names, innermost first ([0] is the current function). |
BASH_SOURCE |
Array of file names matching FUNCNAME; ${BASH_SOURCE[0]} is the current file. |
BASH_LINENO |
Array of call-site line numbers for each function in FUNCNAME. |
BASH_COMMAND |
The command currently being run (useful in traps). |
PIPESTATUS |
Array of exit statuses of the last foreground pipeline. |
REPLY |
Default variable for read and select. |
OPTIND, OPTARG, OPTERR |
State used by getopts. |
HOSTNAME, HOSTTYPE, OSTYPE, MACHTYPE |
Machine and platform names. |
UID, EUID, GROUPS |
Real and effective user ID (read-only) and the group list. |
SHLVL |
Nesting level of Bash, increased in every new shell. |
DIRSTACK |
The directory stack used by pushd and popd. |
BASH_REMATCH |
Matches from [[ string =~ regex ]]. |
Returns: strings, except the arrays noted above. Edge cases: unset removes the special behavior of RANDOM, SECONDS, LINENO and others for the rest of the session.
Usage notes
RANDOMis not secure. It is a simple generator; never use it for passwords or tokens. Use/dev/urandominstead.- Modulo bias:
RANDOM % nslightly favors small values when 32768 is not a multiple ofn. Fine for games, not for statistics. SECONDShas one-second resolution and is perfect for rough timing without callingdate. For finer timing compareEPOCHREALTIMEvalues or usetime.- Check versions by number, not by string. Features such as associative arrays need 4.0, namerefs 4.3 and
EPOCHSECONDS5.0, so testBASH_VERSINFObefore using them. PIPESTATUSis overwritten by the next command, includingecho. Copy it right away:st=("${PIPESTATUS[@]}").BASH_SOURCE[0]vs$0: when a file is sourced,$0is still the original script, butBASH_SOURCE[0]is the sourced file. Use it to find a script's own directory.- Read-only ones: assigning to
BASHPID,UIDorEUIDis ignored or an error. FUNCNAMEis empty outside functions, so${FUNCNAME[1]:-main}provides a safe fallback.