在我开始之前,请不要咬我的脑袋我知道这个问题已经在这里和其他论坛上被问过很多次了,但是我无法让这个简单的 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
我尝试了很多组合,例如单引号或双引号或使用反斜杠转义或双引号并使用反斜杠转义
如果我在我的变量周围加上双引号,输出会给我一些数字,就像您在我的示例中看到的那样。
有人知道转义引号的正确用法和组合是什么吗?
希望有人能帮忙
问候,
拉尔夫
最佳答案
只是做:
DEST="/Volumes/Ext 1TB/Google Drive/Backup Ralph/mac"
cd "$DEST"
您需要 cd
行中的引号,因为 $DEST
的值只是插入到命令行中,没有它们会为 cd 生成 4 个参数
:
cd $DEST
=>
cd /Volumes/Ext 1TB/Google Drive/Backup Ralph/mac
HTH
附言我假设您问题中的弯引号“”只是一个自动更正的拼写错误,您知道使用直引号“”
关于bash - 带有空格的 Mac OS shell 脚本 cd 文件夹不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41988083/