Bash getopts : smart positional-parameter parsing


  • getopts optstring name [args]


optstringThe option characters to be recognized
nameThen name where parsed option is stored



optstring : The option characters to be recognized

If a character is followed by a colon, the option is expected to have an argument, which should be separated from it by white space. The colon (:) and question mark (?) can not be used as option characters.

Each time it is invoked, getopts places the next option in the shell variable name, initializing name if it does not exist, and the index of the next argument to be processed into the variable OPTIND. OPTIND is initialized to 1 each time the shell or a shell script is invoked.

When an option requires an argument, getopts places that argument into the variable OPTARG. The shell does not reset OPTIND automatically; it must be manually reset between multiple calls to getopts within the same shell invocation if a new set of parameters is to be used.

When the end of options is encountered, getopts exits with a return value greater than zero.

OPTIND is set to the index of the first non-option argument, and name is set to ?. getopts normally parses the positional parameters, but if more arguments are given in args, getopts parses those instead.

getopts can report errors in two ways. If the first character of optstring is a colon (:), silent error reporting is used. In normal operation diagnostic messages are printed when invalid options or missing option arguments are encountered.

If the variable OPTERR is set to 0, no error messages will be displayed, even if the first character of optstring is not a colon.

If an invalid option is seen, getopts places ? into name and, if not silent, prints an error message and unsets OPTARG. If getopts is silent, the option character found is placed in OPTARG and no diagnostic message is printed.

If a required argument is not found, and getopts is not silent, a question mark (?) is placed in name, OPTARG is unset, and a diagnostic message is printed. If getopts is silent, then a colon (:) is placed in name and OPTARG is set to the option character.


# Script name : pingnmap
# Scenario : The systems admin in company X is tired of the monotonous job
# of pinging and nmapping, so he decided to simplify the job using a script.
# The tasks he wish to achieve is
# 1. Ping - with a max count of 5 -the given IP address/domain. AND/OR
# 2. Check if a particular port is open with a given IP address/domain.
# And getopts is for her rescue.
# A brief overview of the options
# n : meant for nmap
# t : meant for ping
# i : The option to enter the IP address
# p : The option to enter the port
# v : The option to get the script version

while getopts ':nti:p:v' opt
#putting : in the beginnnig suppresses the errors for invalid options
case "$opt" in
   'v')echo "pingnmap version 1.0.0"
    *) echo "Invalid option $opt"
       echo "Usage : "
       echo "pingmap -[n|t[i|p]|v]"
if  [ ! -z "$nmap_yes" ] && [ "$nmap_yes" -eq "1" ]
   if [ ! -z "$ip" ] && [ ! -z "$port" ]
     nmap -p "$port" "$ip"

if  [ ! -z "$ping_yes" ] && [ "$ping_yes" -eq "1" ]
   if [ ! -z "$ip" ]
     ping -c 5 "$ip"
shift $(( OPTIND - 1 )) # Processing additional arguments
if [ ! -z "[email protected]" ]
  echo "Bogus arguments at the end : [email protected]"


$ ./pingnmap -nt -i -p 80

Starting Nmap 6.40 ( ) at 2016-07-23 14:31 IST
Nmap scan report for (
Host is up (0.034s latency).
rDNS record for
80/tcp open  http

Nmap done: 1 IP address (1 host up) scanned in 0.22 seconds
PING ( 56(84) bytes of data.
64 bytes from ( icmp_seq=1 ttl=57 time=29.3 ms
64 bytes from ( icmp_seq=2 ttl=57 time=30.9 ms
64 bytes from ( icmp_seq=3 ttl=57 time=34.7 ms
64 bytes from ( icmp_seq=4 ttl=57 time=39.6 ms
64 bytes from ( icmp_seq=5 ttl=57 time=32.7 ms

--- ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4007ms
rtt min/avg/max/mdev = 29.342/33.481/39.631/3.576 ms
$ ./pingnmap -v
pingnmap version 1.0.0
$ ./pingnmap -h
Invalid option ?
Usage : 
pingmap -[n|t[i|p]|v]
$ ./pingnmap -v
pingnmap version 1.0.0
$ ./pingnmap -h
Invalid option ?
Usage : 
pingmap -[n|t[i|p]|v]