Bash is an acronym of Bourne-Again Shell, which is the successor of Bourne Shell distributed with most of the Linux and GNU operating systems. It comes with multiple advanced features from the previous version.
The PATH
is an environment variable that stores the directories path containing the executable files.
How to Set PATH Variable?
Whenever you need to add a new executable in the PATH variable, you can either add it to the start of another directory or at the end of other directories.
The system checks for any executable under directories set in PATH from start to end. If the required binary is found in a directory, it will ignore the rest. In case, you have added an executable at the end, the system may ignore that if an executable is found with the same name in previously configured directories.
- Setting up after other executables:1export PATH=${PATH}:/opt/maven/bin
- Setting up before other executables:1export PATH=/opt/maven/bin:${PATH}
/etc/environment
file, just set the path without export
command, like:1 | PATH=/opt/maven/bin:${PATH} |
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.