Bash parameter expansion from a config file

Hi -

I am trying to do a simple config file with known variable names in it, e.g.:

contents of config file a.conf: -a
-b $work
-c $host
simplified contents of bash script file: work='trunk'
host='alaska'
opts=$(tr '\n' ' ' < a.conf)
opts="$opts $*"
mycommand $opts arg1 arg2
The goal is to get the script to execute: mycommand -a -b trunk -c alaska arg1 arg2
But no matter what I try, it always comes out: mycommand -a -b $work -c $host arg1 arg2
I have tried various quoting methods and evals, but no go.

Can anyone suggest the proper syntax for this?

Note: this is a highly simplified example so I would rather not have to do some kind of sed/awk substitution on the config file contents.

This doesn't work because bash doesn't substitute twice. If you have a string with "$varname" in it, it won't go back and start over and realize you also wanted $varname to be evaluated, too!

To do that kind of doublethink, you have to stuff it into an eval.

ARGS="$(eval $stringwithvarinit)"

Beware that you can do anything in an eval. Someone could stuff an `rm -rf ~/` in your config file and it could run it.

1 Like

Thanks Corona688, I had already tried that and found that it would not work because eval attempts to execute the string, but the string is not a command, just a bunch of dash options, e.g.:script: line 4: eval: -a: invalid option
eval: usage: eval [arg ...]

However you did give me an idea which does work (and I think does not have security issues)...
opts="$(eval echo $opts) $*"
So thanks again!

It still has security issues. Anything in $(brackets) or `backticks`, eval will try to execute.