0
0
Bash Scriptingscripting~10 mins

Long option parsing in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Long option parsing
Start
Read argument
Is argument a long option?
Match option
Execute option action
More arguments?
End
The script reads each argument, checks if it is a long option, matches it, executes its action, and continues until no arguments remain.
Execution Sample
Bash Scripting
#!/bin/bash
while [[ $# -gt 0 ]]; do
  case "$1" in
    --help) echo "Help info"; shift;;
    --version) echo "Version 1.0"; shift;;
    *) echo "Unknown option: $1"; shift;;
  esac
done
This script loops over all arguments, checks for long options --help and --version, prints messages, and shifts to next argument.
Execution Table
StepArgument ($1)ConditionActionOutputNext Argument Count
1--helpMatches --helpPrint help, shiftHelp info2
2--versionMatches --versionPrint version, shiftVersion 1.01
3--unknownNo matchPrint unknown, shiftUnknown option: --unknown0
4No argumentsLoop endsExit loop0
💡 No arguments left, loop ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$1--help--version--unknown
$#32100
Key Moments - 3 Insights
Why does the script use 'shift' after handling each option?
Because 'shift' removes the first argument ($1), so the next argument becomes $1 for the next loop iteration, as shown in execution_table steps 1-3.
What happens if an argument does not match any known long option?
The script prints 'Unknown option' and still shifts to the next argument, as seen in step 3 of the execution_table.
How does the script know when to stop processing arguments?
It stops when the argument count ($#) becomes zero, meaning no more arguments, as shown in step 4 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $1 after step 2?
A"--help"
B"--version"
C"--unknown"
D"" (empty)
💡 Hint
Check the variable_tracker row for $1 after step 2.
At which step does the script print 'Unknown option: --unknown'?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the output column in the execution_table.
If the script did not use 'shift', what would happen to the loop?
AIt would skip arguments.
BIt would process the same argument forever.
CIt would exit immediately.
DIt would print nothing.
💡 Hint
Refer to the key_moments explanation about 'shift' and argument processing.
Concept Snapshot
Long option parsing in bash:
- Use a while loop with [[ $# -gt 0 ]] to process all args
- Use case "$1" to match long options like --help
- Use shift to move to next argument after handling
- Print messages or handle unknown options
- Loop ends when no args remain
Full Transcript
This example shows how a bash script processes long options. It loops over all command-line arguments using a while loop checking if argument count is greater than zero. Inside, it uses a case statement to match the first argument ($1) against known long options like --help and --version. When a match is found, it prints a message and uses shift to remove that argument so the next one becomes $1. If no match is found, it prints an unknown option message and still shifts. The loop ends when no arguments remain. This method ensures each argument is handled one by one until all are processed.