66

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\ 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
user2316667
  • 4,826
  • 11
  • 40
  • 68
  • 3
    Don't escape the space, but do add the quote marks : `DOCS="/cygdrive/c/Users/my dir/Documents"` – Floris Aug 19 '13 at 21:35
  • No difference. I edited above so there is no escape in the space. – user2316667 Aug 19 '13 at 21:36
  • No realy difference if I escape the space using a backslash or quotes, but I did it again anyway. See revision. – user2316667 Aug 19 '13 at 21:38
  • 1
    When you want to `cd`, use `cd "$DOCS"` – Floris Aug 19 '13 at 21:38
  • I don't want to use quotes. I'm lazy, heh. Although I'll throw in that it DOES work. – user2316667 Aug 19 '13 at 21:39
  • Well if you are _that_ lazy then you are out of luck. – Floris Aug 19 '13 at 21:40
  • You can solve almost any laziness problem with an alias - but I am assuming that this is happening inside a script, so I don't understand the "use quotes every time" comment (in your response to John Kugelman's answer). – Floris Aug 19 '13 at 21:43
  • Its not a script. I'm setting up an environment variable – user2316667 Aug 19 '13 at 21:44
  • OK - see my answer. An alias (a bit like an environment variable, but more powerful) will give you what you need with the minimum of fuss/typing. Who wants to type `cd $DOC` when they could just type `mydoc`. You could even make it a single character if you really wanted... – Floris Aug 19 '13 at 21:57
  • @Floris: If all you have is an alias that does a `cd` to a specified directory, what if you want to do an `ls -l` on that directory, as in `ls -l "$DOCS"`? – Keith Thompson Aug 19 '13 at 23:21
  • @KeithThompson: `mydoc; ls -l`. If you looks at the earlier comments you will see I had suggested a proper definition and use of `"$DOCS"` but was told "I am too lazy for that" - so I gave an alternative. Trying to work some alternatives is all... – Floris Aug 20 '13 at 02:39
  • It should be enclosed in double quotes because of the [Quote Removal](https://www.gnu.org/software/bash/manual/html_node/Quote-Removal.html#Quote-Removal) @Floris – ohyecloudy Feb 13 '18 at 12:08

14 Answers14

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

John Kugelman
  • 307,513
  • 65
  • 473
  • 519
  • I don't want to use quotes every time. That is why I originally inserted the backslash. – user2316667 Aug 19 '13 at 21:39
  • 1
    @user2316667 Sorry. You have to. – John Kugelman Aug 19 '13 at 21:40
  • Well... how does $HOME do it? – user2316667 Aug 19 '13 at 21:40
  • -_-. I can safely do cd $HOME but not cd $DOCS...? – user2316667 Aug 19 '13 at 21:46
  • Oh.. what the... I could've sworn I could do cd $HOME, but... hm. Okay, is there a way to include " inside the variable? I will try that now. – user2316667 Aug 19 '13 at 21:49
  • No; there isn't a way to include the double quotes inside the variable, short of having to use `eval cd $DOCS` which is worse than `cd "$DOCS"` (by 3 characters). The double quotes are necessary around the variable; use them and get over the overwhelming desire for laziness. Or don't create folders with spaces in the names — that's even easier. – Jonathan Leffler Aug 19 '13 at 21:51
  • Can I ask why it works when I manually type it in but not in a variable? Also view edits in op. I got double quotes inside the variable. Still same problem though. Any plausible explanation? – user2316667 Aug 19 '13 at 21:54
  • When you type it manually with the backslash, the shell reads it all as a word and `cd`'s to the right place, spaces and all. When you save the name with a space in it in a variable, the shell splits `$DOCS` at the space unless you surround it with double quotes. Even if you include one or two backslashes in the variable value, the shell splits the value at the space. You'd have to use `eval cd $DOCS` when `$DOCS` contains a backslash-space to get the `cd` to work OK. In a sense, it is just 'the way it works'. It is the way it 'always has worked', too. – Jonathan Leffler Aug 19 '13 at 22:03
  • $DOCS "splits?" Are you saying $DOCS becomes an array when spaces are included? Nope.. I tested it. $DOCS[0] outputs the entire, proper string. And I even echoed it above. Conclusive evidence.. no? – user2316667 Aug 19 '13 at 22:08
  • Ah, I see, thank you very much. I appreciate your insight beyond the scope of the question! – user2316667 Aug 19 '13 at 22:19
  • Great answer. Thanks for providing my "I learn something new every day" moment. – Floris Aug 19 '13 at 22:26
20

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.

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

Mindsect Team
  • 1,441
  • 3
  • 24
  • 34
5
$ 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.

Keith Thompson
  • 230,326
  • 38
  • 368
  • 578
3

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

cd My\ Documents

Hit enter and you will be good

Ricardo Gonzalez
  • 1,590
  • 1
  • 10
  • 22
Nsaavs
  • 31
  • 1
1

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

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 cd to your directory.

Laziness is the mother of invention...

Floris
  • 43,828
  • 5
  • 63
  • 112
  • I think they said that about war... I'll try it. Gotta install alias though (cygwin). – user2316667 Aug 19 '13 at 21:58
  • Ugh, needed new setup.exe. 2 more seconds. – user2316667 Aug 19 '13 at 22:03
  • Works! But now I'm mad because cd $DOCS should for all purposes have worked... Could you explain why the variable didn't work when manually typing in (what the variable SHOULD Have been expanded to) did? If anything trumps my laziness, it is curiosity. – user2316667 Aug 19 '13 at 22:05
  • I see that @JohnKugelman already answered your other question... "it can't be done"; I think this means that my method is the most promising way to achieve what you actually wanted - to change to this directory with the least amount of typing (and without using quotes). – Floris Aug 19 '13 at 22:24
  • That gives you a way to `cd` into the directory, but not to do anything else with it. If `$DOCS` is properly defined, you can do things like `ls -l "$DOCS"`. – Keith Thompson Aug 19 '13 at 23:18
1

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/

gniourf_gniourf
  • 38,851
  • 8
  • 82
  • 94
antonio
  • 423
  • 5
  • 14
1

Use quotes! cd "Name of Directory"
Or you can go to the file explorer and click "copy path" in the top left corner!

Dharman
  • 21,838
  • 18
  • 57
  • 107
samarmohan
  • 160
  • 2
  • 7
0

try

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

cd "$DOCS"
michael501
  • 1,354
  • 8
  • 17
0

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:

cd desktop

Now if you want to display subfolders:

cd, make 1 spacebar, and press tab 2 times

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

cd file\ name

p.s. notice this space after slash :)

