将 Bash 脚本 cd 到路径名中有空格的目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/589149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:58:19  来源:igfitidea点击:

Bash script to cd to directory with spaces in pathname

bashescaping

提问by

I'm using Bash on macOS X and I'd like to create a simple executable script file that would change to another directory when it's run. However, the path to that directory has spaces in it. How the heck do you do this? This is what I have...

我在 macOS X 上使用 Bash,我想创建一个简单的可执行脚本文件,该文件在运行时会更改到另一个目录。但是,该目录的路径中有空格。你是怎么做到的?这就是我所拥有的...

Name of file: cdcode

文件名: cdcode

File contents:

文件内容:

cd ~/My Code

Now granted, this isn't a long pathname, but my actual pathname is five directories deep and four of those directories have spaces in the path.

现在当然,这不是一个长路径名,但我的实际路径名有五个目录深,其中四个目录在路径中有空格。

BTW, I've tried cd "~/My Code"and cd "~/My\ Code"and neither of these worked.

顺便说一句,我已经试过cd "~/My Code"cd "~/My\ Code"和这些都不奏效。

回答by derobert

When you double-quote a path, you're stopping the tilde expansion. So there are a few ways to do this:

当您双引号路径时,您正在停止波浪号扩展。所以有几种方法可以做到这一点:

cd ~/"My Code"
cd ~/'My Code'

The tilde is not quoted here, so tilde expansion will still be run.

这里没有引用波浪号,所以波浪号扩展仍然会运行。

cd "$HOME/My Code"

You can expand environment variables inside double-quoted strings; this is basically what the tilde expansion is doing

您可以在双引号字符串中扩展环境变量;这基本上就是波浪号扩展所做的

cd ~/My\ Code

You can also escape special characters (such as space) with a backslash.

您还可以使用反斜杠转义特殊字符(例如空格)。

回答by user365757

I found the solution below on this page:

我在此页面上找到了以下解决方案:

x="test\ me"  
eval cd $x

A combination of \in a double-quoted text constant and an evalbefore cdmakes it work like a charm!

的组合\在一个双引号的文本常量和eval之前cd它像一个魅力的工作使!

回答by Andrew Flanagan

cd ~/My\ Code

seems to work for me... If dropping the quotes but keeping the slash doesn't work, can you post some sample code?

似乎对我有用......如果删除引号但保留斜杠不起作用,你能发布一些示例代码吗?

回答by Jonathan Leffler

You can use any of:

您可以使用以下任何一种:

cd ~/"My Code"
cd ~/M"y Code"
cd ~/My" Code"

You cannot use:

您不能使用:

cd ~"/My Code"

The first works because the shell expands ~/ into $HOME/, and then tacks on My Code without the double quotes. The second fails because there isn't a user called '"' (double quote) for ~"to map to.

第一个有效,因为 shell 将 ~/ 扩展为 $HOME/,然后在没有双引号的情况下添加我的代码。第二个失败,因为没有名为 ' "' (双引号)的~"用户映射到。

回答by HanniBaL90

After struggling with the same problem, I tried two different solutions that works:

在遇到同样的问题后,我尝试了两种不同的解决方案:

1. Use double quotes ("") with your variables.

1.""对变量使用双引号 ( )。

Easiest way just double quotes your variables as pointed in previous answer:

最简单的方法只是双引号你的变量,如前面的答案所指出的:

cd "$yourPathWithBlankSpace"

2. Make use of eval.

2.利用eval

According to this answer Unix command to escape spacesyou can strip blank space then make use of eval, like this:

根据this answer Unix命令转义空格,您可以去除空格然后使用eval,如下所示:

yourPathEscaped=$(printf %q "$yourPathWithBlankSpace")
eval cd $yourPathEscaped

回答by Ry4an Brase

A single backslash works for me:

一个反斜杠对我有用:

ry4an@ry4an-mini:~$ mkdir "My Code"

ry4an@ry4an-mini:~$ vi todir.sh

ry4an@ry4an-mini:~$ . todir.sh 

ry4an@ry4an-mini:My Code$ cat ../todir.sh 
#!/bin/sh
cd ~/My\ Code

Are you sure the problem isn't that your shell script is changing directory in its subshell, but then you're back in the main shell (and original dir) when done? I avoided that by using . to run the script in the current shell, though most folks would just use an alias for this. The spaces could be a red herring.

您确定问题不在于您的 shell 脚本正在更改其子 shell 中的目录,而是在完成后返回主 shell(和原始目录)?我通过使用 . 在当前 shell 中运行脚本,尽管大多数人只会为此使用别名。这些空间可能是一个红鲱鱼。

回答by Chris F.A. Johnson

Avoid ~ in scripts; use $HOME instead.

避免在脚本中使用 ~;改用 $HOME。

回答by Mayukh Datta

The very simple way of doing this is-

这样做的非常简单的方法是——

 $ cd My\ Folder

In bash, run DIR command and in the results you would see that the folder or path names having space between them has been written in the results like this -

在 bash 中,运行 DIR 命令,在结果中您会看到在结果中写入了文件夹或路径名称之间有空格的名称,如下所示 -

$dir
My\ Folder
New\ Folder

回答by Alterlife

When working under Linux the syntax below is right:

在 Linux 下工作时,以下语法是正确的:

cd ~/My\ Code

cd ~/My\ Code

However when you're executing your file, use the syntax below:

但是,当您执行文件时,请使用以下语法:

$ . cdcode

$ . cdcode

(just '.' and not './')

(只是 '.' 而不是 './')

回答by Bobo

use double quotes

使用双引号

go () 
{ 
    cd "$*"
}