bash 如何 cd 进入名称中有空格的目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18323508/
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-10 00:04:39  来源:igfitidea点击:

How to cd into a directory with space in the name?

bashcygwincd

提问by user2316667

I'm attempting to get into the directory /cygdrive/c/Users/my dir/Documents:

我正在尝试进入目录/cygdrive/c/Users/my dir/Documents

$ DOCS="/cygdrive/c/Users/my\ dir/Documents"

$ echo $DOCS
/cygdrive/c/Users/my\ dir/Documents

$ cd $DOCS
-bash: cd: /cygdrive/c/Users/my\: No such file or directory

$ cd /cygdrive/c/Users/my\ dir/Documents
(success)

When I manually type it in, the backspace does its escape character thing, but not when I use parameter expansion with the variable DOCS.

当我手动输入它时,退格符会执行转义字符的操作,但当我将参数扩展与变量DOCS.

I tried other variations such as no backslash.

我尝试了其他变体,例如没有反斜杠。

$ DOCS=/cygdrive/c/Users/Rahman\ dir/Documents

$ echo $DOCS
/cygdrive/c/Users/my dir/Documents

$ cd $DOCS
-bash: cd: /cygdrive/c/Users/my: No such file or directory

or

或者

$ DOCS="/cygdrive/c/Users/my dir/Documents"

$ echo $DOCS
/cygdrive/c/Users/my dir/Documents

$ cd $DOCS
-bash: cd: /cygdrive/c/Users/my: No such file or directory

The same happens for $HOME:

同样的情况发生在$HOME

$ echo $HOME
/home/my dir

cd $HOMEdoesn't work either. Quotes must be put around it.

cd $HOME也不起作用。必须在它周围加上引号。

What the heck:

有没有搞错:

$ DOCS="\"/cygdrive/c/Users/my dir/Documents\""

$ echo $DOCS
"/cygdrive/c/Users/my dir/Documents"

$ cd $DOCS
-bash: cd: "/cygdrive/c/Users/my: No such file or directory

采纳答案by John Kugelman

$ cd "$DOCS"

You need to quote "$DOCS"to prevent spaces from being parsed as word separators. More often than not, variable references should be quoted.

您需要引用"$DOCS"以防止空格被解析为单词分隔符。通常情况下,变量引用应该被引用。

Note that $HOMEwould have the same problem. The issue is coming from when the shell evaluates variable references; it's nothing to do with what variables you use or how you assign to them. It's the expansion that needs to be quoted.

请注意,这$HOME也会有同样的问题。问题来自 shell 评估变量引用时;这与您使用的变量或分配给它们的方式无关。这是需要引用的扩展。

$ echo $HOME
/home/my dir

This is deceptive. echois actually echoing the two strings /home/myand dir. If you use cdor lsyou'll see how it's actually working.

这是骗人的。echo实际上是呼应两个字符串/home/mydir。如果你使用cdorls你会看到它是如何实际工作的。

$ ls $HOME
ls: cannot access /home/my: No such file or directory
ls: cannot access dir: No such file or directory
$ cd $HOME
bash: cd: /home/my: No such file or directory
$ cd "$HOME"
<success!>


Can I ask why it works when I manually type it in but not in a variable?

我可以问为什么当我手动输入它而不是变量时它有效吗?

Great question! Let's examine the commands you typed:

好问题!让我们检查您键入的命令:

$ DOCS="\"/cygdrive/c/Users/my dir/Documents\""
$ echo $DOCS
"/cygdrive/c/Users/my dir/Documents"
$ cd $DOCS
-bash: cd: "/cygdrive/c/Users/my: No such file or directory

The reason this doesn't work is because Bash doesn't parse quotes inside variable expansions. It doesperform word splitting, so whitespace in unquoted variable expansions is taken as word separators. It doesn'tparse quotes in any way, meaning you can't put double quotes inside a variable to override word splitting.

这不起作用的原因是因为 Bash 不解析变量扩展中的引号。它确实执行分词,因此未加引号的变量扩展中的空格被视为单词分隔符。它不会以任何方式解析引号,这意味着您不能在变量中放置双引号来覆盖分词。

$ cd $DOCS

Because of this, cdis passed two parameters. As far as cdknows it looks like you wrote:

因此,cd传递了两个参数。据cd了解它看起来像你写的:

$ cd '"/cygdrive/c/Users/my' 'dir/Documents"'

Two parameters, with double quotes intact.

两个参数,双引号完好无损。

回答by Mindsect Team

SOLUTION:

解决方案:

cd "Documents and Photos"

problem solved.

问题解决了。

The reason I'm submitting this answer is you'll find that StackOverflow is being used by every day users (not just web devs, programmers or power users) and this was the number one result for a simple Windows user question on Google.

我提交这个答案的原因是你会发现 StackOverflow 被日常用户(不仅仅是网络开发人员、程序员或高级用户)使用,这是谷歌上一个简单的 Windows 用户问题的第一结果。

People are becoming more tech-savvy, but aren't necessarily familiar with command line in the cases above.

人们变得越来越精通技术,但在上述情况下不一定熟悉命令行。

回答by Keith Thompson

$ DOCS="/cygdrive/c/Users/my\ dir/Documents"

Here's your first problem. This puts an actual backslash character into $DOCS, as you can see by running this command:

这是你的第一个问题。这会将一个实际的反斜杠字符放入 中$DOCS,您可以通过运行以下命令看到:

$ echo "$DOCS"
/cygdrive/c/Users/my\ `

When defining DOCS, you do need to escape the space character. You can quote the string (using either single or double quotes) oryou can escape just the space character with a backslash. You can't do both. (On most Unix-like systems, you can have a backslash in a file or directory name, though it's not a good idea. On Cygwin or Windows, \is a directory delimiter. But I'm going to assume the actual name of the directory is my dir, not my\ dir.)

定义时DOCS,您确实需要转义空格字符。您可以引用字符串(使用单引号或双引号)也可以仅使用反斜杠转义空格字符。你不能两者都做。(在大多数类 Unix 系统上,您可以在文件名或目录名中使用反斜杠,尽管这不是一个好主意。在 Cygwin 或 Windows 上,它\是一个目录分隔符。但我将假设目录的实际名称是my dir,不是my\ dir。)

$ cd $DOCS

This passes two arguments to cd. The first is cygdrive/c/Users/my\, and the second is dir/Documents. It happens that cdquietly ignores all but its first argument, which explains the error message:

这将两个参数传递给cd. 第一个是cygdrive/c/Users/my\,第二个是dir/Documents。碰巧它cd悄悄地忽略了除第一个参数之外的所有参数,这解释了错误消息:

-bash: cd: /cygdrive/c/Users/my\: No such file or directory

To set $DOCSto the name of your Documentsdirectory, do any one of these:

要设置$DOCS为您的Documents目录名称,请执行以下任一操作:

$ DOCS="/cygdrive/c/Users/my dir/Documents"
$ DOCS='/cygdrive/c/Users/my dir/Documents'
$ DOCS=/cygdrive/c/Users/my\ dir/Documents

Once you've done that, to change to your Documentsdirectory, enclose the variable reference in double quotes (that's a good idea for anyvariable reference in bash, unless you're sure the value doesn't have any funny characters):

完成后,要切换到您的Documents目录,请将变量引用括在双引号中(这对于bash 中的任何变量引用都是一个好主意,除非您确定该值没有任何有趣的字符):

$ cd "$DOCS"

You might also consider giving that directory a name without any spaces in it -- though that can be hard to do in general on Windows.

您还可以考虑为该目录指定一个不带任何空格的名称——尽管这在 Windows 上通常很难做到。

回答by Nsaavs

To change to a directory with spaces on the name you just have to type like this:

要切换到名称上带有空格的目录,您只需键入如下:

cd My\ Documents

cd My\ Documents

Hit enterand you will be good

点击进入,你会很好

回答by Floris

Why not put the following in your .cshrc(or .bashrc, or whatever your default shell is):

为什么不将以下内容放在您的.cshrc(或.bashrc,或任何您的默认 shell 中):

alias mydoc 'cd "/cygdrive/c/Users/my dir/Documents"'

First time you do this, you have to do

第一次这样做,你必须这样做

source .cshrc

to update the shell with this new alias, then you can type

用这个新别名更新外壳,然后你可以输入

mydoc

anytime you want to cdto your directory.

随时cd访问您的目录。

Laziness is the mother of invention...

懒惰是发明之母……

回答by antonio

If you want to move from c:\and you want to go to c:\Documents and settings, write on console: c:\Documents\[space]+taband cygwin will autocomplete it as c:\Documents\ and\ settings/

如果您想从 移动c:\c:\Documents and settings,请在控制台上写:c:\Documents\[space]+tab并且 cygwin 将自动完成它为c:\Documents\ and\ settings/

回答by Vasyl Gutnyk

ok i spent some frustrating time with this problem too. My little guide.

好吧,我也花了一些令人沮丧的时间来解决这个问题。我的小向导。

Open desktop for example. If you didnt switch your disc in cmd, type:

例如打开桌面。如果您没有在 cmd 中切换您的光盘,请键入:

cd desktop

光盘桌面

Now if you want to display subfolders:

现在,如果要显示子文件夹:

cd, make 1 spacebar, and press tab2 times

cd,制作 1 个空格键,然后按Tab2 次

Now if you want to enter directory/file with SPACE IN NAME. Lets open some file namef.g., to open it we need to type:

现在,如果您想输入带有SPACE IN NAME 的目录/文件。让我们打开一些文件名fg,打开它我们需要输入:

cd file\ name

cd 文件\名称

p.s. notice this space after slash :)

ps 注意斜杠后的这个空格:)

回答by Ilemona James Atuluku

Use the backslash symbol to escape the space

使用反斜杠符号来转义空格

C:\> cd my folder

will be

将会

 C:\> cd my\folder 

回答by shaurya uppal

METHOD1: With Quotes

方法一:带引号

cd "C:/Prgram Files (x86)"

cd 'C:/Program Files (x86)'

Generalised

cd 'Folder Path'

cd "C:/Prgram Files (x86)"

cd 'C:/Program Files (x86)'

广义的

cd 'Folder Path'

Method2: Without using Quotes

方法 2:不使用引号

cd Program\ Files \(x86\)

GeneralisedWhenever we want to skip next character we use blackslash \.

cd Program\ Files \(x86\)

广义每当我们想跳过下一个字符时,我们都会使用 blackslash \

For the above question: cd /cygdrive/c/Users/my\ dir/Documents

对于上述问题: cd /cygdrive/c/Users/my\ dir/Documents

回答by Damilare Oyediran

Cygwin has issue recognizing space in between the PC name. So to solve this, you have to use "\" after the first word then include the space, then the last name.

Cygwin 无法识别 PC 名称之间的空格。所以要解决这个问题,你必须在第一个单词之后使用“\”,然后包括空格,然后是姓氏。

such as ".../my\ dir/"

例如“.../my\ dir/”

$ cd /cygdrive/c/Users/my\ dir/Documents

Another interesting and simple way to do it, is to put the directory in quotation marks ("")

另一种有趣且简单的方法是将目录放在引号 ("") 中

e.g run it as follows:

例如运行它如下:

$ cd c:
$ cd Users
$ cd "my dir"
$ cd Documents

Hope it works?

希望它有效吗?