bashboy bashboy - 10 months ago 51
Linux Question

How to make an "alias" for a long path?

I tried to make an "alias" for a path that I use often while shell scripting. I tried something, but it failed:

cd myFold

bash: cd: myFold: No such file or directory

How do I make it work ?

cd ~/Files/Scripts/Main

Answer Source

Since it's an environment variable (alias has a different definition in bash), you need to evaluate it with something like:

cd "$myFold"

But I actually find it easier to create a real alias (in one of the bash startup files like .bashrc), so I can save keystrokes:

alias myfold='cd ~/Files/Scripts/Main'

Then you can just use (without the cd):


To get rid of the definition, you use unalias. The following transcript shows all of these in action:

pax> cd ; pwd ; ls -ald footy
drwxr-xr-x 2 pax pax 4096 Jul 28 11:00 footy

pax> footydir=/home/pax/footy ; cd "$footydir" ; pwd

pax> cd ; pwd

pax> alias footy='cd /home/pax/footy' ; footy ; pwd

pax> unalias footy ; footy
bash: footy: command not found