- What is the PATH environment variable and how do I add to it?
- 4 Answers 4
- Method 1
- Method 2
- List all variables on terminal
- Show one variable at a time
- Add or change a variable
- Linux path environment variable
- The Linux PATH Variable
- View currently configured directories in $PATH
- Add Directory to PATH Variable
- Temporarily add a directory to $PATH
- Permanently add a directory to $PATH
- Remove directory from $PATH
- Closing Thoughts
- Related Linux Tutorials:
- How to set your $PATH variable in Linux
- View your PATH
- Set your PATH
- Set your PATH permanently
What is the PATH environment variable and how do I add to it?
PATH is a global operating system variable that contains names of files that are to be executed without specyfing the whole path to them. For example You can just write startx to start graphic environemnt instead of /bin/some other folders/startx
After editing .profile restart your system to get the changes to take effect. (Perhaps there is a way around this, but restarting certainly works)
4 Answers 4
PATH is an enviroment variable. It basically tells your machine where to search for programs, so when you run your picc program you can just do this:
To add a directory to your $PATH , follow either one of the options below.
Method 1
PATH="$HOME/bin:$PATH:/usr/hitech/picc/9.82/bin"
Method 2
Run the command below in the terminal:
export PATH=$PATH:/usr/hitech/picc/9.82/bin
i do not clearly understand what do i edited /etc/profile but the link in your answer says something like this if [ -d «$HOME/bin» ] ; then PATH=»$PATH:$HOME/bin» fi what do do with this?
Just keep adding a colon, then your new path to that string. i.e. PATH=»$HOME/bin:$PATH:/usr/hitech/picc/9.82/bin:/my/new/path»
Shell environment variables are used for several purposes, from storing data, storing software configurations, set terminal settings, and changing shell environment. The environment variables are normally set at boot time, or by different software as required. One way of setting environmental variables is from the command line.
List all variables on terminal
this will print all the variable that you have
Show one variable at a time
The amount of these variables can become a very long list and locating one specific variable can become a tough task. Fortunately Linux allows us to display the value of one shell variable by using the echo command along with the name of the variable. This makes the task very easy. example: echo «$HOME»
Add or change a variable
To add or modify an environment variable, we can use the export command followed by the name of the variable and the values that go with it.
export NameofVariable='value'
Note, however, that this will only work for the current shell session. It won’t be available in any other terminals.
Linux path environment variable
The PATH environment variable is an essential component of any Linux system. If you ever use the command line at all, the system is relying on the PATH variable to find the location of the commands you are entering.
In this tutorial, you’ll learn about the PATH environment variable and how it works. You’ll also see how it can be modified by removing paths or adding your own custom directories to the variable.
In this tutorial you will learn:
- What is the PATH variable and how’s it work?
- How to temporarily or permanently add a directory to $PATH
- How to remove a directory from $PATH
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | N/A |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions | # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux commands to be executed as a regular non-privileged user |
The Linux PATH Variable
When you type a command into a Linux terminal, what’s really happening is that a program is being executed. Normally, to execute a custom program or script, we need to use its full path, such as /path/to/script.sh or just ./script.sh if we’re already in its residing directory. Alternatively, we can execute a lot of commands without specifying paths, by simply typing a command like uptime or date , etc.
The reason we don’t need to specify paths for some commands is because of the $PATH variable. This is a variable that can be configured to tell our Linux system where to look for certain programs. That way, when typing a command into the terminal, Linux checks the $PATH variable to see a list of directories to look for the program.
View currently configured directories in $PATH
Seeing all the directories that are currently configured in your system’s $PATH variable is easy. Just use the echo command like this:
As you can see, there are a few different directories already stored in $PATH. This is what allows us to run so many commands by default, without specifying their full location in the terminal.
To see what directory a command belongs to, you can use the which command.
Add Directory to PATH Variable
Adding a directory to the PATH variable will enable you to call on your program or script from anywhere in the system, without needing to specify the path to where you’ve stored it.
Follow along with the steps below to see how to add a directory either temporarily or permanently to the PATH variable.
Temporarily add a directory to $PATH
To add a directory to $PATH for the current session, use the following command syntax. In this example, we’re adding the /bin/myscripts directory.
$ export PATH="/bin/myscripts:$PATH"
You can verify afterwards that the directory has been added.
Now, files we have stored in the /bin/myscripts directory can be executed anywhere, without specifying their full path. This configuration will change when we end the current session (reboot the PC or close the terminal). To make it permanent, check out the section below.
Permanently add a directory to $PATH
To add a directory to $PATH permanently, we’ll need to edit the .bashrc file of the user you want to change. Use nano or your favorite text editor to open the file, stored in the home directory.
At the end of this file, put your new directory that you wish to permanently add to $PATH.
export PATH="/bin/myscripts:$PATH"
Save your changes and exit the file. Afterwards, execute the following command to make the changes take effect in your current session. Alternative, you can log out or reboot the system.
That’s all there is to it. You can check $PATH once more to verify the change.
Remove directory from $PATH
You can remove a directory from PATH by editing the appropriate file and removing the undesirable directory. The directories for PATH can be configured in the ~/.bashrc file (per user basis) or the /etc/environment (system wide variables).
As an example, here’s what the /etc/environment file looks like on our test system.
Removing directories from here would take effect across the whole system, whereas removing directories from .bashrc will only affect one user.
We elaborate more on these files in our guide on how to set and list environment variables.
Closing Thoughts
In this guide, we learned about the $PATH variable and how it controls what commands are able to be executed without specifying their full path. We also saw how to remove a directory or add new programs or scripts to $PATH either temporarily or permanently.
Related Linux Tutorials:
Comments and Discussions
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.
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.
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.