0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 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.