There is a shell option cdable_vars
:
cdable_vars
If this is set, an argument to the cd
builtin
command that is not a directory is assumed to be the name of a
variable whose value is the directory to change to.
You could add this to your .bashrc
:
shopt -s cdable_vars
export myFold=$HOME/Files/Scripts/Main
Notice that I've replaced the tilde with $HOME
; quotes prevent tilde expansion and Bash would complain that there is no directory ~/Files/Scripts/Main
.
Now you can use this as follows:
cd myFold
No $
required. That's the whole point, actually – as shown in other answers, cd "$myFold"
works without the shell option. cd myFold
also works if the path in myFold
contains spaces, no quoting required.
This usually even works with tab autocompletion as the _cd
function in bash_completion
checks if cdable_vars
is set – but not every implementation does it in the same manner, so you might have to source bash_completion
again in your .bashrc
(or edit /etc/profile
to set the shell option).
Other shells have similar options, for example Zsh (cdablevars
).