Mastering Long Options in Bash: A Practical Guide with getopt
This article explains why long command‑line options improve shell script usability, compares the built‑in getopt utility with manual parsing, and provides complete Bash examples that demonstrate defining short and long flags, handling them, and printing the parsed results.
Long options in shell scripts improve readability and avoid conflicts compared to short flags like -h.
Advantages of long options
Intuitive : words describe purpose.
Conflict‑free : more combinations than limited short flags.
Readable : users see meaning of each parameter.
Implementation approaches
Two main ways to support long options in Bash:
Use the external getopt command, which parses both short and long options.
Manually parse arguments with loops and string operations.
Using getopt
The following script demonstrates defining short ( hv) and long ( help,version,input:,output:) options, invoking getopt, evaluating the result, and handling each case.
#!/bin/bash
# Define short and long options
SHORT_OPTS="hv"
LONG_OPTS="help,version,input:,output:"
# Parse command‑line options
PARSED_OPTS=$(getopt -o $SHORT_OPTS -l $LONG_OPTS -- "$@")
if [[ $? -ne 0 ]]; then
exit 1
fi
# Set positional parameters to the parsed result
eval set -- "$PARSED_OPTS"
# Initialize variables
input_file=""
output_file=""
# Process options
while true; do
case "$1" in
-h|--help)
echo "Usage: $0 [options]"
echo " -h, --help Show help"
echo " -v, --version Show version"
echo " --input FILE Specify input file"
echo " --output FILE Specify output file"
shift
exit 0
;;
-v|--version)
echo "$0 version 1.0"
shift
exit 0
;;
--input)
input_file="$2"
shift 2
;;
--output)
output_file="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "Invalid option: $1"
exit 1
;;
esac
done
# Print results
echo "Input file: $input_file"
echo "Output file: $output_file"Manual parsing of long options
If getopt is unavailable, the script can manually iterate over arguments and handle each long flag similarly.
#!/bin/bash
# Initialize variables
input_file=""
output_file=""
# Process command‑line parameters
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
echo "Usage: $0 [options]"
echo " -h, --help Show help"
echo " -v, --version Show version"
echo " --input FILE Specify input file"
echo " --output FILE Specify output file"
exit 0
;;
-v|--version)
echo "$0 version 1.0"
exit 0
;;
--input)
input_file="$2"
shift 2
;;
--output)
output_file="$2"
shift 2
;;
*)
echo "Invalid option: $1"
exit 1
;;
esac
done
echo "Input file: $input_file"
echo "Output file: $output_file"Conclusion
Both getopt and manual parsing enable Bash scripts to accept descriptive long options, enhancing usability and script readability.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development & AI Practice
DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
