If you’ve ever used Go I’m pretty sure you have heard of the term GOPATH. It refers to the internal directory structure that the Go toolchain uses to find source code and packages.
If i have a project called weather and i wanted to
cd into it, i would have to do something like this (assuming my $GOPATH is set to $HOME/go).
This works fine but if weather is a directory you visit frequently typing all that becomes boring,super repetitive and also a little bit annoying.
But wouldn’t it be cool if we could just cd weather and we would be able to jump to that directory directly?
It turns out we can actually do that with the help of CDPATH. Learning the bash shell, 3rd eidition defines CDPATH as
The variable whose value, like that of PATH, is a list of directories separated by colons. Its purpose is to augment the functionality of the cd built-in command.
By default CDPATH is not set. To set it up so that we can jump to our Go projects faster we would do something like this
So what does the above command do? Well it assigns a single value to the CDPATH variable which is the location we would like
cd to use while looking for the directory we are interested in. With that exported if we typed cd weather we would automatically go to
How cool right?😃
However please note that
The above would only work if your github username and $USER are the same. If not, all you have to do is replace $USER with your actually github username.
To make it persitent add the export statement to your .bashrc or .zshrc.
Let’s imagine a scenerio where you have multiple projects in your GOPATH which you contribute to and some of them are not under your github username as is usually the case. Using what we have learnt above is it still be possible to jump to those projects quickly without typing the entire path? Yes.
To do that we would pass a second value to CDPATH separated by a colon. For example
With that out of the way we would be able to do something like
And automatically go to
If we had projects from github and gitlab can we still use CDPATH? Again yes. All we have to do is pass a third value to
CDPATH and it would look something like this.
With that i can now do
and automatically go to
If i had a directory $HOME/weather and i typed cd weather while i was at $HOME where would it take me?
./weatherunfourtanely. From softparanoma, If you put a trailing slash “/” on each path in
$CDPATH, you’ll “cd” to the local directory. If you don’t put a trailing slash on each pathname, you’ll “cd” to the first pathname in the
$CDPATHlist that contains a matching subdirectory.
cd into the local directory i would have to do
cd ./weather but from anywhere else i would still be able to do
cd weather and go to
I hope you have learnt something new and are going to use
CDPATH to make your life easier while using the terminal.
Thank you for reading!