|
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
|
Re: [PLUG] Variable interpolation in bash
|
> This has been bugging me for a while now. When I am writting a bash
> script, I often want to do something of the form:
>
> COMMAND_OPTIONS="-foo -bar --long-opt-whos-arg 'has spaces'"
> command $COMMAND_OPTIONS baz
>
> What I want to happen is for the shell to eventually execute:
>
> command -foo -bar --long-opt-whos-arg 'has spaces' baz
> Running the script with bash -x shows me this:
>
> + COMMAND_OPTIONS=-foo -bar --long-opt-whos-arg 'has spaces'
> + command -foo -bar --long-opt-whos-arg ''\''has' 'spaces'\''' baz
What you're really asking bash to do is expand the variable
$COMMAND_OPTIONS twice -- once to find it's contents, and once
more to perform quote expansion on those contents. You could
try something along the lines of:
eval command $COMMAND_OPTIONS baz
Example:
[mct@quint ~]$ COMMAND_OPTIONS="-foo -bar --long-opt-whos-arg 'has spaces'"
[mct@quint ~]$ (set -x; echo $COMMAND_OPTIONS baz )>/dev/null
+ echo -foo -bar --long-opt-whos-arg ''\''has' 'spaces'\''' baz
[mct@quint ~]$ (set -x; eval echo $COMMAND_OPTIONS baz )>/dev/null
+ eval echo -foo -bar --long-opt-whos-arg ''\''has' 'spaces'\''' baz
++ echo -foo -bar --long-opt-whos-arg 'has spaces' baz
HTH,
-mct
|
|