Browse - Programming Tips - How can a bash script accept options with a dash (-) ?

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