Special variables cheat sheet
The shell's special parameters ($?, $@, $$, $!, ...) and bash's built-in variables (PIPESTATUS, BASH_SOURCE, LINENO, RANDOM, ...) with real output.
Special parameters ($?, $#, $@ ...) are set by the shell and cannot be assigned to.
Bash adds many ordinary-looking variables that it maintains itself. Rows that start with
bash -c '...' name a b set $0 to name and the positional parameters to a b, the same
way running a script with arguments does.
Script and arguments
bash -c 'echo "0=$0 1=$1 2=$2 count=$#"' myscript alpha beta$0is the script name,$1..$9the arguments,$#how many there are.0=myscript 1=alpha 2=beta count=2set -- a b c d e f g h i j k; echo "${10} ${11} but \$10 is $10"- From the tenth argument on, braces are required:
$10is$1followed by0.j k but $10 is a0 set -- "one two" three; for a in "$@"; do echo "[$a]"; done"$@"expands to every argument as a separate word, spaces preserved. Almost always the one you want.[one two] [three]set -- "one two" three; IFS=,; echo "$*""$*"joins all arguments into one word, separated by the first character ofIFS.one two,threeset -- a b c; shift; echo "$# left: $*"shiftdrops$1and renumbers the rest.2 left: b cf() { echo "${FUNCNAME[0]} got $# args, first=$1"; }; f x y- Inside a function the positional parameters are the function's own arguments.f got 2 args, first=x
Status and processes
grep -q nothing todo.txt; echo "status $?"$?is the exit status of the most recent command (0 means success).status 1false | true | (exit 3); echo "${PIPESTATUS[@]}"PIPESTATUSholds the status of every command of the last pipeline. Bash-only.1 0 3echo "shell $$, parent $PPID"; ( echo "subshell: \$\$=$$ BASHPID=$BASHPID" )$$is the PID of the main shell, even in a subshell;BASHPIDis the current process.shell 6351, parent 1 subshell: $$=6351 BASHPID=6352sleep 1 & echo "started $!"; wait $!; echo "it exited with $?"$!is the PID of the most recent background job.started 6354 it exited with 0echo "$-"; set -eu; echo "$-"$-lists the current single-letter shell options (e,u,x, ...).hBc ehuBc
Where am I
echo 'echo "${BASH_SOURCE[0]} line $LINENO"' > where.sh; bash where.shLINENOis the current line;BASH_SOURCE[0]is the file being executed (empty underbash -c).where.sh line 1outer() { inner; }; inner() { echo "${FUNCNAME[*]}"; }; outerFUNCNAMEis the call stack of function names, innermost first.inner outercd data; cd ../logs; echo "now $PWD, before $OLDPWD"PWDandOLDPWDare kept bycd;cd -goes toOLDPWD.now /home/dev/demo/logs, before /home/dev/demo/dataecho "$BASH_VERSION / ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]}"- The running bash version as a string and as an array of numbers.5.2.37(1)-release / 5.2
echo "$HOSTNAME $OSTYPE $MACHTYPE"- Host name and the platform bash was built for.demo linux-gnu x86_64-pc-linux-gnu
echo "user $USER uid $UID euid $EUID home $HOME"UID/EUIDare set by bash (read-only);USERandHOMEcome from the login environment.user dev uid 1000 euid 1000 home /home/dev
Time and randomness
SECONDS=0; sleep 1; echo "took ${SECONDS}s"SECONDScounts seconds since the shell started, or since you assigned it.took 1secho "$EPOCHSECONDS $EPOCHREALTIME"- Unix time in seconds and with microseconds, without running
date(bash 5.0+).1793478174 1793478174.297820 echo "$RANDOM $RANDOM $(( RANDOM % 6 + 1 ))"RANDOMgives 0..32767 per read. Not for secrets;SRANDOM(5.1+) gives 32-bit values from the system's random source.20490 23487 5
Parsing and input
printf '%q\n' "$IFS"IFSis the field separator for word splitting andread: space, tab, newline by default.$' \t\n'read <<< " keep spaces "; echo "[$REPLY]"readwith no variable name stores the whole line, unstripped, inREPLY.[ keep spaces ][[ "v2.4.1" =~ ^v([0-9]+)\.([0-9]+) ]]; echo "${BASH_REMATCH[@]}"BASH_REMATCHholds the whole=~match and each capture group.v2.4 2 4set -- -v -o out.txt file; while getopts vo: opt; do echo "$opt ${OPTARG-}"; done; echo "OPTIND=$OPTIND"getoptsputs an option's value inOPTARGand the next argument index inOPTIND.v o out.txt OPTIND=4echo one two three > /dev/null; echo "$_"$_is the last argument of the previous command.three
Prompts and tracing
PS4='+ line $LINENO: 'PS4prefixes eachset -xtrace line; adding$LINENOshows where each command is.BASH_XTRACEFD=5- Send
set -xoutput to file descriptor 5 instead of standard error. PS1='\u@\h:\w\$ '- The interactive prompt;
\uuser,\hhost,\wworking directory. TMOUT=300- An interactive shell exits after this many idle seconds;
readuses it as a default timeout.
From https://opscape.vip/cheatsheets/special-variables/ · checked 2026-10-31