getopts
getopts
In Linux, the getopts function is used to parse command-line options. It is a built-in function that is available in most shells.
The optstring argument is a string that specifies the options that you want to parse. The var argument is the name of the variable that will store the parsed options.
For example, the following code will parse the options -a, -b, and -c:
while getopts ":abc" opt; do
case $opt in
a)
echo "Option -a was specified."
;;
b)
echo "Option -b was specified."
;;
c)
echo "Option -c was specified."
;;
*)
echo "Unknown option: $opt"
;;
esac
done
help
Options:
-n, --no-argument Don't assign an argument to the option.
-r, --require-argument The option must have an argument.
-u, --uppercase Use uppercase letters for the option names.
breakdown
The : character indicates that the option takes an argument.
The - character indicates that the option is optional.
The a, b, and c characters are the names of the options.
Last updated