CLI, passing the arguments to the command line
Geopts is a Bash built-in widely used to parse switches and arguments passed on the command line of a script. We already saw other ways to this task, but getops makes it quite easy to handle it, since it can automatically recognize the switches and argument passed to the script. Its syntax is as follows:
getops options variableThe first thing we pass to getops is a string of options, the classical -a -x -f of whatever you want, without any leading dash, such asgetops axfor alsogetops ax:f. If you see an option followed by a colon, this means that the option is meant to have an argument such as follows:
./our_script.sh -x our_argument -aIn our example, -x has an argument while -a is a simple switch, or we can call it flag that can just be there or not, but it does not require any arguments. The options can be specified as lower or upper characters or digits. The getops built-in has some predefined variables for its internal use:
OPTARGholds...