Command Line Options: How To Parse In Bash Using “getopt”

Use “getopt” in a Bash script to parse long and short command line options, enforce arguments for some options and spot dubious user input. Most of the times, when you need to automate system administration tasks on your Linux machine, writing a Bash script is your best bet. And sometimes, you need to be able to control the behaviour of your script at some point which leaves you with two choices: use environment variables (set before running the script) as sort of a flag or the better and more intuitive way to use command line arguments. What is "getopt"? getopt is a program that parses command line options in shell scripts. It is the enhanced version of older getopts and uses the getopt C library to do its job. It is compatible with getopts as long as GETOPT_COMPATIBLE environment variable is set, however some of it best features are not available in compatibility mode. An overview of command options Command line input is viewed in 3 categories by getopt: short ...