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.
#!/bin/bash echo "First argument: $1" echo "Second argument: $2"
| Step | Action | Value of $1 | Value of $2 | Output |
|---|---|---|---|---|
| 1 | Start script | apple | banana | |
| 2 | Print first argument | apple | banana | First argument: apple |
| 3 | Print second argument | apple | banana | Second argument: banana |
| 4 | End script | apple | banana |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| $1 | unset | apple | apple | apple | apple |
| $2 | unset | banana | banana | banana | banana |
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.