使用空格和逗号更正路径的bash语法

Bay*_*rSe 0 bash rsync

我使用rsync备份数据.现在我在备份包含逗号和空格字符的文件夹时遇到了麻烦.我使用一个带有源名称的数组:

dirs=(
    /home/user/Desktop
    ...
    /home/user/foo, bar
)
Run Code Online (Sandbox Code Playgroud)

最后一个条目的正确语法是什么?

Tom*_*ech 5

引用它们:

dirs=(
    '/home/user/Desktop'
    ...
    '/home/user/foo, bar'
)
Run Code Online (Sandbox Code Playgroud)

这可以防止发生单词分裂.由于这些是字符串文字(例如,没有扩展变量),我更喜欢使用单引号.

大概你打算循环遍历这些目录,在这种情况下,你还应该在循环中引用变量:

for dir in "${dirs[@]}"; do
    rsync command using "$dir"
done
Run Code Online (Sandbox Code Playgroud)

这里需要双引号,以便不执行单词拆分,但仍然会扩展变量.