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

pw p.willis at telus.net
Wed May 2 11:25:40 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


Thanks Noel,

That looks a lot simpler.
Isn't $1 only the first command line argument though?
I'll try it out.

Peter



More information about the Discuss mailing list