Positional & Special Params in Bash

From the Bash Scripting cheat sheet · Parameters & Special Variables · verified Jul 2026

Positional & Special Params

Access arguments and shell state.

bash
# Quick Reference
$0   # script name
$1 $2 ...   # positional args
$@   # all args (each quoted)
$#   # arg count
$?   # last exit status
$$   # current PID
💡 Prefer "$@" (quoted) to iterate args - it preserves each argument's boundaries.
⚡ $? holds the previous command's exit code: 0 means success, non-zero means failure.
📌 shift drops $1 and renumbers the rest - handy for processing args one by one.
🟢 "$@" keeps arguments separate; "$*" joins them into a single word.
argumentsspecial-vars
Back to the full Bash Scripting cheat sheet