Home » Get Shell Script Options with Getopts

Get Shell Script Options with Getopts

by Online Tutorials Library

Shell Scripting getopts options

The getopts options are used in shell scripts to parse arguments passed to them. When arguments are passed on the command line, getopts parse those arguments instead of command lines.

Options are written starting with a hyphen (-), followed by the letter. For example, -a, -b, -c, -d, etc.

Example 1 without argument:

Linux Shell Scripting Get script options with getopts 1

Look at the above snapshot, we have created three options -a, -b and-c. And no arguments are required for any option.

Linux Shell Scripting Get script options with getopts 2

Look at the above snapshot, this is the sample output from the above script.

Example 2 with argument:

Linux Shell Scripting Get script options with getopts 3

Look at the above snapshot, argument is required with option b.

Linux Shell Scripting Get script options with getopts 4

Look at the above snapshot, this is the sample output of above script.

Next Topicif then else

You may also like