如何CD与名称空间中的目录? [英] How to cd into a directory with space in the name?

查看:163
本文介绍了如何CD与名称空间中的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多东西。见下图:结果

尝试进入该目录:
/ cygdrive / C /用户/我的目录/文件

  $ DOCS =/ cygdrive / C /用户/我的\\目录/文件$回声$ DOCS
/ cygdrive / C /用户/我的\\目录/文件$ $ CD DOCS
-bash:CD:/ cygdrive / C /用户/我的\\:没有这样的文件或目录$ CD / cygdrive / C /用户/我的\\目录/文件
(成功)

非常怪异的行为。当我手动输入的,退格做它的转义字符的事情。但是当我使用没有参数扩展与变量DOCS。结果
我试过其他变化,如没有反斜杠。

  $ DOCS = / cygdrive / C /用户/拉赫曼\\目录/文件$回声$ DOCS
/ cygdrive / C /用户/我的目录/文件$ $ CD DOCS
-bash:CD:/ cygdrive / C /用户/我:没有这样的文件或目录

  $ DOCS =/ cygdrive / C /用户/我的目录/文件$回声$ DOCS
/ cygdrive / C /用户/我的目录/文件$ $ CD DOCS
-bash:CD:/ cygdrive / C /用户/我:没有这样的文件或目录

我知道这是可能的。在这里看到:

  $回声$ HOME
/家/我的目录

正如指出的那样,CD $ HOME也不起作用。报价必须围绕它付诸表决。

到底什么:

  $ DOCS =\\/ cygdrive / C /用户/我的目录/文件\\$回声$ DOCS
/ cygdrive / C /用户/我的目录/文件$ $ CD DOCS
-bash:CD:/ cygdrive / C /用户/我:没有这样的文件或目录


解决方案

  $ CD$ DOCS

您需要从被解析为字分隔报价$ DOCS来prevent空间。通常情况下,变量引用应该被引用。

注意 $ HOME 将有同样的问题。这个问题是从壳时计算变量引用过来;它无关,与你用什么变量或你如何分配给他们。这是一个需要加引号的扩展。

  $回声$ HOME
/家/我的目录

这是骗人的。 回声实际上是呼应两个字符串 /家/我的 DIR 。如果你使用 CD LS 你会看到它是如何实际工作。

  $ LS $ HOME
LS:无法访问的/ home /我:没有这样的文件或目录
LS:不能访问DIR:没有这样的文件或目录
$ CD $ HOME
庆典:CD:/家/我:没有这样的文件或目录
$ CD$ HOME
<!成功和GT;



  

我可以问为什么当我手动,但不是在一个变量键入它的工作原理?


大问题!让我们来看看您所键入的命令:

  $ DOCS =\\/ cygdrive / C /用户/我的目录/文件\\
$回声$ DOCS
/ cygdrive / C /用户/我的目录/文件
$ $ CD DOCS
-bash:CD:/ cygdrive / C /用户/我:没有这样的文件或目录

这不工作的原因是因为击不解析可变扩展引号内。它的确实的进行分词,所以在不带引号的变量扩展空格被视为单词分隔符。它的的解析以任何方式报价,这意味着你不能把双引号的变量中重写分词。

  $ CD $ DOCS

由于这个原因, CD 传递两个参数。至于 CD 知道它看起来像你写的:

  $ CD'/ cygdrive / C /用户/我的''目录/文件

两个参数,用双引号完好。

I tried a lot of things. See below:

Attempt to get into the directory: /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)

Very weird behaviour. When I manually type it in, the backspace does its escape character thing. But not when I use parameter expansion with the variable 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

I know it is possible. See here:

$ echo $HOME
/home/my dir

[EDIT] As pointed out, cd $HOME doesn't work either. quotes must be put around it.

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

解决方案

$ 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.

Note that $HOME would 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.

$ echo $HOME
/home/my dir

This is deceptive. echo is actually echoing the two strings /home/my and dir. If you use cd or ls you'll see how it's actually working.

$ 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 does perform word splitting, so whitespace in unquoted variable expansions is taken as word separators. It doesn't parse quotes in any way, meaning you can't put double quotes inside a variable to override word splitting.

$ cd $DOCS

Because of this, cd is passed two parameters. As far as cd knows it looks like you wrote:

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

Two parameters, with double quotes intact.

这篇关于如何CD与名称空间中的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