Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

myFold="~/Files/Scripts/Main" cd myFold  bash: cd: myFold: No such file or directory 

How do I make it work ?
However, cd ~/Files/Scripts/Mainworks.

like image 734
bashboy Avatar asked Jul 30 '13 22:07

bashboy


People also ask

What is a path alias?

🎉 Path aliasing or aliases are preconfigured names used to replace long paths in files and resolve to certain directories on a codebase.


1 Answers

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

cd "${myFold}" 

or:

cp "${myFold}/someFile" /somewhere/else 

But I actually find it easier, if you just want the ease of switching into that directory, 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):

myfold 

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

pax> cd ; pwd ; ls -ald footy /home/pax drwxr-xr-x 2 pax pax 4096 Jul 28 11:00 footy  pax> footydir=/home/pax/footy ; cd "$footydir" ; pwd /home/pax/footy  pax> cd ; pwd /home/pax  pax> alias footy='cd /home/pax/footy' ; footy ; pwd /home/pax/footy  pax> unalias footy ; footy bash: footy: command not found 
like image 176
paxdiablo Avatar answered Sep 24 '22 12:09

paxdiablo