tags:

views:

2681

answers:

7

Argggg. I've been struggling with this stupid problem for days and I can't find an answer.

I'm using BASH on Mac OS 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.

If you can help, THANKS! This is driving me crazy!!

+2  A: 
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?

Andrew Flanagan
+2  A: 

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.

Ry4an
Thanks, Ry4an (and the rest of the respondents). It seems that I was executing the script incorrectly. I was just trying to run it like so:% ./cdsomedirI didn't realize I needed to use the "." to run the script as you did:% . cdsomedir.shOnce I did that, it worked. Newbie mistake. Thanks!
A: 

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 './')

Alterlife
Out of wild curiosity, why does that matter? I've only ever seen './' useed.
whaley
./ will spawn a new shell, and then change the directory. So when the script exits, it goes back to the shell which spawned the new shell... you'll be in the directory where you executed the script instead of ~/My Code. '.' on the other hand causes the script to be executed in the current instance of the shell.
Alterlife
A: 

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.

Jonathan Leffler
+4  A: 

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.

derobert
You could even cd ~/My' 'Code, but that would be silly when you can escape it with a backslash instead
Nick Dixon
A: 

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.

Alexis
A: 

Hey! I saw this in http://www.askdavetaylor.com/can_my_path_include_directory_names_with_spaces.html

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!