How to substitute quoted, multi-word strings as arguments?

Don’t use quotes, use an array (see BashFAQ #050):

$ myArgs=("hello" "world" "multiword arg with * ?")
+ myArgs=("hello" "world" "multiword arg with * ?")
$ echo "${myArgs[@]}"
+ echo hello world 'multiword arg with * ?'
hello world multiword arg with * ?

If it really needs to be in the form of quoted strings within a string, you’re either going to have to use something like eval "echo $myArg" (which can cause some really nasty bugs, if you aren’t careful) or parse it yourself (which is going to be difficult).

Leave a Comment