Argg!!! Bash and Spaces in Path

I want to create a simple bash shell script that will take me to a specific directory whose path contains spaces. How do I do that?

cdshared, which is in ~/Documents/bin folder, contains this:

cd "~/Documents/Shared Folders"

Bash doesn't seem to care if I put the path between double quotes as it can't find the path. I also removed the double quotes and escaped the space with a backslash but that didn't work either! What gives?

iMac, Mac OS X (10.5.6)

Posted on Feb 11, 2009 10:09 PM

Reply
5 replies

Feb 11, 2009 11:30 PM in response to RobertF57

If you use the alias command you can set something like the following:

alias mypic='cd ~/documents/My_Pics'

(edit) if you have a space in the filename then - alias mypic='cd ~/documents/My\ Pics'

At the shell all you have to do is type mypic and you will go to that folder no matter which folder you are currently in.

Chris

Message was edited by: 2point5

Feb 12, 2009 12:11 AM in response to RobertF57

OK to save your alias's you can set up a .bash_profile in the home folder which will be read each time you access the shell.

Edit it with vi and add the alias definition. Save it, exit the shell then open a new shell and your alias should be active. Type *alias -p* to check.

Chris

BTW this is the site that refreshed my memory on the .bash_profile
http://www.peachpit.com/articles/article.aspx?p=31442&seqNum=5

Message was edited by: 2point5

Feb 12, 2009 9:12 AM in response to RobertF57

This discussion would be better in the Unix forum, but it's too late now...

The problem is putting the ~ inside the quotes, which causes it to be interpreted literally. There are a number of ways to do this. The best is do not use ~ in a script. It is just a typing shorthand anyway. Use $HOME. Any of the following work for me:

cd ~/Documents/"Shared Folders"
cd ~/Documents/Shared Folders
cd "$HOME/Documents/Shardes Folders"

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Argg!!! Bash and Spaces in Path

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.