如何在 bash 脚本中使用包含带空格的文件名的变量?
我有一个在 OS X 上运行的简单 bash 脚本,它删除特定的文件和目录并复制新的文件和目录。一个 Mac .app 目录包含一个空格,当我运行脚本时出现“没有这样的文件或目录”错误。
这是相关代码。大量内容已被删除,因此简单地对这些进行不同的硬编码是不可能的(变量必须按原样分解):
CP="cp -Rf"
INSTALL_DIR="/Applications/"
APP="The App.app"
NEWAPP="$HOME/Downloads/The App.app"
$CP "$NEWAPP " "$INSTALL_DIR$NEWAPP"
我尝试转义 The\ App.app
但没有成功,以及尝试单引号、双引号和双转义,但没有任何效果。我想有一种简单的方法可以做到这一点。
感谢您的帮助。
I have a simple bash script running on OS X that removes specific files and directories and copies new ones in their place. One Mac .app directory contains a space, and when I run the script there is a "No such file or directory" error.
Here is the relevant code. A good amount has been cut out, so simply hard coding these differently isn't possible (the variables must be broken up as they are):
CP="cp -Rf"
INSTALL_DIR="/Applications/"
APP="The App.app"
NEWAPP="$HOME/Downloads/The App.app"
$CP "$NEWAPP " "$INSTALL_DIR$NEWAPP"
I've tried escaping The\ App.app
with no luck, as well as trying single quoting, double quoting, and double escaping, but nothing has worked. I imagine there is a simple way to do this.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你那里有一个额外的空间,它只是
“$NEWAPP”
You have an extra space there, it's simply
"$NEWAPP"
将整个最后一行用引号引起来:
没有必要在此处继续打开和关闭引号,并且 $CP 必须包含在引号中,因为 CP 中有一个空格。
enclose the whole final line in quotes:
it's unnecessary to keep opening and closing the quotes here, and the $CP must be contained in quotes as CP has a space in it.