How to Set the $PATH Variable in Linux
As a Linux user, you may have come across the term “PATH variable” when working with the command line. The $PATH variable is a system variable that determines where the operating system searches for executable files when you run a command.
By default, Linux looks for executable files in the directories specified in the PATH variable. Therefore, it’s essential to set this variable correctly when installing new programs or services to ensure that they are accessible from the command line.
Here are the steps to set the $PATH variable in Linux:
Step 1: Determine the location of the executable file
Before setting the $PATH variable, you need to know the location of the executable file. You can do this by running the following command:
which filename
Replace ‘filename’ with the name of the program or service you want to set the path for. The output will show you the directory location of the file.
Step 2: Set the $PATH variable
Now that you know the location of the executable file, you can set the path. There are two ways to set the $PATH variable temporarily or permanently.
To set the $PATH variable temporarily, run the following command:
export PATH=$PATH:/path/to/directory
Replace ‘/path/to/directory’ with the directory location of the file you wish to add to your PATH. This command will add the directory location to the existing $PATH variable.
To set the $PATH variable permanently, you need to edit the ‘.bashrc’ file in your home directory. You can do this by running the following command:
nano ~/.bashrc
This command will open the ‘.bashrc’ file in the nano text editor. Find the line that starts with ‘export PATH=’ and add the directory location to the end of the list. Here is an example:
export PATH=$PATH:/home/username/bin
Save the file by pressing ‘Ctrl + O’ and exit by pressing ‘Ctrl + X’. You can apply the changes by either opening a new terminal or running the following command:
source ~/.bashrc
This will reload the ‘.bashrc’ file and apply the changes to the $PATH variable.