In the first part of this series, we discussed the**cd -**command in detail, and the in the second part, we took an in-depth look into the**pushd**and**popd**commands as well as the scenarios where-in they come in handy.
Continuing with our discussion on the command line navigation aspects, in this tutorial, we'll discussthe**CDPATH**environment variable through easy to understand examples. We'll also discuss some advance details related to this variable.
_But before we proceed, it's worth mentioning that all the examples in this tutorial have been tested on Ubuntu 14.04 with Bash version4.3.11(1)._
### The CDPATH environment variable
Even ifyour command line work involves performing all operations under a particular directory - say your home directory- then also you have to provide absolute paths while switching directories. For example, consider a situation where-in I am in_/home/himanshu/Downloads_directory:
$ pwd
/home/himanshu/Downloads
And the requirement is to switch to the_/home/himanshu/Desktop_directory. To do this, usually, I'll have to either run:
cd /home/himanshu/Desktop/
or
cd ~/Desktop/
or
cd ../Desktop/
Wouldn't it be easy if I could just run the following command:
cd Desktop
Yes, that's possible. And this is where the CDPATH environment variable comes in.You can use this variable to define the base directory for the**cd**command.
If you try printing its value, you'll see that this env variable is empty by default:
$ echo $CDPATH
$
Now, considering the case we've been discussing so far, let's use this environment variable to define_/home/himanshu_as the base directory for the cd command.
The easiest way to do this is:
export CDPATH=/home/himanshu
And now, I can do what I wasn't able to do earlier - from within the_/home/himanshu/Downloads_directory, run the_cd Desktop_command successfully.
$ pwd
/home/himanshu/Downloads
$**cd Desktop/**
**/home/himanshu/Desktop**
$
This means that I can now do a cd to any directory under_/home/himanshu_without explicitly specifying_/home/himanshu_or_~_or_../_(or multiple_../_)in the cd command.
### Points to keep in mind
So you now know how we used the CDPATH environment variable to easily switch to/from_/home/himanshu/Downloads_from/to_/home/himanshu/Desktop_. Now, consider a situation where-in the_/home/himanshu/Desktop_directory contains a subdirectory named_Downloads_, and it's the latter where you intend to switch.
But suddenly you realize that doing a_cd Desktop_will take you to_/home/himanshu/Desktop_. So, to make sure that doesn't happen, you do:
cd ./Downloads
While there's no problem in the aforementioned commandper se, that's an extra effort on your part (howsoever little it may be), especially considering that you'll have to do this each time such a situation arises. A more elegant solution to this problem can be to originally set the CDPATH variable in the following way:
export CDPATH=".:/home/himanshu"
This means, you're telling the cd command to first look for the directory in the current working directory, and then try searching the_/home/himanshu_directory. Of course, whether or not you want the cd command to behave this way dependsentirely on your preference or requirement - my idea behind discussing this point was to let you know that this kind of situation may arise.
As you would have understood by now, once the CDPATH env variable is set, it's value - or the set of paths it contains - are the only places on the system where the cd command searches for directories (except of course the scenarios where-in you use absolute paths). So, it's entirely up to you to make sure that the behavior of the command remains consistent.
Moving on, if there's a bash script that uses the cd command with relativepaths, then it's better to clear or unset the CDPATH environment variable first, unless you are ok with getting trapped into unforeseen problems. Alternatively, rather than using the_export_command on the terminal to set CDPATH, you can set the environment variable in your`.bashrc`file after testing for interactive/non-interactive shells to make sure that the change you're trying to make is only reflected in interactive shells.
The order in which paths appear in the environment variable's value is also important. For example, if current directory is listed before_/home/himanshu_, then the cd command will first search for a directory in the present working directory and then move on to_/home/himanshu_. However, if the value is_"/home/himanshu:."_then the first search will be made in_/home/himanshu_and after thatthe current directory. Needless to say, this will effect what the cd command does, and may cause problems if you aren't aware of the order of paths.
Always keep in mind that the CDPATH environment variable, as the name suggests, works only for the cd command. This means that while inside the_/home/himanshu/Downloads_directory, you can run the_cd Desktop_command to switch to_/home/himanshu/Desktop_directory, but you can't do an_ls_. Here's an example:
$ pwd
/home/himanshu/Downloads
**$ ls Desktop**
**ls: cannot access Desktop: No such file or directory**
$
However, there could be some simple workarounds. For example, wecan achieve what wewant with minimal effort in the following way:
But yeah, there might not be a workaround for every situation.
Another important point:as you might have observed, whenever you use the cd command with the CDPATH environment variable set, the command producesthe full path of directory you are switching toin the output. Needless to say, not everybody would want to have this information each time they run the cd command on their machine.
To make sure this output gets suppressed, you can use the following command:
alias cd='>/dev/null cd'
The aforementioned command will mute the output whenever the cd command is successful, but will allow the error messages to be produced whenever the command fails.
Lastly, in case you face a problem where-in after setting the CDPATH environment variable, you can't use the shell's tab completion feature, then you can try installing and enablingbash-completion - more on it[here][4].
### Conclusion
The CDPATH environment variable isa double edged sword - if not used with caution and complete knowledge, it may land you in some complex traps that may require a lot of your time precious time to get resolved. Of course, that doesn't mean you should never give it a try; just evaluate all the available options and if you conclude that using CDPATH would be of great help, then do go ahead and useit.
Have you been using CDPATH like a pro? Do you have some more tips to share? Please share your thoughts in comments below.