0
0
Bash Scriptingscripting~10 mins

Option parsing with getopts in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Option parsing with getopts
Start script
Initialize getopts
Read next option
Option found?
NoExit loop
Yes
Match option with case
Execute option action
Loop back to read next option
The script starts and initializes getopts to read options one by one. For each option found, it matches and executes the related action, looping until no more options.
Execution Sample
Bash Scripting
while getopts ":ab:" opt; do
  case $opt in
    a) echo "Option a set" ;;
    b) echo "Option b set with value $OPTARG" ;;
    ?) echo "Invalid option: -$OPTARG" ;;
  esac
done
This script reads options -a and -b (which requires a value). It prints messages based on the options given.
Execution Table
StepOPTINDoptOPTARGActionOutput
11aMatch 'a' caseOption a set
22bvalueMatch 'b' caseOption b set with value value
34?xInvalid optionInvalid option: -x
45No more options
💡 No more options to process, getopts returns false and loop ends
Variable Tracker
VariableStartAfter 1After 2After 3Final
OPTIND12455
optab?
OPTARGvaluex
Key Moments - 3 Insights
Why does getopts use OPTIND and how does it change?
OPTIND is the index of the next argument to process. It starts at 1 and increments after each option is read, as shown in the execution_table under OPTIND.
What happens when getopts encounters an invalid option?
getopts sets opt to '?' and OPTARG to the invalid option character. The script can handle this case to show an error message, as in step 3 of the execution_table.
Why does option 'b' have a value and 'a' does not?
In the getopts string ':ab:', the colon after 'b' means it requires a value. So when 'b' is found, OPTARG holds its value, shown in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of OPTARG at step 2?
A"a"
B"value"
C"b"
D"x"
💡 Hint
Check the OPTARG column in row for step 2 in execution_table
At which step does getopts detect an invalid option?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the row where opt is '?' in execution_table
If option 'a' required a value, how would the execution_table change?
AOPTARG would hold a value for 'a'
BOPTARG would be empty for 'a'
Copt would be '?' for 'a'
DOPTIND would not increment
💡 Hint
Recall that options with ':' require a value, setting OPTARG accordingly
Concept Snapshot
Use getopts in a while loop to parse options.
Options with ':' require a value, stored in OPTARG.
OPTIND tracks the next argument index.
Use case to handle each option.
'?' handles invalid options.
Loop ends when no more options.
Full Transcript
This visual execution shows how bash's getopts parses command-line options. The script loops, reading one option at a time. OPTIND starts at 1 and moves forward after each option. For each option, the script matches it in a case statement and runs code. Options with a colon require a value, stored in OPTARG. If an invalid option is found, opt is set to '?'. The loop ends when getopts finds no more options. Variables OPTIND, opt, and OPTARG change step by step as shown. This helps beginners see exactly how option parsing works in bash scripts.