Bash script for cd to a directory with spaces in pathname

I use Bash on macOS X and would like to create a simple executable script file that will switch to a different directory upon startup. However, the path to this directory contains spaces. How the hell are you doing this? This is what I have ...

File name: cdcode

File content:

 cd ~/My Code 

Now provided, this is not a long way, but my actual path has five directories and four of them have spaces.

By the way, I tried cd "~/My Code" and cd "~/My\ Code" and none of them worked.

+79
bash escaping
Feb 26 '09 at 4:50
source share
12 answers

When you point the path twice, you stop the tilde expansion. So, there are several ways to do this:

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

Tilde is not quoted here, so tilde expansion will still be triggered.

 cd "$HOME/My Code" 

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

 cd ~/My\ Code 

You can also avoid special characters (e.g. spaces) with backslash.

+74
Feb 26 '09 at 5:23
source share

I found a solution below on this page :

 x="test\ me" eval cd $x 

The combination \ in the text constant in double quotes and in eval before cd makes it work like a charm!

+33
Jun 13 '10 at 15:52
source share
 cd ~/My\ Code 

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

+8
Feb 26 '09 at 4:55
source share

You can use any of:

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

You cannot use:

 cd ~"/My Code" 

The first one works because the shell extends ~ / to $ HOME / and then overlays My Code without double quotes. The second fails, because for ~" user 't22>' (double quote) is not specified for the user.

+8
Feb 26 '09 at 5:22
source share

One 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 that the problem is not that your shell script changes the directory in its subshell, but then you returned to the main shell (and the original directory) when it was done? I avoided this using. to run the script in the current shell, although most people would just use an alias for this. Spaces can be red herring.

+4
Feb 26 '09 at 4:57
source share

Avoid ~ in scripts; use $ HOME instead.

+3
Jun 10 '15 at 6:06
source share

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

1. Use double quotation marks ( "" ) with your variables.

The easiest way is to simply quote your variables as indicated in the previous answer:

 cd "$yourPathWithBlankSpace" 

2. Use eval .

According to this Unix response command, to escape spaces you can remove the space and then use eval , for example:

 yourPathEscaped=$(printf %q "$yourPathWithBlankSpace") eval cd $yourPathEscaped 
+3
Dec 03 '17 at 10:43 on
source share

When working under Linux, the syntax below is right:

cd ~/My\ Code

However, when you execute your file, use the syntax below:

$ . cdcode

(just '.', not './')

+2
Feb 26 '09 at 5:02
source share

use double quotes

 go () { cd "$*" } 
+2
May 15 '14 at 15:31
source share

The easiest way to do this is

  $ cd My\ Folder 

In bash, run the DIR command, and in the results you will see that the names of folders or paths that have a space between them were recorded in results like this -

 $dir My\ Folder New\ Folder 
+2
Apr 28 '17 at 10:04 on
source share

I had a similar problem when I used a bash script to dump some data. I ended up creating a symbolic link in the script folder without any spaces in it. Then I pointed out a script to a symbolic link and it works great.

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

Mau or may not be useful.

+1
May 13 '10 at 9:20 a.m.
source share

Use single quotes, for example:

 myPath=~/'my dir' cd $myPath 
0
May 24 '18 at 1:37
source share



All Articles