Using Special Variables in Bash Scripts
📖 Scenario: You are writing a simple bash script to understand how special variables work. These variables help you get information about the script name, arguments passed, number of arguments, process ID, and the exit status of commands.
🎯 Goal: Build a bash script that uses the special variables $0, $1, $#, $@, $?, and $$ to display useful information about the script and its arguments.
📋 What You'll Learn
Create a variable to hold the first argument using
$1Create a variable to hold the number of arguments using
$#Create a variable to hold all arguments using
$@Use
$0 to get the script nameUse
$$ to get the current process IDUse
$? to get the exit status of the last commandPrint all these values clearly
💡 Why This Matters
🌍 Real World
Scripts often need to know their own name, what arguments were passed, and how commands executed to behave correctly.
💼 Career
Understanding special variables is essential for writing robust bash scripts used in automation, system administration, and DevOps tasks.
Progress0 / 4 steps