How to Set Bash “$PATH” Variable? [Easiest Configuration]

A Complete Guide for Beginners Enroll Course Now

The $PATH variable is one of the most important environment variables in Linux. It stores the path of the directories. When programmers give a command to execute a program, initially the program is searched inside all the directories enlisted inside the bash $PATH variable. This is a great help for the programmers because it enables them to execute a program from any directory. They can enlist the path of their frequently used program and run them from any location they desire.

Key Takeaways

  • Learning about the $PATH variable.
  • Learning how to add a new directory to the $PATH variable.
  • Learning how to execute programs from different directories.

Free Downloads

What is the “$PATH” Variable in Linux?

The $PATH variable in Linux is basically an environment variable that specifies a list of directories where the system should look for executable files when you enter a command in the terminal without providing a full path to the executable. This allows you to run commands from anywhere in the terminal without having to specify the full path to the executable each time.

How to Set a “$PATH” Variable and Update?

If you keep your file in such a location that is not currently listed on your $PATH variable you can easily enlist that variable on the $PATH variable using the export command. Suppose I want to enlist the /home/susmit/script-location directory to the $PATH variable. For this execute the following command:

export PATH=/home/susmit/script-location:$PATH

Then I have to source ~/.profile to activate this new change. For this execute the below command:

source ~/.profile

The ~/.profile is a startup file located inside the user’s home directory. It contains all the PATH variable lists. After changing the PATH variable, this file needs to be updated using the source command.

Configuring the “$PATH” Variable Using a Bash Script

You can execute a program located in any directory if its location is enlisted on the $PATH variable. I have a script named at /home/susmit/Desktop directory. And it has to be executable by all.

Script ( >


#printing a message on the terminal
echo "Hello from myprogram!"

Here, I will execute the Bash script from the home directory, which is located inside the /home/susmit/Desktop folder. For this, I will create a new bash script named ‘’, where I will add the /home/susmit/Desktop directory to the PATH variable. And, I will execute the previously created ‘‘ program from this script ( After that, I will also show how you can remove the added directory from the $PATH variable. To know more, follow the below steps:

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a file in Nano:

  • nano: Opens the nano text editor.
  • Bash script name.

❸ Copy the script mentioned below:


#This script demonstrates the use of the $PATH variable

#Add a new directory to the PATH
export PATH=$PATH:$new_path

echo "Current PATH: $PATH"

#Check if a program is in the PATH
if command -v $program_to_check &>/dev/null; then
echo "$program_to_check is found in the PATH."
echo "$program_to_check is not found in the PATH."

#Using a program from the newly added directory
echo "Running a program from $new_path:"

#Remove the added directory from the PATH
export PATH=${PATH//$new_path:/}
echo "Updated PATH: $PATH"

#! /bin/bash ‘#!’, is called shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash. Then the new_path=”/home/susmit/Desktop” command assigns the directory to the new_path variable. After that, the export PATH=$PATH:$new_path command has exported the new_path variable.

Afterwards, the echo “Current PATH: $PATH” command has printed the current value of the $PATH variable. After that the if command checks the existence of the and prints a message depending on the existence or not. Afterward, the bash script has been executed. Finally, the newly added directory has been removed from the $PATH variable and printed the current value of the $PATH variable.

❹ Press CTRL+O and ENTER to save the file; CTRL+X to exit.

❺ Use the following command to make the file executable:

chmod u+x
  • chmod: Changes the permissions of files and directories.
  • +x: Argument with the chmod command to add the executable permission.
  • File that you want to make executable.

❻ Run the script by the following command:


The Bash script has been executed from the user’s home directory. Then the PATH variable has been updated and the updated value has been printed on the terminal.The image shows that the Bash script has been executed from the user’s home directory. Then the PATH variable has been updated and the updated value has been printed on the terminal.


Learning the configuration of the Bash $PATH variable is an essential skill for any Linux user by enabling them to run any program from anywhere. It makes them an efficient programmer. This article has outlined the easiest and most effective methods for setting and modifying the $PATH variable, making readers skillful in executing their necessary commands from any directory whenever necessary.

People Also Ask

What is the purpose of the PATH Bash variable?
The PATH variable is one of the most important environment variables. This variable tells the bash shell where to find different executable files and scripts. The shell checks the script inside the directories listed inside the PATH variable.

Where is the PATH variable stored?
The Bash $PATH variable is an important environment variable. The default systemwide PATH value is stored in the /etc/profile file.

How do PATH variables work?
The PATH variable contains a list of paths that Linux will search for executable commands. Linux traverses the colon-separated paths in order until it finds an executable program/command.

Who searches the PATH variable?
When users specify only a command name, the shell searches the command inside the directories enlisted inside the PATH variable. If the shell finds the command, it can execute the command.

Related Articles

<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial

Rate this post
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment