Skip to main content
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.
The image is a slide titled "Special Shell Variables," explaining that these variables allow access to information about the current shell environment and command-line arguments.

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:
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: Each of these variables updates dynamically based on the context in which your script runs.
Expanding $* without quotes can lead to word splitting and unintended globbing. Use quotes or consider $@ when preserving argument boundaries is critical.

Practical Use Cases

  1. Argument Validation
  2. Logging Script Name
  3. Background Job Monitoring

Watch Video