bash tools for parsing arguments

Posted by BCS on Stack Overflow See other posts from Stack Overflow or by BCS
Published on 2010-05-17T17:31:39Z Indexed on 2010/05/17 17:40 UTC
Read the original article Hit count: 159

I have a bash script that uses a few variables (call them $foo and $bar). Right now the script defines them at the top with hard coded values like this:

foo=fooDefault
bar=barDefault

....

# use $foo and $bar

What I want is to be able to use the script like any of these:

myscript              # use all defaults
myscript -foo=altFoo  # use default bar
myscript -bar=altBar  # use default foo
myscript -bar=altBar -foo=altFoo

An ideal solution would allow me to just list the variable that I want to check for flags for.

Is there a reasonably nice way to do this?


I've seen getopt and I think it might do about 70% of what I'm looking for but I'm wondering if there is a tool or indium that builds on it or the like that gets the rest.

© Stack Overflow or respective owner

Related posts about bash

Related posts about command-line-arguments