Parsing Command Line Options in Shell Scripts

In programs written in C, parsing of command line arguments has always been done using the getopt(3) library function. This function has set the standards Linux/Unix users have come to expect from command line interfaces. Fortunately, there’s a getopt(3) equivalent for almost every programming language and the shell is no exception.

The getopts command available in all POSIX-compliant Bourne shell derivates (like bash, dash, or ksh) provides convenient command line parsing capabilities. It is a builtin accepting POSIX-style argument lists (as opposed to GNU-style, which is a bit more fancy) and should not be confused with the getopt utility.

For my shell scripts, I use the following template to implement command line parsing:

#! /bin/sh

USAGE="Usage: `basename $0` [-hv] [-o arg] args"

# we want at least one parameter (it may be a flag or an argument)
if [ $# -eq 0 ]; then
	echo $USAGE >&2
	exit 1
fi

# parse command line arguments
while getopts hvo: OPT; do
    case "$OPT" in
	h)	echo $USAGE
		exit 0
		;;
	v)	echo "`basename $0` version 0.1"
		exit 0
		;;
	o)	OUTPUT_FILE=$OPTARG
		;;
	\?)	# getopts issues an error message
		echo $USAGE >&2
		exit 1
		;;
    esac
done

shift `expr $OPTIND - 1`

# access additional parameters through $@ or $* as usual or using this loop:
for PARAM; do
    echo $PARAM
done

# EOF

More command line switches can be added to the loop. If you want to add an -x switch requiring an argument (-x arg) and a y flag without an argument, you would have to change the getopts call to getopts hvo:x:y OPT and add two case labels to the loop. Note that the colon indicates that an argument is required for a flag.

Explore posts in the same categories: shell

Tags: , ,

You can comment below, or link to this permanent URL from your own site.

3 Comments on “Parsing Command Line Options in Shell Scripts”


  1. [...] on the ideas given in a Ubuntu forum thread and a template on command line parsing, I wrote a simple script “parallel” that allows you to run [...]

  2. meowsqueak Says:

    Useful, thanks.

  3. mark foltz Says:

    thanks, this was a great help. beats digging through the man pages.


Comment: