[bash] Bash script to cd to directory with spaces in pathname

I'm using Bash on macOS X and I'd like to create a simple executable script file that would change to another directory when it's run. However, the path to that directory has spaces in it. How the heck do you do this? This is what I have...

Name of file: cdcode

File contents:

cd ~/My Code

Now granted, this isn't a long pathname, but my actual pathname is five directories deep and four of those directories have spaces in the path.

BTW, I've tried cd "~/My Code" and cd "~/My\ Code" and neither of these worked.

This question is related to bash escaping

The answer is


After struggling with the same problem, I tried two different solutions that works:

1. Use double quotes ("") with your variables.

Easiest way just double quotes your variables as pointed in previous answer:

cd "$yourPathWithBlankSpace"

2. Make use of eval.

According to this answer Unix command to escape spaces you can strip blank space then make use of eval, like this:

yourPathEscaped=$(printf %q "$yourPathWithBlankSpace")
eval cd $yourPathEscaped

cd ~/My\ Code

seems to work for me... If dropping the quotes but keeping the slash doesn't work, can you post some sample code?


When working under Linux the syntax below is right:

cd ~/My\ Code

However when you're executing your file, use the syntax below:

$ . cdcode

(just '.' and not './')


Avoid ~ in scripts; use $HOME instead.


A single backslash works for me:

ry4an@ry4an-mini:~$ mkdir "My Code"

ry4an@ry4an-mini:~$ vi todir.sh

ry4an@ry4an-mini:~$ . todir.sh 

ry4an@ry4an-mini:My Code$ cat ../todir.sh 
#!/bin/sh
cd ~/My\ Code

Are you sure the problem isn't that your shell script is changing directory in its subshell, but then you're back in the main shell (and original dir) when done? I avoided that by using . to run the script in the current shell, though most folks would just use an alias for this. The spaces could be a red herring.


You can use any of:

cd ~/"My Code"
cd ~/M"y Code"
cd ~/My" Code"

You cannot use:

cd ~"/My Code"

The first works because the shell expands ~/ into $HOME/, and then tacks on My Code without the double quotes. The second fails because there isn't a user called '"' (double quote) for ~" to map to.


Use single quotes, like:

myPath=~/'my dir'

cd $myPath

use double quotes

go () 
{ 
    cd "$*"
}

I found the solution below on this page:

x="test\ me"  
eval cd $x

A combination of \ in a double-quoted text constant and an eval before cd makes it work like a charm!


I had a similar problem now were I was using a bash script to dump some data. I ended up creating a symbolic link in the script folder with out any spaces in it. I then pointed my script to the symbolic link and that works fine.

To create your link. ln -s [TARGET DIRECTORY OR FILE] ./[SHORTCUT]

Mau or may not be of use.


When you double-quote a path, you're stopping the tilde expansion. So there are a few ways to do this:

cd ~/"My Code"
cd ~/'My Code'

The tilde is not quoted here, so tilde expansion will still be run.

cd "$HOME/My Code"

You can expand environment variables inside double-quoted strings; this is basically what the tilde expansion is doing

cd ~/My\ Code

You can also escape special characters (such as space) with a backslash.


The very simple way of doing this is-

 $ cd My\ Folder

In bash, run DIR command and in the results you would see that the folder or path names having space between them has been written in the results like this -

$dir
My\ Folder
New\ Folder

This will do it:

cd ~/My\ Code

I've had to use that to work with files stored in the iCloud Drive. You won't want to use double quotes (") as then it must be an absolute path. In other words, you can't combine double quotes with tilde (~).

By way of example I had to use this for a recent project:

cd ~/Library/Mobile\ Documents/com~apple~CloudDocs/Documents/Documents\ -\ My\ iMac/Project

I hope that helps.