• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
WebSetNet

WebSetNet

Technology News

  • Technology News
    • Mobile
    • Games
  • Internet Marketing
  • System Admin
    • Windows 11
    • Linux
    • Mac & Apple
You are here: Home / System Admin / Linux / How to Add a Directory to PATH in Linux [Quick Tip]

How to Add a Directory to PATH in Linux [Quick Tip]

May 19, 2021 by bartez64

The PATH variable in Linux stores the path to the directories where it should look for executables when you run a command.

abhishek@its-foss:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

As you can see, the PATH consists of several directories (like /usr/local/sbin, /usr/bin and more) separated by colon (:).

If you want to run some executables as command from anywhere in the system, you should add their location in the PATH variable.

This is common while setting up a development environment. For example, imagine you downloaded and installed Java and Maven. To make your programs work properly, you’ll need to specify the location of the binaries of Maven and Java in the PATH.

This quick tutorial is about setting up PATH in Linux. Apart from the steps, I’ll also mention things you should be careful about while dealing with PATH.

Adding a directory to PATH in Linux

The process to add a new directory to the PATH variable in Linux is essentially this:

export PATH=$PATH:your_directory

Where your_directory is the absolute path to the concerned directory.

Let’s say, you download and extracted Maven to the home directory and you want to add its bin directory to the PATH. Let’s assume that the absolute path of this bin directory is /home/abhishek/maven/apache-maven-3.8.0/bin.

Here’s what you should be doing:

export PATH=$PATH:/home/abhishek/maven/apache-maven-3.8.0/bin

export PATH=$PATH:/home/abhishek/maven/apache-maven-3.8.0/bin

Things to pay attention here:

  • The $ before a variable name means you are referring to its value. PATH is the variable name, $PATH is the value of variable PATH.
  • You should not use $ with PATH on the left side of =
  • There must not be any spaces before and after =
  • Don’t forget to include the : after $PATH because the directories in the PATH are separated by colon.
  • There must not be a space before and after the colon (:).

Once you have set the PATH with the new value, please check that the PATH has been correctly updated.

abhishek@its-foss:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/abhishek/maven/apache-maven-3.8.0/bin

You may want to run the command or script for which you modified the PATH. This will tell you for sure if the PATH is correctly set now.

Making the changes to PATH permanent

You added the desired directory to the PATH variable but the change is temporary. If you exit the terminal, exit the session or logout from the system, the PATH will revert and the changes will be lost.

If you want to make the changes to the PATH variable permanent for yourself, you can add it to the .bashrc file in your home directory, assuming you are using the Bash shell.

You can use a text editor like Nano or Vim for this task.

nano ~/.bashrc
add directory to path linux

If you want the modified PATH variable to be available for everyone on the Linux system, you can add the export to the /etc/profile file. This is suitable when you are a sysadmin and have a configured system with custom path.

Bonus tip: The directories take precedence in PATH

There are several directories in the PATH variable. When you run an executable file/command, your system looks into the directories in the same order as they are mentioned in the PATH variable.

If /usr/local/sbin comes before /usr/bin, the executable is searched first in /usr/local/sbin. If the executable is found, the search ends and the executable is executed.

This is why you’ll find some examples where the additional directory is added before everything else in PATH:

export PATH=your_directory:$PATH

If you think that your additional directory should be searched before everything else you should add it before the $PATH otherwise add it after $PATH.

Was it clear enough?

I have tried to explain things with necessary details but not going too deep into details. Doe sit make the topic clear or are you more confused than before? If you still have doubts, please let me know in the comments.

Original Article

Related posts:

  1. How to Install Apache Maven on Ubuntu 16.04 LTS
  2. How to Install Software from Source Code… and Remove it Afterwards
  3. Install Apache Maven 3.5 on CentOS 7
  4. How to Install Maven on Ubuntu 16.04
  5. How to Run a Shell Script in Linux [Essentials Explained for Beginners]
  6. Google Home tips and tricks: Master your Nest Mini, Max, Audio, Hub and Hub Max
  7. Google Home tips and tricks: Master your Mini, Max, Hub and Hub Max
  8. Fast Delete Complex Directories
  9. How to Find the PID of a Linux Process With pidof or pgrep
  10. How to Create Bash Aliases

Filed Under: Linux Tagged With: directory, linux, path, tip]

Primary Sidebar

Popular Posts

  • What Is Shader Compilation and Why Does It Make PC Games Stutter? 2.1k views
  • 3 Ways to Disable GetApps on Xiaomi, Redmi, and Poco Phones Running MIUI 500 views
  • Enable or Disable Adjust for Daylight Saving Time in Windows 10 400 views
  • How To Restore Last Session On Google Chrome 300 views
  • How to Highlight Duplicates in Google Sheets 300 views
  • Exclamation Mark on Network Signal, Mobile Data Not Working? 8 Ways to Fix 300 views
  • How do I enable or disable Alt Gr key on Windows 10 keyboard 300 views
  • How to Block TLS 1.0 and TLS 1.1 in Microsoft Edge and Google Chrome 200 views
  • How to delete Downloaded, Failed & Pending Windows Updates 200 views
  • Dual Booting Ubuntu With Windows 10 Pro With BitLocker Encryption 200 views
  • What is "Workspace Trust" in Visual Studio Code? 200 views
  • How to change Outlook Navigation Pane position 200 views
  • Fix Explorer.exe Application error on Windows 10 200 views
  • LiVES Video Editor 2.8.5 Adds Experimental Wayland Support 200 views
  • Microsoft Store Error 0x80D03805 on Windows 10 200 views
  • Is Outbyte PC Repair Safe? 200 views
  • Will PS5 Play PS4 and PS3 Games? 200 views

Footer

Tags

Amazon android Apple Asus available download: edge feature features first free from galaxy Game games gaming gets google install Intel iPhone launches linux Microsoft more OnePlus phone release released review: samsung series support this Ubuntu update using video watch what will windows with xbox your

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org