Skip to content
Tutorials Center
My learning

Topics

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

Terminal
echo "$BASH_VERSION  line $LINENO  pid $BASHPID"

Try it

Bash Version, location and pipeline status

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.

Bash Random numbers, time and counters

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

Terminal
${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

  • RANDOM is not secure. It is a simple generator; never use it for passwords or tokens. Use /dev/urandom instead.
  • Modulo bias: RANDOM % n slightly favors small values when 32768 is not a multiple of n. Fine for games, not for statistics.
  • SECONDS has one-second resolution and is perfect for rough timing without calling date. For finer timing compare EPOCHREALTIME values or use time.
  • Check versions by number, not by string. Features such as associative arrays need 4.0, namerefs 4.3 and EPOCHSECONDS 5.0, so test BASH_VERSINFO before using them.
  • PIPESTATUS is overwritten by the next command, including echo. Copy it right away: st=("${PIPESTATUS[@]}").
  • BASH_SOURCE[0] vs $0: when a file is sourced, $0 is still the original script, but BASH_SOURCE[0] is the sourced file. Use it to find a script's own directory.
  • Read-only ones: assigning to BASHPID, UID or EUID is ignored or an error.
  • FUNCNAME is empty outside functions, so ${FUNCNAME[1]:-main} provides a safe fallback.