Vasyl Gutnyk
  • 4,153
  • 2
  • 29
  • 35
0

Use the backslash symbol (\) to escape the space:

C:\> cd my folder

will be

 C:\> cd my\ folder 
Huanfa Chen
  • 472
  • 4
  • 13
0

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

Community
  • 1
  • 1
shaurya uppal
  • 2,335
  • 23
  • 28
0

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.

such as ".../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?

0

As an alternative to using quotes, for a directory you want to go to often, you could use the cdable_vars shell option:

shopt -s cdable_vars
docs='/cygdrive/c/Users/my dir/Documents'

Now, to change into that directory from anywhere, you can use

cd docs

and the shell will indicate which directory it changed to:

$ cd docs
/cygdrive/c/Users/my dir/Documents
Benjamin W.
  • 33,075
  • 16
  • 78
  • 86
-2

Instead of:

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

Try:

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

This should work on any POSIX system.

Pang
  • 8,605
  • 144
  • 77
  • 113
  • I should throw in, I'm using Cygwin. Shouldn't make a difference but I tried that and didn't work. – user2316667 Aug 19 '13 at 21:38
  • What does the ls /cygdrive/c/Users show? Whatever Cygwin sees the directory as should be used as the BASH variable.
    Cygwin is often less than perfect about windows-unicey file translation. If you're not porting software, and don't require POSIX compliance, there is MinGW/MSYS which is 10-100% faster than Cygwin, and far less error prone: http://mingw.org/wiki/msys
    – user2697927 Aug 19 '13 at 21:58
  • You could also try setting the DOCS variable in the Windows User or System settings, as they are imported by default. System-> Advanced -> Environment Variables Cygwin is incredibly "quarky" about spaces, case sensitivity and other things, often requiring additional configuration to behave in a Unicey manner. – user2697927 Aug 19 '13 at 22:23
  • This can also be set at runtime via editing startcyg.bat and adding set DOCS="/cygdrive/c/Users/my\ dir/Documents" – user2697927 Aug 19 '13 at 22:26