Built-in Shell Variables

The $ sign is actually not part of the variable name, although some built-in variables are always referenced this way. We do the same.

The first element passed to the shell is the command name.
The nth argument passed on the command line. If n ≥ 10, then the syntax must be ${n}.
All the arguments on the command line. The values are separated by the first character in the shell variable IFS: (${1} … ${n}). See also: the IFS entry in Other Shell Variables.
All the arguments on the command line. The values are individually quoted: ("${1}" … "${n}").
The number of command-line arguments.
The exit value of the last executed command.
The last argument of the previous command.

The full path used to invoke this instance of Bash.

A string describing the Bash version.

Examples of use:

  • If more than one version of Bash are installed on the computer, then the running one can be retrieved by:
    bash -c 'echo $BASH_VERSION'
  • To prompt only the numeric “major.minor.patch” part of the version string:
    echo ${BASH_VERSION%%[^0-9.]*}
  • To verify if the running Bash is at least version 4:
    if (( $(bash -c 'echo ${BASH_VERSION%%.*}') >= 4 )); then
      echo "yes"
      echo "no"
  • To verify if Bash is at least of version 3.2:
    if printf '%s\n%s\n' "$BASH_VERSION" "3.2" | sort -rVC; then
      echo "yes"
      echo "no"
The name of the current host.
The default array for the built-in mapfile and readarray commands.
The value of arguments to last option processed by the built-in getopts command.
The numerical index of OPTARG in a built-in getopts command.
The current working directory (“print working directory”). It can be changed by the built-in cd command.