Dave's Brain

Browse - programming tips - bash script accept options with getopts

Date: 2010feb21
Language: bash

Q.  How can a bash script accept options with a dash (-) ?

A.  Use the builtin getopts (not the external getopt) like this:

	#!/bin/bash

	VERBOSE=no
	while getopts "v" flag; do
        	## echo "$flag" $OPTIND $OPTARG
        	if [[ $flag == v ]]; then
                	VERBOSE=yes
        	fi
	done

	... later on ...

	if [[ $VERBOSE == yes ]]; then
		echo blah
	fi
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.