Dave's Brain

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

Add a comment

Sign in to add a comment
Copyright © 2008-2017, 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.