There is a cdable_vars shell cdable_vars :
cdable_vars
If this is set, the argument to the cd built-in cd which is not a directory, is assumed to be the name of a variable whose value is the directory to be changed.
You can add this to your .bashrc :
shopt -s cdable_vars export myFold=$HOME/Files/Scripts/Main
Note that I replaced the tilde with $HOME ; quotation marks prevent tilde expansion, and Bash will complain about the lack of the ~/Files/Scripts/Main directory.
Now you can use it as follows:
cd myFold
$ Not required. The fact of the matter is that in fact - as shown in other answers, cd "$myFold" works without a shell option. cd myFold also works, if the path in myFold contains spaces, myFold not required.
This usually even works with autocomplete tabs, since the _cd function in bash_completion checks if cdable_vars is cdable_vars - but not every implementation does the same, so you may bash_completion again bash_completion in your .bashrc (or edit /etc/profile to set the option shell).
Other shells have similar parameters, for example Zsh ( cdablevars ).
Benjamin W. Oct. 03 '16 at 19:51 2016-10-03 19:51
source share