🐚 Bash Special Variables – $?, $#, $@, $0, $1 Explained with Examples
Introduction to Bash Special Variables – Understand Script Arguments & Exit Status
Bash includes a powerful set of special variables that provide insight into script arguments, command success, process IDs, and more. These built-in variables begin with a dollar sign ($) and serve different roles when writing or debugging scripts.
Whether you’re automating tasks or handling input parameters, knowing how to use Bash’s special variables like $?, $#, $@, $0, and $1 is essential.
In this article, you’ll learn:
- What Bash special variables are and how to use them
- The purpose of
$0,$1,$@,$#, and$? - Real examples of how they behave in scripts
- How to debug scripts using these variables
List of Common Bash Special Variables
| Variable | Description |
|---|---|
$0 | Name of the script itself |
$1 – $9 | Positional parameters (first to ninth argument) |
$@ | All arguments as separate words |
$* | All arguments as a single word |
$# | Number of arguments passed |
$? | Exit status of the last command |
$$ | Process ID (PID) of the current shell |
$! | PID of the last background command |
Example – Script Using Special Variables
args-demo.sh
#!/bin/bash
echo "Script name: $0"
echo "1st argument: $1"
echo "2nd argument: $2"
echo "All arguments (\$@): $@"
echo "Number of arguments: $#"
ls /nonexistentdir
echo "Exit status of last command: $?"
Run the Script:
chmod +x args-demo.sh
./args-demo.sh one two
Output:
Script name: ./args-demo.sh
1st argument: one
2nd argument: two
All arguments ($@): one two
Number of arguments: 2
ls: cannot access '/nonexistentdir': No such file or directory
Exit status of last command: 2
Detailed Explanation of Key Variables
$0 – Script Name
Returns the name of the script as it was called.
echo "Running $0"
# Output: Running ./myscript.sh
$1, $2, …, $9 – Positional Parameters
Access individual arguments passed to the script.
echo "First input: $1"
$@ – All Arguments (Preserves Quoting)
Treats each parameter as a separate word.
for arg in "$@"; do
echo "$arg"
done
$* – All Arguments (Single String)
Treats all arguments as a single string.
echo "$*"
$# – Argument Count
Returns the number of arguments passed.
echo "There are $# arguments"
$? – Exit Status of Last Command
Returns the exit code of the last command.
0means success- Any other number indicates failure
mkdir myfolder
echo "Status: $?" # Output: 0 (if success)
$$ – Current Shell PID
Useful for creating unique filenames in temp scripts.
echo "Current PID: $$"
$! – Last Background Process ID
When a command runs in the background (&), $! stores its process ID.
sleep 10 &
echo "Sleep PID: $!"
Summary: Bash Special Variables
Bash special variables unlock dynamic scripting and make shell scripts more robust, interactive, and debuggable. Use them to handle arguments, track errors, and control background processes effectively.
Key Takeaways:
$0,$1–$9handle script names and arguments$@and$*differ in how they preserve input$#tells how many arguments were passed$?helps in tracking command success/failure
Real-world Uses:
- Writing parameterized automation scripts
- Checking command execution results in cron jobs
- Capturing process IDs for cleanup or logs
FAQ – Bash Special Variables
What is the difference between $@ and $* in Bash?
$@ treats each argument as a separate quoted string, while $* merges them into a single string. Example:
for arg in "$@"; do echo "$arg"; done # each argument separately
echo "$*" # all arguments as one
How do I know how many arguments a script received?
Use $#:
echo "Argument count: $#"
What is $? used for in Bash?
$? gives the exit status of the last executed command. 0 means success, non-zero means failure.
What does $$ mean in Bash?
It holds the process ID (PID) of the currently running shell.
How do I use $! in a script?
Use it to capture the PID of a command run in the background:
command &
echo "PID: $!"
Share Now :
