Path environment variable setting linux

How To View and Update the Linux PATH Environment Variable

How To View and Update the Linux PATH Environment Variable

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:

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

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:

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:

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

Читайте также:  Linux прекратить выполнение скрипта

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

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

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:

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:

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:

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.

Источник

How to set your $PATH variable in Linux

Telling your Linux shell where to look for executable files is easy, and something everyone should be able to do.

Читайте также:  Отключить заставку linux mint

A path through nature

Thomas Hendele on Pixabay (CC0). Modified by Opensource.com. CC BY-SA 4.0.

Being able to edit your $PATH is an important skill for any beginning POSIX user, whether you use Linux, BSD, or macOS.

When you type a command into the command prompt in Linux, or in other Linux-like operating systems, all you’re doing is telling it to run a program. Even simple commands, like ls, mkdir, rm, and others are just small programs that usually live inside a directory on your computer called /usr/bin. There are other places on your system that commonly hold executable programs as well; some common ones include /usr/local/bin, /usr/local/sbin, and /usr/sbin. Which programs live where, and why, is beyond the scope of this article, but know that an executable program can live practically anywhere on your computer: it doesn’t have to be limited to one of these directories.

When you type a command into your Linux shell, it doesn’t look in every directory to see if there’s a program by that name. It only looks to the ones you specify. How does it know to look in the directories mentioned above? It’s simple: They are a part of an environment variable, called $PATH, which your shell checks in order to know where to look.

View your PATH

Sometimes, you may wish to install programs into other locations on your computer, but be able to execute them easily without specifying their exact location. You can do this easily by adding a directory to your $PATH. To see what’s in your $PATH right now, type this into a terminal:

You’ll probably see the directories mentioned above, as well as perhaps some others, and they are all separated by colons. Now let’s add another directory to the list.

Set your PATH

Let’s say you wrote a little shell script called hello.sh and have it located in a directory called /place/with/the/file. This script provides some useful function to all of the files in your current directory, that you’d like to be able to execute no matter what directory you’re in.

Simply add /place/with/the/file to the $PATH variable with the following command:

export PATH=$PATH:/place/with/the/file

You should now be able to execute the script anywhere on your system by just typing in its name, without having to include the full path as you type it.

Читайте также:  Mac os el capitan linux

Set your PATH permanently

But what happens if you restart your computer or create a new terminal instance? Your addition to the path is gone! This is by design. The variable $PATH is set by your shell every time it launches, but you can set it so that it always includes your new path with every new shell you open. The exact way to do this depends on which shell you’re running.

Not sure which shell you’re running? If you’re using pretty much any common Linux distribution, and haven’t changed the defaults, chances are you’re running Bash. But you can confirm this with a simple command:

That’s the «echo» command followed by a dollar sign ($) and a zero. $0 represents the zeroth segment of a command (in the command echo $0, the word «echo» therefore maps to $1), or in other words, the thing running your command. Usually this is the Bash shell, although there are others, including Dash, Zsh, Tcsh, Ksh, and Fish.

For Bash, you simply need to add the line from above, export PATH=$PATH:/place/with/the/file, to the appropriate file that will be read when your shell launches. There are a few different places where you could conceivably set the variable name: potentially in a file called ~/.bash_profile, ~/.bashrc, or ~/.profile. The difference between these files is (primarily) when they get read by the shell. If you’re not sure where to put it, ~/.bashrc is a good choice.

For other shells, you’ll want to find the appropriate place to set a configuration at start time; ksh configuration is typically found in ~/.kshrc, zsh uses ~/.zshrc. Check your shell’s documentation to find what file it uses.

This is a simple answer, and there are more quirks and details worth learning. Like most everything in Linux, there is more than one way to do things, and you may find other answers which better meet the needs of your situation or the peculiarities of your Linux distribution. Happy hacking, and good luck, wherever your $PATH may take you.

This article was originally published in June 2017 and has been updated with additional information by the editor.

Источник

Оцените статью
Adblock
detector