๐ 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 :
