Command-line arguments ($1, $2, ...) in Bash Scripting - Time & Space Complexity
When using command-line arguments in a bash script, it's important to know how the script's work changes as you add more arguments.
We want to see how the script's running time grows when it reads and uses these arguments.
Analyze the time complexity of the following code snippet.
#!/bin/bash
for arg in "$@"; do
echo "Argument: $arg"
done
# This script prints each command-line argument one by one.
This script loops through all given command-line arguments and prints each one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each command-line argument.
- How many times: Once for each argument provided.
Each new argument adds one more loop step, so the work grows steadily with the number of arguments.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 echo commands |
| 100 | 100 echo commands |
| 1000 | 1000 echo commands |
Pattern observation: The work increases directly in proportion to the number of arguments.
Time Complexity: O(n)
This means the script takes longer in a straight line as you add more arguments.
[X] Wrong: "The script runs in the same time no matter how many arguments there are."
[OK] Correct: Each argument adds a step to the loop, so more arguments mean more work and more time.
Understanding how loops over inputs affect time helps you explain script efficiency clearly and confidently in real-world tasks.
"What if the script only printed the first argument instead of all? How would the time complexity change?"
Practice
$1 represent in a bash script?Solution
Step 1: Understand command-line arguments in bash
In bash scripts,$1refers to the first argument given after the script name.Step 2: Differentiate from other special variables
$#is the count of arguments,$0is the script name, and$@is all arguments.Final Answer:
The first command-line argument passed to the script -> Option DQuick Check:
$1= first argument [OK]
- Confusing $1 with $# (argument count)
- Thinking $1 is the script name
- Mixing $1 with $0 or $@
Solution
Step 1: Recall argument numbering in bash
Arguments start from$1for the first, so the third argument is$3.Step 2: Identify incorrect options
$2is second argument,$#is count,$0is script name.Final Answer:
$3 -> Option CQuick Check:
Third argument = $3 [OK]
- Using $2 instead of $3 for third argument
- Confusing $# with argument value
- Using $0 which is script name
./script.sh apple banana cherry?
echo "First: $1" echo "Second: $2" echo "Count: $#"
Solution
Step 1: Identify argument values
Arguments are apple ($1), banana ($2), cherry ($3). Script name is $0.Step 2: Understand output commands
echo prints $1 as apple, $2 as banana, and $# as 3 (number of arguments).Final Answer:
First: apple Second: banana Count: 3 -> Option AQuick Check:
Arguments match output [OK]
- Mixing script name ($0) with arguments
- Confusing argument order
- Miscounting number of arguments
echo "Argument 1: $1" echo "Argument 2: $2"What is the error if you run the script without any arguments?
Solution
Step 1: Understand missing arguments behavior
If no arguments are passed, $1 and $2 are empty strings, not errors.Step 2: Check script output
echo prints "Argument 1: " and "Argument 2: " with nothing after colon.Final Answer:
Script prints empty lines for $1 and $2 -> Option AQuick Check:
Missing args = empty $1, $2 [OK]
- Expecting syntax or runtime error
- Thinking script name prints as argument
- Assuming no output at all
$1, $2, etc. Which approach works best?Solution
Step 1: Understand how to access all arguments one by one
Usingshiftmoves arguments left, so $1 becomes next argument each time.Step 2: Compare options
Use a loop with a counter and access arguments by $1, $2, ... until counter > $# is complex and error-prone; Useecho $@to print all arguments on one line prints all on one line; Use$0to print all arguments one by one prints script name; Useshiftcommand in a loop to access each argument and print it correctly loops and prints each argument on new line.Final Answer:
Useshiftcommand in a loop to access each argument and print it -> Option BQuick Check:
Shift loops through args one by one [OK]
- Using $0 instead of arguments
- Trying to manually count without shift
- Printing all args on one line instead of separate lines
Ready to prove it?
Pass the test and this pattern moves unknown
