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

2020-01-25 00:36发布

I'm attempting 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)

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

The same happens for $HOME:

$ echo $HOME
/home/my dir

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

标签: bash cygwin cd
13条回答
▲ chillily
2楼-- · 2020-01-25 00:49
$ 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:

$ 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) or you 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.)

$ cd $DOCS

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

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

To set $DOCS to the name of your Documents directory, do any one of these:

$ 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 Documents directory, enclose the variable reference in double quotes (that's a good idea for any variable reference in bash, unless you're sure the value doesn't have any funny characters):

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

查看更多
祖国的老花朵
3楼-- · 2020-01-25 00:49

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

查看更多
祖国的老花朵
4楼-- · 2020-01-25 00:50

METHOD1: With Quotes

cd "C:/Prgram Files (x86)"

cd 'C:/Program Files (x86)'

Generalised

cd 'Folder Path'

Method2: Without using Quotes

cd Program\ Files \(x86\)

Generalised Whenever we want to skip next character we use blackslash \.

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

查看更多
SAY GOODBYE
5楼-- · 2020-01-25 00:50

Instead of:

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

Try:

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

This should work on any POSIX system.

查看更多
干净又极端
6楼-- · 2020-01-25 00:53
$ 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.

查看更多
Lonely孤独者°
7楼-- · 2020-01-25 00:54

try

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

cd "$DOCS"
查看更多
登录 后发表回答