How do I use a variable containing a filename with

2019-02-26 04:59发布

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.

2条回答
smile是对你的礼貌
2楼-- · 2019-02-26 05:37

You have an extra space there, it's simply

"$NEWAPP"

查看更多
对你真心纯属浪费
3楼-- · 2019-02-26 05:50

enclose the whole final line in quotes:

"$CP $NEWAPP $INSTALL_DIR$NEWAPP"

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.

查看更多
登录 后发表回答