Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Command-line arguments ($1, $2, ...) in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Command-line arguments ($1, $2, ...)
Start script
↓
Read $1
↓
Read $2
↓
... Read $n
↓
Use arguments in script
↓
End script
The script starts and reads each command-line argument in order ($1, $2, ...), then uses them as needed before ending.
Execution Sample
Bash Scripting
#!/bin/bash

echo "First argument: $1"
echo "Second argument: $2"
This script prints the first and second command-line arguments passed to it.
Execution Table
StepActionValue of $1Value of $2Output
1Start scriptapplebanana
2Print first argumentapplebananaFirst argument: apple
3Print second argumentapplebananaSecond argument: banana
4End scriptapplebanana
💡 Script ends after printing the first and second arguments.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$1unsetappleappleappleapple
$2unsetbananabananabananabanana
Key Moments - 2 Insights
Why does $1 hold the first argument and not the script name?
In bash, $0 holds the script name, while $1, $2, etc. hold the first, second, and subsequent arguments. See execution_table step 1 where $1 is 'apple' and not the script name.
What happens if I run the script without any arguments?
If no arguments are given, $1 and $2 are empty strings. The script will print 'First argument: ' and 'Second argument: ' with nothing after the colons, as no values are assigned.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 2?
AFirst argument: apple
BSecond argument: banana
CFirst argument: banana
DSecond argument: apple
💡 Hint
Check the 'Output' column at step 2 in the execution_table.
At which step does the script print the second argument?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column to find when the second argument is printed.
If you run the script with no arguments, what will $1 be?
AThe script name
BError message
CEmpty string
DThe first argument typed previously
💡 Hint
Refer to key_moments about what happens when no arguments are passed.
Concept Snapshot
Command-line arguments in bash are accessed as $1, $2, etc.
$0 is the script name.
Arguments are strings passed after the script name.
Use them to customize script behavior.
If no argument is given, $n is empty.
Example: echo "$1" prints the first argument.
Full Transcript
This lesson shows how bash scripts use command-line arguments. The script reads arguments as $1, $2, and so on. $0 is reserved for the script name. The example script prints the first two arguments. The execution table traces each step: starting the script, printing $1, printing $2, and ending. Variables $1 and $2 hold the values passed when running the script. If no arguments are given, these variables are empty. Key moments clarify common confusions like why $1 is not the script name and what happens if no arguments are passed. The quiz tests understanding by asking about outputs and variable values at specific steps. The snapshot summarizes how to use command-line arguments in bash scripts.

Practice

(1/5)
1. What does $1 represent in a bash script?
easy
A. The last command-line argument passed to the script
B. The total number of arguments passed to the script
C. The name of the script file
D. The first command-line argument passed to the script

Solution

  1. Step 1: Understand command-line arguments in bash

    In bash scripts, $1 refers to the first argument given after the script name.
  2. Step 2: Differentiate from other special variables

    $# is the count of arguments, $0 is the script name, and $@ is all arguments.
  3. Final Answer:

    The first command-line argument passed to the script -> Option D
  4. Quick Check:

    $1 = first argument [OK]
Hint: Remember $1 is always the first argument after script name [OK]
Common Mistakes:
  • Confusing $1 with $# (argument count)
  • Thinking $1 is the script name
  • Mixing $1 with $0 or $@
2. Which of the following is the correct way to access the third argument in a bash script?
easy
A. $0
B. $2
C. $3
D. $#

Solution

  1. Step 1: Recall argument numbering in bash

    Arguments start from $1 for the first, so the third argument is $3.
  2. Step 2: Identify incorrect options

    $2 is second argument, $# is count, $0 is script name.
  3. Final Answer:

    $3 -> Option C
  4. Quick Check:

    Third argument = $3 [OK]
Hint: Count arguments starting at $1, so third is $3 [OK]
Common Mistakes:
  • Using $2 instead of $3 for third argument
  • Confusing $# with argument value
  • Using $0 which is script name
3. What will be the output of this script if run as ./script.sh apple banana cherry?
echo "First: $1"
echo "Second: $2"
echo "Count: $#"
medium
A. First: apple Second: banana Count: 3
B. First: apple Second: cherry Count: 3
C. First: ./script.sh Second: apple Count: 3
D. First: banana Second: cherry Count: 2

Solution

  1. Step 1: Identify argument values

    Arguments are apple ($1), banana ($2), cherry ($3). Script name is $0.
  2. Step 2: Understand output commands

    echo prints $1 as apple, $2 as banana, and $# as 3 (number of arguments).
  3. Final Answer:

    First: apple Second: banana Count: 3 -> Option A
  4. Quick Check:

    Arguments match output [OK]
Hint: Remember $1, $2 are first and second args; $# is count [OK]
Common Mistakes:
  • Mixing script name ($0) with arguments
  • Confusing argument order
  • Miscounting number of arguments
4. Given this script snippet:
echo "Argument 1: $1"
echo "Argument 2: $2"
What is the error if you run the script without any arguments?
medium
A. Script prints empty lines for $1 and $2
B. No output is printed
C. Script throws a syntax error
D. Script prints the script name instead of arguments

Solution

  1. Step 1: Understand missing arguments behavior

    If no arguments are passed, $1 and $2 are empty strings, not errors.
  2. Step 2: Check script output

    echo prints "Argument 1: " and "Argument 2: " with nothing after colon.
  3. Final Answer:

    Script prints empty lines for $1 and $2 -> Option A
  4. Quick Check:

    Missing args = empty $1, $2 [OK]
Hint: No args means $1, $2 are empty, no errors [OK]
Common Mistakes:
  • Expecting syntax or runtime error
  • Thinking script name prints as argument
  • Assuming no output at all
5. You want a bash script to print all arguments passed to it, each on a new line, using only $1, $2, etc. Which approach works best?
hard
A. Use a loop with a counter and access arguments by $1, $2, ... until counter > $#
B. Use shift command in a loop to access each argument and print it
C. Use $0 to print all arguments one by one
D. Use echo $@ to print all arguments on one line

Solution

  1. Step 1: Understand how to access all arguments one by one

    Using shift moves arguments left, so $1 becomes next argument each time.
  2. Step 2: Compare options

    Use a loop with a counter and access arguments by $1, $2, ... until counter > $# is complex and error-prone; Use echo $@ to print all arguments on one line prints all on one line; Use $0 to print all arguments one by one prints script name; Use shift command in a loop to access each argument and print it correctly loops and prints each argument on new line.
  3. Final Answer:

    Use shift command in a loop to access each argument and print it -> Option B
  4. Quick Check:

    Shift loops through args one by one [OK]
Hint: Use shift in loop to print each argument separately [OK]
Common Mistakes:
  • Using $0 instead of arguments
  • Trying to manually count without shift
  • Printing all args on one line instead of separate lines
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard