Are there some built-in tools that will recognize -x and --xxxx as switches, and not arguments, or do you have to go through all the input variables, test for dashes, and then parse the arguments thereafter?


Use getopts.

It is fairly portable as it is in the POSIX spec. Unfortunately it doesn't support long options.

See also this getopts tutorial courtesy of the bash-hackers wiki and this question from stackoverflow.

If you only need short options, typical usage pattern for getopts (using non-silent error reporting) is:

# process arguments "$1", "$2", ... (i.e. "$@")
while getopts "ab:" opt; do
    case $opt in
    a) aflag=true ;; # Handle -a
    b) barg=$OPTARG ;; # Handle -b argument
    \?) ;; # Handle error: unknown option or missing required argument.
| improve this answer | |
  • 3
    It should be mentioned that getopt should always be verified as GNU getopt before you use it, but you shouldn't use it anyway as getopts is more portable (and generally nicer) anyway. If you do need to call it for some reason, call it in a GNU-specific way, and ensure that GETOPT_COMPATIBLE is not in the environment. – Chris Down Sep 18 '11 at 20:38
  • What does the colon in while getopts "ab:" opt do? – user394 Oct 23 '19 at 18:25
  • 1
    @user394 The : after an option letter signifies it requires an argument. A : as the first character means to suppress error messages. – jw013 Oct 23 '19 at 21:17

I assume you're using bash or similar. An example:


while getopts ":hal" option; do
  case $option in
    h) echo "usage: $0 [-h] [-a] [-l] file ..."; exit ;;
    a) all=true ;;
    l) long=true ;;
    ?) echo "error: option -$OPTARG is not implemented"; exit ;;

# remove the options from the positional parameters
shift $(( OPTIND - 1 ))

$all && ls_opts+=( -a )
$long && ls_opts+=( -l )

# now, do it
ls "${ls_opts[@]}" "$@"
| improve this answer | |
  • 1
    +1 for using += with an array. Didn't know you could do that. Nice! – James Sneeringer Oct 14 '11 at 21:31

You have to write a cycle to parse the parameters. Indeed you can use getopts command to do it easily.

This is a simple example from getopts manual page:

while getopts ab: name
    case $name in
    a)    aflag=1;;
    b)    bflag=1
    ?)    printf "Usage: %s: [-a] [-b value] args\n" $0
          exit 2;;
if [ ! -z "$aflag" ]; then
    printf "Option -a specified\n"
if [ ! -z "$bflag" ]; then
    printf 'Option -b "%s" specified\n' "$bval"
shift $(($OPTIND - 1))
printf "Remaining arguments are: %s\n" "$*"
| improve this answer | |

I wrote a script recently for work that was versatile and allowed for multiple kinds of switches in any order. I can't disclose the full script for obvious legal reasons (not to mention I don't have it with me at the moment), but here's the meat of it.. you can put it in a subroutine and call it at the end of your script:

options () {

    if [ -n "$1" ]; then # test if any arguments passed - $1 will always exist
        while (( "$#" )); do  # process ALL arguments
            if [ "$1" = ^-t$ ]; then # -t short for "test"
                # do something here THEN shift the argument
                # shift removes it from $@ and reduces $# by
                # one if you supply no argument

            # we can also process multiple arguments at once
            elif [[ "$1" =~ ^--test=[:alnum:]{1,8}$ ]] && [[ "$2" =~ ^-t2$ ]] && [[ -n "$3" ]]; then # check for 3 arguments
                # do something more then remove them ALL from the arg list    
                shift 3
                echo "No matching arguments!"
                echo "Usage: [script options list here]"
        echo "Usage: [script options list here]"
        exit 0

options "$@" # run options and loop through/process ALL arguments

I do recommend limiting your bash script to less than 400 lines/15k characters; my aforementioned script grew past this size and became greatly difficult to work on. I started rewriting it in Perl, which is much better suited for the task. Keep that in mind as you work on your scripts in bash. Bash is great for small scripts and oneliners, but anything more complex and you're better off writing it in Perl.

Note, I haven't test the above, so it probably doesn't work, but you get the general idea from it.

| improve this answer | |
  • The way you call options at the end is not correct, it will return -bash: syntax error near unexpected token $@. Call it as options "$@". – Chris Down Sep 18 '11 at 22:46
  • Yup, I mixed up Perl and Bash. Corrected. – laebshade Sep 19 '11 at 2:32
  • That while condition should not be (($#)) instead? – manatwork Sep 19 '11 at 7:45
  • Why would you need two sets of parentheses for $#? Edit: you're right. Fixed it to while (( "$#" )) – laebshade Sep 20 '11 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.