📖 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 $1
Create a variable to hold the number of arguments using $#
Create a variable to hold all arguments using $@
Use $0 to get the script name
Use $$ to get the current process ID
Use $? to get the exit status of the last command
Print 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
1
Create variables for script name and first argument
Create a variable called script_name and set it to $0. Then create a variable called first_arg and set it to $1.
Bash Scripting
Hint
Use script_name=$0 to get the script name and first_arg=$1 to get the first argument.
2
Add variables for argument count and all arguments
Add a variable called arg_count and set it to $#. Add another variable called all_args and set it to $@.
Bash Scripting
Hint
Use arg_count=$# for number of arguments and all_args="$@" for all arguments.
3
Add variables for last command status and process ID
Add a variable called last_status and set it to $?. Add a variable called process_id and set it to $$.
Bash Scripting
Hint
Use last_status=$? for last command exit status and process_id=$$ for current process ID.
4
Print all special variable values
Print the values of script_name, first_arg, arg_count, all_args, last_status, and process_id using echo. Each value should be on its own line with a clear label.
Bash Scripting
Hint
Use echo to print each variable with a label. For example, echo "Script name: $script_name".
Practice
(1/5)
1. In a bash script, what does the special variable $# represent?
easy
A. The exit status of the last command
B. The name of the script
C. The number of arguments passed to the script
D. The process ID of the script
Solution
Step 1: Understand the role of $#
The variable $# in bash holds the count of all arguments passed to the script.
Step 2: Differentiate from other special variables
$0 is the script name, $$ is the process ID, and $? is the last command's exit status, so they don't represent argument count.
Final Answer:
The number of arguments passed to the script -> Option C
Quick Check:
$# = argument count [OK]
Hint: Remember: '#' means count, so $# counts arguments [OK]
Common Mistakes:
Confusing $# with $@ which holds all arguments
Thinking $0 is the argument count
Mixing up $? with argument count
2. Which of the following is the correct way to print the first argument passed to a bash script?
easy
A. echo $1
B. echo $0
C. echo $#
D. echo $$
Solution
Step 1: Identify the variable for the first argument
$1 holds the first argument passed to the script.
Step 2: Check other options
$0 is the script name, $# is the argument count, and $$ is the script's process ID, so they are incorrect for printing the first argument.
Final Answer:
echo $1 -> Option A
Quick Check:
First argument = $1 [OK]
Hint: Use $1 to access the first argument in bash scripts [OK]
Common Mistakes:
Using $0 instead of $1 for first argument
Confusing $# with argument value
Trying to print $$ for argument
3. Consider the script ./myscript.sh apple banana with this content:
echo "$0"
echo "$1"
echo "$#"
echo "$@"
echo "$$"
What will be the output of the third line echo "$#"?
medium
A. myscript.sh
B. 3
C. apple
D. 2
Solution
Step 1: Count the arguments passed
The script is called with two arguments: 'apple' and 'banana'. So, the count is 2.
Step 2: Understand what $# prints
$# prints the number of arguments, which is 2 here.
Final Answer:
2 -> Option D
Quick Check:
Arguments count = 2 [OK]
Hint: Count arguments after script name for $# [OK]
Common Mistakes:
Counting script name as argument
Confusing $@ with $#
Expecting argument values instead of count
4. You wrote this script snippet:
echo "Last command status: $?"
ls /nonexistent
But it always prints Last command status: 0 even if the directory doesn't exist. What is the problem?
medium
A. The ls command is correct, no problem
B. The echo runs before ls, so $? is from previous command
C. You should use $$ instead of $?
D. You need to quote $? differently
Solution
Step 1: Understand when $? is evaluated
$? holds the exit status of the last command executed before it.
Step 2: Check command order
The echo runs first, so it shows the status of the command before echo, not ls.
Final Answer:
The echo runs before ls, so $? is from previous command -> Option B
Quick Check:
Check command order for $? [OK]
Hint: Print $? immediately after the command to get correct status [OK]
Common Mistakes:
Using $$ instead of $? for exit status
Printing $? before running the command
Assuming $? holds script PID
5. You want to write a bash script that prints its own process ID and all arguments passed to it on separate lines. Which script snippet correctly does this?
hard
A. echo "PID: $$"
echo "Args: $@"
B. echo "PID: $0"
echo "Args: $#"
C. echo "PID: $?"
echo "Args: $1"
D. echo "PID: $1"
echo "Args: $$"
Solution
Step 1: Identify variable for process ID
$$ holds the current script's process ID.
Step 2: Identify variable for all arguments
$@ holds all arguments passed to the script as separate words.
Final Answer:
echo "PID: $$"
echo "Args: $@" -> Option A
Quick Check:
PID = $$, all args = $@ [OK]
Hint: Use $$ for PID and $@ for all arguments [OK]
Common Mistakes:
Using $0 as PID instead of $$
Using $# (count) instead of $@ (all args)
Mixing $1 (first arg) with PID
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.