82

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
15
  • 4
    Don't escape the space, but do add the quote marks : DOCS="/cygdrive/c/Users/my dir/Documents"
    – Floris
    Commented Aug 19, 2013 at 21:35
  • No difference. I edited above so there is no escape in the space. Commented Aug 19, 2013 at 21:36
  • No realy difference if I escape the space using a backslash or quotes, but I did it again anyway. See revision. Commented Aug 19, 2013 at 21:38
  • 1
    When you want to cd, use cd "$DOCS"
    – Floris
    Commented Aug 19, 2013 at 21:38
  • 1
    Well if you are that lazy then you are out of luck.
    – Floris
    Commented Aug 19, 2013 at 21:40

19 Answers 19

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

11
  • I don't want to use quotes every time. That is why I originally inserted the backslash. Commented Aug 19, 2013 at 21:39
  • 1
    @user2316667 Sorry. You have to. Commented Aug 19, 2013 at 21:40
  • Well... how does $HOME do it? Commented Aug 19, 2013 at 21:40
  • -_-. I can safely do cd $HOME but not cd $DOCS...? Commented Aug 19, 2013 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. Commented Aug 19, 2013 at 21:49
63

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.

0
7

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

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

4

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

3

If you want to CD into a path where the directory with space comes in between. You can wrapped the entire path into quotes.

cd "C:\Users\JOE\Local Sites\skeleton"
2

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?

2

The answer is too simple

cd 'my app'

Or you can double-quote

cd "my app"

Both commands should work in most Unix-like terminals, including Linux and macOS.

1

try

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

cd "$DOCS"
1
  • Did you try it? That puts an actual backslash character into "$DOCS". Commented Aug 19, 2013 at 23:19
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...

5
  • I think they said that about war... I'll try it. Gotta install alias though (cygwin). Commented Aug 19, 2013 at 21:58
  • Ugh, needed new setup.exe. 2 more seconds. Commented Aug 19, 2013 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. Commented Aug 19, 2013 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
    Commented Aug 19, 2013 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". Commented Aug 19, 2013 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/

1

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

C:\> cd my folder

will be

 C:\> cd my\ folder 
1

Just use quotes around the file that has a space in it

cd "file with space"
1
  • This answer has already been given by others, like Mindsect Team.
    – ahuemmer
    Commented Jul 30, 2022 at 15:41
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 :)

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

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
0

The approaches discussed here don't work for aliases in a consistent basis - adding/not adding quotes etc.

E.g. if I have a directory variable like dir_with_spaces='path of the directory with spaces', it is not possible to directly use alias new_dir='cd $dir_with_spaces/subdirectory'.

As an alternative, it would be useful to create a custom function that adds quotes around the cd, and then use this custom function instead.

cdwq() # Custom cd to support directories with spaces in the name by adding quotes around it
{
 local folder_path=$1
 cd "${folder_path}"
}

Now, it is possible to use this custom function with any directory variable.

dir_with_spaces='path of the directory with spaces'
dir_without_spaces='path_of_the_directory_without_spaces'

alias new_dir1='cdwq $dir_with_spaces/subdirectory1'
alias new_dir2='cdwq $dir_with_spaces/subdirectory2'
alias new_dir3='cdwq $dir_without_spaces/subdirectory3'
alias new_dir4='cdwq $dir_without_spaces/subdirectory4'
alias new_dir5='cdwq $dir_with_spaces/subdirectory5'
0

If there is space inside dir name, you can use wild card character (? or *) in place of space & wrap the directory name in double-quotes.

Example - in the below path, "Program Files" has space inside -

/mnt/c/Program Files$ JAVA_HOME=/mnt/c/"Program?Files"/Java/jdk-11.0.12

/mnt/c/Program Files$ cd $JAVA_HOME

/mnt/c/Program Files/Java/jdk-11.0.12$
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented May 28, 2022 at 12:28
-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.

4
  • I should throw in, I'm using Cygwin. Shouldn't make a difference but I tried that and didn't work. Commented Aug 19, 2013 at 21:38
  • What does the ls /cygdrive/c/Users show? Whatever Cygwin sees the directory as should be used as the BASH variable. <br> 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: mingw.org/wiki/msys Commented Aug 19, 2013 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. Commented Aug 19, 2013 at 22:23
  • This can also be set at runtime via editing startcyg.bat and adding set DOCS="/cygdrive/c/Users/my\ dir/Documents" Commented Aug 19, 2013 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.