How to escape whitespace in a bash alias? How to escape whitespace in a bash alias? bash bash

How to escape whitespace in a bash alias?


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 $umusiccd /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