Tutorial

How To View and Update the Linux PATH Environment Variable

Published on July 20, 2022
How To View and Update the Linux PATH Environment Variable

The author selected the Wikimedia Foundation to receive a donation as part of the Write for DOnations program.

Introduction

After installing a command-line program, you may only be able to run it in the same directory as the program. You can run a command-line program from any directory with the help of an environment variable called PATH.

The PATH variable contains a list of directories the system checks before running a command. Updating the PATH variable will enable you to run any executables found in the directories mentioned in PATH from any directory without typing the absolute file path.

For example, instead of typing the following to run a Python program:

  1. /usr/bin/python3

Because the /usr/bin directory is included in the PATH variable, you can type this instead:

  1. python3

The directories are listed in priority order, so the ones that will be checked first are mentioned first.

In this tutorial, you will view the PATH variable and update its value.

Prerequisites

For an overview of environment variables, refer to the How To Read and Set Environmental and Shell Variables on Linux article.

Step 1 — Viewing the PATH Variable

You can view the PATH variable with the following command:

  1. echo $PATH

An unchanged PATH may look something like this (file paths may differ slightly depending on your system):

Output
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

Some directories are mentioned by default, and each directory in PATH is separated with a colon :. The system checks these directories from left to right when running a program.

When a command-line program is not installed in any of the mentioned directories, you may need to add the directory of that program to PATH.

Step 2 — Adding a Directory to the PATH Environment Variable

A directory can be added to PATH in two ways: at the start or the end of a path.

Adding a directory (/the/file/path for example) to the start of PATH will mean it is checked first:

  1. export PATH=/the/file/path:$PATH

Adding a directory to the end of PATH means it will be checked after all other directories:

  1. export PATH=$PATH:/the/file/path

Multiple directories can be added to PATH at once by adding a colon : between the directories:

  1. export PATH=$PATH:/the/file/path:/the/file/path2

Once the export command is executed, you can view the PATH variable to see the changes:

  1. export PATH=$PATH:/the/file/path
  2. echo $PATH

You will see an output like this:

Output
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/the/file/path

This method will only work for the current shell session. Once you exit the current session and start a new one, the PATH variable will reset to its default value and no longer contain the directory you added. For the PATH to persist across different shell sessions, it has to be stored in a file.

Step 3 — Permanently Adding a Directory to the PATH Variable

In this step, you will add a directory permanently in the shell configuration file, which is ~/.bashrc if you’re using a bash shell or ~/.zshrc if you’re using a zsh shell. This tutorial will use ~/.bashrc as an example.

First, open the ~/.bashrc file:

  1. nano ~/.bashrc

The ~/.bashrc file will have existing data, which you will not modify. At the bottom of the file, add the export command with your new directory:

  1. ...
  2. Adding paths to your PATH
  3. export PATH=$PATH:the/file/path

Use the methods described in the prior section to clarify whether you want the new directory to be checked first or last in the PATH.

Save and close the file. The changes to the PATH variable will be made once a new shell session is started. To apply the changes to the current session, use the source command:

  1. source ~/.bashrc

You can add new directories in the future by opening this file and appending directories separated by a colon : to the existing export command.

Conclusion

The PATH environment variable is a crucial aspect of command-line use. It enables you to run command-line programs, such as echo and python3, from any directory without typing the full path. In cases where adding the directory to PATH isn’t part of the installation process, this tutorial provides the required steps. For more on environmental variables, see How To Read and Set Environmental and Shell Variables on Linux.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Piya Gehi

author


Default avatar

Technical Editor


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.