Special Parameters: $#, $?, $$, $!, $@, and $*
Bash keeps a small group of variables that it sets and maintains for you. They have punctuation instead of letters in their names, which is why they are called special parameters. You can read them, but you cannot assign to them. This lesson covers six of the most useful ones.
| Parameter | Meaning |
|---|---|
$# | The number of positional parameters (arguments) |
$? | The exit status of the last foreground command |
$$ | The process ID (PID) of the current shell |
$! | The process ID of the most recent background command |
$@ | All positional parameters, each as a separate word when quoted |
$* | All positional parameters, joined into one word when quoted |
$# : The Number of Arguments
$# holds the count of arguments given to the script (or to the current function). It does not count $0.
cat > c1.sh <<'EOF'
echo "arguments: $#"
EOF
bash c1.sh
bash c1.sh a b c
bash c1.sh "a b c"
bash c1.sh ""
arguments: 0
arguments: 3
arguments: 1
arguments: 1
Quoting decides how many arguments there are. "a b c" is one argument, and an empty quoted string "" is also one argument. $# is commonly used in conditions such as [[ $# -eq 0 ]] to detect a missing argument, as in the previous lesson.
$? : The Exit Status of the Last Command
Every command finishes with a number called its exit status. By convention 0 means success, and any other number means failure. Bash stores the status of the most recent foreground command in $?:
cat > c2.sh <<'EOF'
true; echo "after true: $?"
false; echo "after false: $?"
ls /nonexistent_dir 2>/dev/null; echo "after failed ls: $?"
echo "x" > /dev/null; echo "after echo: $?"
grep -q root /etc/passwd; echo "grep found: $?"
grep -q zzzznotthere /etc/passwd; echo "grep missing: $?"
nosuchcommand_xyz 2>/dev/null; echo "unknown command: $?"
EOF
bash c2.sh
after true: 0
after false: 1
after failed ls: 2
after echo: 0
grep found: 0
grep missing: 1
unknown command: 127
Different programs use different failure numbers, which their documentation explains. Some conventions are widely shared: 1 for a general error, 2 for wrong usage (GNU ls returned 2 for a missing directory), 127 for "command not found", and 128 plus a signal number when a program is killed by a signal.
$? Changes After Every Command
Reading $? is itself a command (echo), which succeeds and replaces the previous value:
cat > c2b.sh <<'EOF'
false
echo "first: $?"
echo "second: $?"
EOF
bash c2b.sh
first: 1
second: 0
The first echo showed the status of false. The second echo showed the status of the first echo, which succeeded. If you need the status later, copy it into a variable right away:
cat > c3.sh <<'EOF'
grep -q zzzznotthere /etc/passwd
status=$?
echo "grep status saved: $status"
if [[ $status -ne 0 ]]; then echo "not found"; fi
EOF
bash c3.sh
grep status saved: 1
not found
Functions and $?
A function's exit status is the status of its last command, or the number given to return:
cat > ret.sh <<'EOF'
check() { return 4; }
check
echo "function status: $?"
EOF
bash ret.sh
function status: 4
Lessons 116 and 117 discuss exit codes and the safe use of $? in detail.
$$ : The Process ID of the Shell
Every running program has a unique number called its process ID (PID). $$ expands to the PID of the shell that is running your script. Because the number is different for every run, it is often used to build unique file names:
cat > pid.sh <<'EOF'
echo "PID of this script: $$"
tmp=/tmp/report.$$
echo "temporary file name: $tmp"
EOF
bash pid.sh
PID of this script: 4821
temporary file name: /tmp/report.4821
The number 4821 is only a sample; you will see a different value each time. Run the script twice and the temporary names will differ, so two runs at the same time do not collide on the same file.
There is one special rule. A subshell, written with parentheses, is a separate process, but $$ keeps showing the PID of the original shell. To get the PID of the subshell itself, Bash provides $BASHPID:
cat > c4.sh <<'EOF'
PARENT=$$
( [[ $$ == $PARENT ]] && echo "subshell \$\$ is the same as the parent: yes" )
( [[ $BASHPID != $PARENT ]] && echo "subshell \$BASHPID differs from the parent: yes" )
EOF
bash c4.sh
subshell $$ is the same as the parent: yes
subshell $BASHPID differs from the parent: yes
Lesson 139 uses $$ together with $! for process management.
$! : The PID of the Last Background Command
A command followed by & runs in the background, and the shell continues without waiting for it (lesson 136 covers this fully). Right after starting such a command, $! holds its PID, which lets you wait for it or stop it:
cat > bg.sh <<'EOF'
sleep 30 &
pid=$!
echo "started a background job"
kill "$pid"
wait "$pid" 2>/dev/null
echo "wait status after kill: $?"
EOF
bash bg.sh
started a background job
wait status after kill: 143
The script started sleep 30 in the background, saved its PID, stopped it with kill, and then used wait to collect its result. The status 143 is 128 + 15, where 15 is the number of the termination signal that kill sends by default.
Two details are worth remembering:
$!always refers to the most recent background command. Starting a second one replaces the value, so copy it into a variable at once if you need it later.- Before any background command has been started,
$!is empty.
$@ and $* : All the Arguments
Both parameters stand for the complete list of positional parameters, and the difference appears only when they are quoted. The helper below prints how many items a command receives and what each item contains:
cat > c7.sh <<'EOF'
show() { echo "$# items:"; for x in "$@"; do echo " [$x]"; done; }
set -- "red apple" "green pear" plum
echo "--- unquoted \$@"; show $@
echo "--- unquoted \$*"; show $*
echo "--- quoted \"\$@\""; show "$@"
echo "--- quoted \"\$*\""; show "$*"
EOF
bash c7.sh
--- unquoted $@
5 items:
[red]
[apple]
[green]
[pear]
[plum]
--- unquoted $*
5 items:
[red]
[apple]
[green]
[pear]
[plum]
--- quoted "$@"
3 items:
[red apple]
[green pear]
[plum]
--- quoted "$*"
1 items:
[red apple green pear plum]
The set -- command (explained in lesson 44) simply loads three arguments into the positional parameters so that the example does not need command-line input. The results show three behaviors:
- Unquoted
$@and$*are identical. Bash splits every argument again on spaces, sored appleturns into two words. This destroys the original argument boundaries. "$@"expands to each argument as its own word, exactly as it was received.red applestays together."$*"expands to a single word that joins all arguments with a separator.
The separator for "$*" is the first character of the IFS variable, which is a space by default. Changing IFS turns "$*" into a handy way to join values:
cat > c8.sh <<'EOF'
set -- 2026 11 20
IFS=-
echo "$*"
IFS=,
joined="$*"
echo "joined=$joined"
EOF
bash c8.sh
2026-11-20
joined=2026,11,20
Passing Arguments On
Use "$@" when you want to hand your arguments to another command or function, because it preserves every argument exactly:
cat > c9.sh <<'EOF'
show() { echo "$# items:"; for x in "$@"; do echo " [$x]"; done; }
wrapper() { show "$@"; }
wrapper "a b" c
wrapper2() { show "$*"; }
wrapper2 "a b" c
EOF
bash c9.sh
2 items:
[a b]
[c]
1 items:
[a b c]
wrapper forwarded two arguments, while wrapper2 squashed them into one. For this reason, "$@" is the correct choice in almost every script, and "$*" is reserved for the cases where you really want one combined string, such as a log message.
When There Are No Arguments
With no arguments, "$@" expands to nothing, but "$*" expands to one empty string:
cat > c10.sh <<'EOF'
show() { echo "$# items"; }
set --
show "$@"
show "$*"
EOF
bash c10.sh
0 items
1 items
Because of this, "$@" can be passed to a command safely even when the list is empty, since the command simply receives no extra arguments.
Using the Parameters Together
A short script can use several special parameters at once. It checks $#, loops over "$@", and ends with a status that other programs can test through $?:
cat > check.sh <<'EOF'
#!/usr/bin/env bash
if [[ $# -eq 0 ]]; then
echo "No files given" >&2
exit 2
fi
missing=0
for f in "$@"; do
[[ -e $f ]] || { echo "missing: $f"; (( missing++ )); }
done
echo "checked $# file(s), $missing missing"
(( missing == 0 ))
EOF
touch real.txt
bash check.sh real.txt ghost.txt
echo "status: $?"
bash check.sh real.txt
echo "status: $?"
missing: ghost.txt
checked 2 file(s), 1 missing
status: 1
checked 1 file(s), 0 missing
status: 0
The last line, (( missing == 0 )), is an arithmetic test whose result becomes the exit status of the script: 0 (success) when nothing is missing and 1 otherwise. Anything that runs the script afterwards can read that status through its own $?.