ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)
Set your Publisher ID in src/config/ads.ts to display live ads.
Bash Shell Scripting Tutorial• 6 min read
Positional Parameters & Special Variables
$0, $1, $#, $@, and the exit status code $?
Scripts accept command-line arguments accessible via positional parameters ($1, $2), while special variables inspect exit codes and process counts.
Key Special Variables
$0: The script filename or path.
$1, $2 ... $9: Positional arguments passed to the script (use ${10} for two digits).
$#: The total count of arguments passed.
$@: All arguments as a separate list of words.
$?: The EXIT STATUS CODE of the last executed command (0 = Success; non-zero = Failure).
Inspecting Arguments and Exit Status
#!/usr/bin/env bash
if [ "$#" -lt 1 ]; then
echo "Usage: $0 <target_directory>"
exit 1
fi
TARGET_DIR="$1"
echo "Creating backup for: $TARGET_DIR"
mkdir -p "$TARGET_DIR"
# Check exit code of previous mkdir command
if [ "$?" -eq 0 ]; then
echo "Directory created successfully!"
fiADVERTISEMENT
Google AdSense Slot (inContentBanner)
Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
Which special variable in Bash holds the exit status code of the most recently executed command?