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/Main
works.
🎉 Path aliasing or aliases are preconfigured names used to replace long paths in files and resolve to certain directories on a codebase.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With