bash tools for parsing arguments -


I have a Bash script that uses some variables (they are $ foo and < Code> $ bar ). Now the script defines them like this on top with hard coded values:

  foo = fooDefault bar = barDefault .... # $ foo and use $ bar  < / Pre> 

What I need to do is to use the script in any of these ways:

  myscript # All use myscript -foo = altFoo # default time myscript-bar = altBar # default Use Foo myscript-bar = altBar -foo = altFoo  

An ideal solution will allow me to list only the variables that I flag Investigations would like to.

Is there any good way to do this?


I have seen and I think about 70% of what I am looking for, I am wondering if there is any device or indium that builds on it or the rest Just like it.

You can use eval to close something:

 < Default for argument in code> foo = fooDefault bar = bar "$ @"; This version does not bother with a major dash, so you will run it: 

  myscript foo = newvalue $ {arg}  

He said, some words of caution because we are using eval, the caller can insert any code / data into your script. If you need your code to be secure, you must validate $ {arg} before eval .

In addition, it is not very elegant if you need free space in your data, such as you repeat them (once for the command line and internal eval for second time):

  myscript with foo = 'sapces' ''  

Comments