Advanced Bash Scripting
Special Shell Variables
Overview
Shell scripting thrives on feedback—knowing whether each step succeeded, failed, or requires special handling. Special shell variables provide this introspection by exposing the shell environment, command-line arguments, process identifiers, and more. Leveraging these built-in indicators leads to more robust, maintainable scripts.
Understanding Exit Status in Bash
Every command you run returns an exit status:
0
means success- Any nonzero value indicates failure
The special variable $?
always holds the exit status of the last executed command:
$ ls /nonexistent
ls: cannot access '/nonexistent': No such file or directory
$ echo $?
2
$ echo "Listing current directory:"
$ ls
KodeKloud_introduction
$ echo $?
0
Note
Use echo $?
immediately after a command to capture its exit status—subsequent commands will overwrite this value.
Exploring Common Special Shell Variables
Here’s a quick reference for the most widely used POSIX-compliant special parameters:
Variable | Description | Example |
---|---|---|
$* | All positional parameters as a single word | echo "Args: $*" |
$# | Number of positional parameters | echo "Count: $#" |
$0 | Name of the script or shell | echo "Script: $0" |
$! | PID of the last background command | sleep 30 & then echo $! |
$- | Current shell options (flags) as a string | echo "Options: $-" |
Each of these variables updates dynamically based on the context in which your script runs.
Warning
Expanding $*
without quotes can lead to word splitting and unintended globbing. Use quotes or consider $@
when preserving argument boundaries is critical.
Practical Use Cases
- Argument Validation
if [ $# -lt 2 ]; then echo "Usage: $0 <source> <destination>" exit 1 fi
- Logging Script Name
echo "[$(date)] Starting backup script: $0"
- Background Job Monitoring
long_running_task & bg_pid=$! wait $bg_pid echo "Job $bg_pid finished with status $?"
Links and References
Watch Video
Watch video content