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

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 myprogram.sh at /home/susmit/Desktop directory. And it has to be executable by all.

Script (myprogram.sh) >

#!/bin/bash

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

Here, I will execute the myprogram.sh 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 ‘script1.sh’, where I will add the /home/susmit/Desktop directory to the PATH variable. And, I will execute the previously created ‘myprogram.sh‘ program from this script (script1.sh). 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 script1.sh
EXPLANATION
  • nano: Opens the nano text editor.
  • script1.sh: Bash script name.

❸ Copy the script mentioned below:

#!/bin/bash

#This script demonstrates the use of the $PATH variable

#Add a new directory to the PATH
new_path="/home/susmit/Desktop"
export PATH=$PATH:$new_path

echo "Current PATH: $PATH"

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

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

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

#! /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 myprogram.sh and prints a message depending on the existence or not. Afterward, the myprogram.sh 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 script1.sh
EXPLANATION
  • chmod: Changes the permissions of files and directories.
  • +x: Argument with the chmod command to add the executable permission.
  • script1.sh: File that you want to make executable.

❻ Run the script by the following command:

./script1.sh

The myprogram.sh 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 myprogram.sh 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.

Conclusion

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
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
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