Open In App

How To Set $PATH Permanently in Linux

Last Updated : 02 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

An essential part of Linux and other Unix-like operating systems is the $PATH variable. It instructs the shell where to look for these executable files and specifies a list of directories that house various executables on the system. The folders in $PATH on a brand-new Linux installation ensure that most of the programs you run function flawlessly. On your system, you might occasionally want to quickly run customized scripts or apps. In these circumstances, you must adequately specify the $PATH variable before executing those apps.

What Is $PATH in Linux?

As was already established, the Linux shell will refer to the folders listed in the $PATH environment variable when it receives a command or an exec call from a program. The $PATH variable typically contains the directories /bin, /usr/bin, and /usr/local/bin, with superuser receiving two additional entries, namely /sbin and /usr/sbin. To simply start scripts and programs from the terminal regardless of the current working directory, you can add additional entries to your $PATH variable.

To see the current PATH setting:

echo $PATH

How to Set the $PATH Variable in Linux?

There are various ways to add a directory to your $PATH, depending on what you want to do, whether it’s a temporary configuration to run a script only once or you plan to use the script frequently.

  • Method 1 – Setting $PATH Variable Temporarily
  • Method 2 –  Setting a Permanent $PATH Variable

Method 1: Setting $PATH Variable Temporarily

Setting a temporary $PATH variable is an option if you just want to run a script or program within the current (active) session. You will then be able to run that program with a command from anywhere on your system and only in the current session, rather than needing to include its complete path in the command.

Syntax:

Open the terminal and enter the following syntax to add folders to the temporary $PATH variable:

export PATH=$PATH:/path/to/directory
cmd to add folder to temporary $PATH

 

Example: 

To set the temporary path of the go program we use the below cmd.

export PATH=$PATH:/usr/local/go/bin
Set temporary path

 

Note: Keep in mind that with this setup, you can only run the program during the current session. After you restart your computer, the system will revert the $PATH variable you temporarily set to its default value.

Method 2: Setting a Permanent $PATH Variable

We must permanently configure the $PATH variable for any software on your system that you anticipate using regularly. This will ensure that your shell will remember its directory even after a reboot. Setting $PATH permanently, however, entails a few extra steps in contrast to the temporary solution: 

Find the configuration file for the shell you’re using, change it, and add the $PATH variable there.

Step 1: Run the following command in the terminal to determine your system’s shell:

echo $0
Check System Shell

 

The output should show the name of your current shell. Unless you’ve changed it to Zsh or another Linux shell, this will often be the Bash shell.

We must now edit the configuration file for the shell you are using. Depending on your shell, you should change the following file:

Shell Name Configuration File
1. bash ~/.bashrc
2. zsh ~/.kshrc
3. ksh ~/.kshrc
4. csh ~/.cshrc

The methods below will help you set the $PATH variable after you’ve identified the file that needs editing. In this tutorial, I’ll show you how to configure the $PATH variable in the Bash shell.

Step 2: Open the terminal and enter the following command to open the .bashrc file.

nano ~/.bashrc
.bashrc file

 

Syntax:

Use the following syntax to set the $PATH variable.

export PATH=$PATH:/path/to/directory/

Example:

Here We have added the path of the go program at the end of the .bashrc file.

editing .bashrc file

 

Step 3: Save the file and reload the current shell environment using the source.

reload current shell

 

Step 4: Type the following command to check whether the directory has been added or not.

check the added path

 

All the scripts in the folder should be executable from any directory on your system if you input the directory path correctly, which will appear in your system’s $PATH. Furthermore, Linux gives you the option to permanently configure the $PATH variable in a system-wide configuration so that all users on your network can access and run the customized scripts that are located in the given directory. The same command you used to add temporary and permanent setup must be added to the /etc/environment or /etc/profile file if you want to set $PATH globally.

Open either of these files and append the path of the directory:

nano /etc/environment
nano /etc/profile

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads