Bash, a name which is basically an acronym for Bourne-Again Shell, is a pretty popular shell that came after the Bourne Shell and normally is available with any Linux and GNU distribution. It adds several advanced features in a way that it remained backward compatible with the previous.
One of the important environment variables in Bash is the PATH. This lists the directories in which the system should look for an executable file when you give a command through the terminal.
How to Set the PATH Variable
If you need to include another directory in the PATH variable, you can select adding either in the beginning or at the end of this list.
The order of directories in the $PATH variable matters. The system will search from left to right, stopping as soon as it finds a matching file. If you append a directory to the end of the PATH, its executables may be overlooked in favor of an executable by the same name in a directory further up the list.
Adding Directories to the PATH
- Adding a directory after existing executables:
Use this method if you want the new directory to be searched only after all the other directories already in the PATH.export PATH=${PATH}:/opt/maven/bin
- Adding a directory prior to existing executables:
Use this approach if you want the system to prioritize executables in the new directory over those in the existing directories.
export PATH=/opt/maven/bin:${PATH}
By setting up the PATH variable carefully, you will be able to ensure that the correct version of an executable is used when you run a command.
Where to Set the PATH Variable?
There are multiple scripts available in the Linux system where you can set the PATH environment variable. These scripts are executed in predefined conditions. Like, some scripts are executed during system startup, and some of them are executed at user login or logout.
Below is the list of scripts that are executed under different conditions. You can choose one of them to set the PATH variable.
- System-wide Configuration:
- /etc/bashrc: This script is invoked at system startup. Setting the PATH environment to this file will be available for all users.
- /etc/bash.bashrc: This script is invoked for the interactive and login shells.
- /etc/profile: This script is invoked with login shells only.
- /etc/profile.d/*.sh: All the script with “.sh” extension are invoked by /etc/profile script.
- /etc/environment: This is the first file that operating system uses at login. This is specifically used for setting up environment variables. This file doesn’t require to use “
export
” keyword.
- User level configuration:
- $HOME/.bashrc: This file is invoked for non-login shell.
- $HOME/.profile: This file is invoked for the login shell. Also, this file invokes the ~/.bashrc script.
Conclusion
The PATH environment variable is a necessary part of the Linux and other GNU-based operating systems. You must be careful before setting up the PATH variable. The order is very essential to ensure your system runs smoothly and locates executables in the right order. By understanding the order in which directories are searched, you can control which versions of commands and tools are prioritized. This tutorial helped you to easily configure the PATH variable to suit your needs and improve your command-line experience.