views:

48

answers:

1

Tried to set some aliases in my .bashrc file. This one...

export alias umusic="/Volumes/180 gram/Uncompressed/"

...gets the following error...

-bash: cd: /Volumes/180: No such file or directory

...when I try "cd $umusic".

I've tried various methods of escaping that whitespace in the directory name, to no avail. (180\ gram, 180%20gram, single quotes, double quotes, no quotes.) I realize the easiest solution is to rename the directory to "180gram," but I'd like to know how to solve this particular problem.

I'm on a Mac, if that makes any difference.

+2  A: 

Your use of the export command is making umusic an environment variable and not an alias. The export command exports environment variables named on the rest of the command line, optionally with new values. So it's exporting an environment variable named alias (which probably isn't set) and one named umusic.

Given that you're expanding an environment variable, the shell does the following substitution:

cd $umusic
cd /Volumes/180 gram/Uncompressed/

which generates the error you get because the space isn't quoted. If instead you do:

cd "$umusic"

then the expansion is

cd "/Volumes/180 gram/Uncompressed/"

which is what you're expecting.

However, using an environment variable for this might be still a bit too much work, since you have to quote the expansion. Instead, try this alias:

alias umusic="cd '/Volumes/180 gram/Uncompressed'"

which you would run with just

$ umusic
$ pwd
/Volumes/180 gram/Uncompressed
Greg Hewgill
Thank you, and a related question: I thought aliases were supposed to serve as abbreviated file paths. It's OK to include a command in an alias?
parisminton
A shell alias is an abbreviated command; they have no particular connection to file paths (except that they can contain paths, as Greg's suggestion does).
Gordon Davisson