Linux Command Reference ยท Bash Syntax
Bash special parameters
Reference for Bash special parameters: $?, $$, $!, $@, $*, $#, $0, $_ and $-, with the difference between $@ and $* and common pitfalls.
Special parameters are variables that Bash maintains for you. You can read them with a $, but you can never assign to them. They describe the state of the shell: how the last command ended, how many arguments the script received, the process ID of the shell, and more.
Most of them are punctuation characters, which makes them look cryptic at first. Learn the handful you meet daily ($?, $#, $@, $0) and the rest follow the same pattern.
echo "status=$? args=$# first=$1 all=$@ pid=$$"
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
$? $$ $! $# $0 $1 ... ${10} $@ $* $- $_
| Name | Description |
|---|---|
$? |
Exit status of the most recent foreground command (0 means success). |
$$ |
Process ID of the current shell. Inside a subshell it still shows the parent shell's ID; use $BASHPID for the real one. |
$! |
Process ID of the most recent background job. Empty until you start one. |
$# |
Number of positional parameters (arguments). |
$0 |
Name of the shell or script. With bash -c 'cmd' name, it is name. |
$1, $2, ... |
Positional parameters. From the tenth on, use braces: ${10}. |
$@ |
All positional parameters. Quoted, each one stays a separate word. |
$* |
All positional parameters. Quoted, they become a single word joined by the first character of IFS. |
$- |
Letters of the currently active shell options (for example i for interactive, f for noglob). |
$_ |
The last argument of the previous command; at shell start, the path used to run the shell. |
Returns: each expands to a string. $# and $? are always integers.
Edge cases: $1 for a missing argument is empty (or an error under set -u). $@ with no arguments expands to nothing at all when quoted, so "$@" produces zero words.
Usage notes
- Always write
"$@"when you forward arguments. It preserves spaces inside each argument. Unquoted$@and$*are split on whitespace and globbed, which breaks names likemy file.txt. "$*"is useful when you want one string, for example a log message:echo "args: $*". Change the separator by settingIFSfirst.$?is fragile. It holds the status of the last command only, includingechoand[. Copy it to a variable immediately, or test the command directly withif cmd; then.- In a pipeline
$?is the status of the last command. To see every stage, read the arrayPIPESTATUS, or useset -o pipefail. $$is not unique for temporary files across subshells; usemktempinstead.$!andwait: save$!right after the&, since the next background job replaces it. Seewait.$0is not reliable for the script's location (it is whatever the caller typed). Use${BASH_SOURCE[0]}for the path of the current file.$#in a function counts the function's arguments, not the script's.- Changing positional parameters:
set -- a b creplaces them andshiftremoves from the front.