Argument Parsing
The getopts utility is a built-in Bash command that provides a structured, portable way to parse command-line options. Unlike manually iterating through $@, getopts handles error checking, positional arguments, and option validation, making it ideal for robust script development. It adheres to POSIX standards, ensuring compatibility across Unix-like systems.
Basic Usage¶
The getopts command takes a string of option letters and processes them sequentially. Each letter represents a short option, and a colon (:) indicates an option that requires an argument. Options are processed in a while loop, with results stored in the special variables $OPTARG (argument for the current option) and $OPTARG (the option character itself).
Example: Simple Option Parsing¶
#!/bin/bash
while getopts "f:v" opt; do
case $opt in
f) echo "File option: $OPTARG" ;;
v) echo "Verbose mode enabled" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
Explanation:
- "f:v" defines two options: -f (requires an argument) and -v (no argument needed).
- The while loop processes each option until non-option arguments remain.
- The case statement handles each option, with \? catching invalid options.
Handling Positional Arguments¶
After getopts processes all options, positional arguments (non-option arguments) are accessible via ${@:OPTIND}. OPTIND is a global variable tracking the index of the next argument to process.
Example: Combining Options and Positional Args¶
#!/bin/bash
while getopts "f:v" opt; do
case $opt in
f) FILE="$OPTARG" ;;
v) VERBOSE=1 ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
# Remaining positional arguments
shift $((OPTIND - 1))
echo "Remaining arguments: $@"
if [ -n "$FILE" ]; then
echo "Processing file: $FILE"
fi
Key Points:
- shift $((OPTIND - 1)) moves the positional arguments to $@.
- This pattern is useful for scripts that accept both options and files/filenames.
Error Handling¶
By default, getopts prints error messages to stderr when invalid options are encountered. You can suppress these by setting OPTERR=0 and manually check for errors.
Example: Custom Error Handling¶
#!/bin/bash
OPTERR=0 # Disable default error messages
while getopts "f:v:" opt; do
case $opt in
f) FILE="$OPTARG" ;;
v) VERBOSE=1 ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
if [ -z "$FILE" ]; then
echo "Error: -f is required" >&2
exit 1
fi
Best Practices:
- Always validate required options after parsing.
- Use >&2 for error messages to ensure they appear on stderr.
Advanced Usage¶
Options with Arguments¶
Options requiring arguments (e.g., -f filename) store the argument in $OPTARG. For example:
Long Options (Not Supported)¶
getopts does not handle long options (e.g., --file). For long options, use the external getopt utility or manually parse $@.
Key takeaways¶
getoptsprovides structured, portable option parsing for Bash scripts.- Use
while getoptswith acasestatement to handle options and validate inputs. OPTINDandOPTARGtrack remaining arguments and option values.- Suppress default error messages with
OPTERR=0and implement custom validation. - Combine
getoptswith positional arguments for flexible command-line interfaces.