Brannon Brannon - 2 years ago 120
Bash Question

how to keep nested BASH parameter untokenized

Consider this trite BASH script:

g1="f 2.txt"
g2="f1.txt $g1"
cp $g2

It fails because I passed three parameters to the
command. How do I escape the
call to make it pass just two on the final line (er, make it work without passing two variables on line three)? I tried putting quotes around it with no success; it then proceeds to pass the quotes as part of the parameter, which is doubly weird.

In my real scenario I have some optional parameters that themselves take parameters. I had wanted to parse them all out at the top of the script and leave their final parsed values blank if they weren't passed in.

Answer Source

You can do it using shell arrays:

g1="f 2.txt"
g2=("f1.txt" "$g1")

cp "${g2[@]}"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download