[Discuss] How to keep '-' in bash args [Thanks]

pw p.willis at telus.net
Thu May 3 08:45:10 PDT 2007


noel at natnix.com wrote:
> Whoa, that's a bit compicated.  Try using "$#" and "shift" instead of
> seq, eval, awk, etc:
> 
> # parse command-line args in bash
> while [ $# -gt 0 ]; do
>     case "$1" in
>         -opt1) shift; opt1_val="$1" ;;     # option with value arg
>         -opt2) opt2_val=true ;;            # plain option
>         --) shift; break ;;                # end of options
>         -*) echo "unrecognized option: $1"; exit 1 ;;
>         *) break ;; # not an option, start of arguments
>     esac
>     shift
> done
> 
> (One thing about getopt and friends is that it's very easy to write it
> yourself.)
> 
> --Noel

Noel,

Thanks for the help with bash.
This works like a hot damn.

Can you suggest any good bash books/references
or are the man pages as good as it gets?

Peter



More information about the Discuss mailing list