Bash tokenizing quoted string with spaces as individual words

后端 未结 3 1283
没有蜡笔的小新
没有蜡笔的小新 2021-01-23 05:59

I\'m trying to execute the following commands:

mkdir \'my dir\'
CMD=\"ls \'my dir\'\"
RESULT=$($CMD)

This results in:

ls: \'my:         


        
3条回答
  •  离开以前
    2021-01-23 06:07

    (Almost) all languages differentiate between code and data:

    args="1, 2"
    myfunc(args) != myfunc(1, 2)
    

    The same is true in bash. Putting single quotes in a literal string will not make bash interpret them.

    The correct way of storing a program name and arguments (aka a simple command) is using an array:

    cmd=(ls 'my dir')
    result=$("${cmd[@]}")
    

    Bash will automatically split words on spaces unless you double quote, which is why your example sometimes appears to work. This is surprising and error prone, and the reason why you should always double quote your variables unless you have a good reason not to.

    It's also possible to get bash to interpret a string as if it was bash code entirely, using eval. This is frequently recommended, but almost always wrong.

提交回复
热议问题