在 OS X 中以空格作为参数传递路径

Vla*_*mir 0 bash macos

我无法将路径作为内部有空格的参数传递。

keyLocation="/Users/vladimir/Google\ Drive/file.pem"
ssh -i $keyLocation root@192.168.1.1;
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误:

警告:无法访问身份文件 /Users/vladimir/Google:没有此类文件或目录。

基本上在分配 keyLocation 本身变成 "/Users/vladimir/Google Drive/file.pem" (no ) 之后,当它尝试使用 $keyLocation 作为参数时,它会将其作为

ssh -i /Users/vladimir/Google Drive/file.pem root@192.168.1.1
Run Code Online (Sandbox Code Playgroud)

如何让 ssh 命令将其识别为内部有空格的路径?

slh*_*hck 12

你把它弄混了一点。空格不需要在引号内转义。当你扩展一个包含空格的变量时,总是引用它。

keyLocation="/Users/vladimir/Google Drive/file.pem"
ssh -i "$keyLocation" root@192.168.1.1
Run Code Online (Sandbox Code Playgroud)

请参阅:引用和转义 [Bash Hackers Wiki]