Special variables ($0, $1, $#, $@, $?, $) in Bash Scripting - Time & Space Complexity
We want to understand how the use of special variables in a bash script affects how long the script takes to run.
Specifically, we ask: how does the script's running time grow as the number of input arguments changes?
Analyze the time complexity of the following code snippet.
#!/bin/bash
# Print script name
echo "Script name: $0"
# Print first argument
echo "First argument: $1"
# Print number of arguments
echo "Number of arguments: $#"
# Print all arguments
for arg in "$@"; do
echo "Arg: $arg"
done
# Print last command exit status
echo "Last command status: $?"
# Print current process ID
echo "Process ID: $$"
This script prints special variables: script name, arguments, count, all arguments, last command status, and process ID.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop that goes through all input arguments using$@. - How many times: It runs once for each argument passed to the script, so as many times as the number of arguments.
As the number of input arguments grows, the loop runs more times, printing each argument.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 loop prints |
| 100 | About 100 loop prints |
| 1000 | About 1000 loop prints |
Pattern observation: The number of operations grows directly with the number of arguments.
Time Complexity: O(n)
This means the script takes longer roughly in direct proportion to how many arguments it receives.
[X] Wrong: "Using special variables like $@ or $# does not affect how long the script runs."
[OK] Correct: The loop over $@ runs once per argument, so more arguments mean more work and longer run time.
Understanding how loops over input arguments affect script speed shows you can reason about script efficiency and handle inputs well.
"What if we replaced the for loop over $@ with a single print of all arguments at once? How would the time complexity change?"
Practice
$# represent?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
$0is 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 CQuick Check:
$#= argument count [OK]
- Confusing $# with $@ which holds all arguments
- Thinking $0 is the argument count
- Mixing up $? with argument count
Solution
Step 1: Identify the variable for the first argument
$1holds the first argument passed to the script.Step 2: Check other options
$0is 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 AQuick Check:
First argument = $1 [OK]
- Using $0 instead of $1 for first argument
- Confusing $# with argument value
- Trying to print $$ for argument
./myscript.sh apple banana with this content:echo "$0" echo "$1" echo "$#" echo "$@" echo "$$"
What will be the output of the third line
echo "$#"?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 DQuick Check:
Arguments count = 2 [OK]
- Counting script name as argument
- Confusing $@ with $#
- Expecting argument values instead of count
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?Solution
Step 1: Understand when $? is evaluated
$?holds the exit status of the last command executed before it.Step 2: Check command order
Theechoruns first, so it shows the status of the command beforeecho, notls.Final Answer:
Theechoruns beforels, so $? is from previous command -> Option BQuick Check:
Check command order for $? [OK]
- Using $$ instead of $? for exit status
- Printing $? before running the command
- Assuming $? holds script PID
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 AQuick Check:
PID = $$, all args = $@ [OK]
- Using $0 as PID instead of $$
- Using $# (count) instead of $@ (all args)
- Mixing $1 (first arg) with PID
Ready to prove it?
Pass the test and this pattern moves unknown
