[英]How to use a variable in bash when the folder name contains spaces? [duplicate]
我正在处理我的 dotfiles 脚本,并正在创建一个restore.sh
脚本以在全新安装后复制首选项、文件夹等。
我创建了一个检查以查看是否输入了驱动器名称作为参数,如果不使用默认驱动器。 但是....默认驱动器的文件名中有空格。
所以我创建了一个测试脚本来将文件从/Users/tim/My Folder/file.txt
复制到~/Desktop/file.txt
if [[ $# -eq 0 ]];
then
dir="My\ Folder"
else
dir=$1
fi
options=(-a -v -h -W --progress --compress-level=0)
rsync "${options[@]}" "/Users/tim/$dir/file.txt" "~/Desktop/file.txt"
这不起作用,所以我一直在玩这个
from="/Users/tim/$dir/file.txt"
to="~/Desktop/file.txt"
options=(-a -v -h -W --progress --compress-level=0 $from $to)
rsync "${options[@]}"
但我不断收到以下错误
rsync: link_stat "/Users/tim/My\ Folder/file.txt" failed: No such file or directory (2)
我一直在阅读此内容,并认为 arrays 是在变量中添加空格的方法,但到目前为止我没有尝试过任何工作。
if [[ $# -eq 0 ]];
then
dir="My Folder" # this shouldn't have a backslash, it becomes a part of the filename
else
dir="$1" # this should have quotation marks around it in case of globbing
fi
" ~
"~/Desktop/file.txt"
中的~ 不会展开到主目录。 改用${HOME}
:
rsync "${options[@]}" "/Users/tim/$dir/file.txt" "${HOME}/Desktop/file.txt"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.