Bash tokenizing quoted string with spaces as individual words

后端 未结 3 1398
没有蜡笔的小新
没有蜡笔的小新 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 05:59

    To provide another approach -- one that works on POSIX systems -- xargs can do this parsing for you, as long as you can guarantee that the argument list is short enough that it doesn't get split into multiple separate commands:

    CMD="ls 'my dir'"
    printf '%s\n' "$CMD" | xargs sh -c '"$@"' sh
    

提交回复
热议问题