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

前端 未结 4 1787
一整个雨季
一整个雨季 2020-12-01 21:08

I\'m trying to substitute a string variable, containing multiple quoted words, as a parameter to a command.

Thus, given the following example script (Note the -x in

4条回答
  •  萌比男神i
    2020-12-01 21:55

    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).

提交回复
热议问题