Mac OS shell脚本cd文件夹,空格不起作用

时间:2017-02-01 19:29:30

标签: bash macos shell escaping

在开始之前,请不要咬我的脑袋我知道这个问题在这里和其他论坛已被多次询问,但我无法使用这个简单的shell脚本。

这是我的剧本:

#!/bin/bash

DEST=“/Volumes/Ext\ 1TB/Google\ Drive/Backup\ Ralph/mac”

echo dest is $DEST
cd $DEST

这是我的输出:

bash -x gdrive.sh 
+ DEST='“/Volumes/Ext 1TB/Google Drive/Backup Ralph/mac”'
+ echo dest is $'?\200\234/Volumes/Ext' 1TB/Google Drive/Backup $'Ralph/mac?\200\235'
dest is “/Volumes/Ext 1TB/Google Drive/Backup Ralph/mac”
+ cd $'?\200\234/Volumes/Ext' 1TB/Google Drive/Backup $'Ralph/mac?\200\235'
gdrive.sh: line 6: cd: “/Volumes/Ext: No such file or directory

我尝试了很多组合,比如单引号或双引号,或者用反斜杠或双引号转义并用反斜杠转义

如果我在我的变量周围添加双引号,输出会给我一些数字,如我在示例中所示。

有人知道转义的正确用法和组合是什么?

我希望有人可以提供帮助

此致

拉​​尔夫

1 个答案:

答案 0 :(得分:2)

只是做:

DEST="/Volumes/Ext 1TB/Google Drive/Backup Ralph/mac"

cd "$DEST"

您需要cd行中的引号,因为$DEST的值只是插入到命令行中而没有它们会产生4个cd的参数:

cd $DEST

=>

cd /Volumes/Ext 1TB/Google Drive/Backup Ralph/mac

HTH

P.S。我认为你的问题中的卷曲引号“”只是一个自动纠正的错字,你知道要使用直引号,""

相关问题