Ubuntu how to add to path
Ubuntu how to add to path
The environment variables control the behavior of the shell. Environment variables are the variables that set the working environment of the shell. Some of the environment variables are USER, HOME, SHELL, PWD, SHELL, PS1, PS2, etc.
The HOME variable contains the path of home directory of the user. Similarly, the other variables contain other values needed for the operation of the shell. This tutorial discusses an important shell environment variable called PATH and how you can add values to this variable.
Show Variable Value
The shell interprets the value of a variable by the «$» sign. To display the value of a variable, precede the variable with «$» sign. The «echo» command is used to display the value of the variable. Let us display the value of the variable HOME:
What is PATH variable
The commands in Unix/Linux are the binary executable files. When you enter a command at the shell prompt, the binary file with that name is executed. So when a command is entered at the prompt, the shell searches for that binary file in some directories. These directories are listed in the PATH variable. Without the PATH variable, no command can be executed.
Now let’s show the current value of PATH variable.
The directories in this variable are separated by : (colon). At present, the shell searches the following directories for the binary executable files:
The order of the search path is also important. Suppose, you enter the ls command at the command line. Now at first, the shell searches /usr/local/sbin directory. If it does not find an executable file with that name in this directory, then it looks in the /usr/local/bin directory and then /usr/sbin, followed by /usr/bin and so on. If none of the directories specified in the PATH variable contains that file, then «command not found» error is displayed on the terminal.
Which command is used to find out the directory that contains a particular command. This command also uses the PATH variable to search for the command location. For example,
It means that when you type ls at the prompt and execute the ls command, then “/bin/ls” file is executed. In other words, location of ls command is “/bin/ls”. You can also find path of other commands with «which» command.
Add Directories to PATH
Now assume that you create your shell scripts in bin directory located in your home directory, i.e. in “
represents home directory); and now you want to add this directory to your PATH variable as well, so that you do not have to specify the path to your script every time you run the script.
The value of a variable is changed with the syntax “variable=value”. But here, we want to add a directory to the PATH variable. We can’t just write “PATH=
The «/root/bin» directory (/root is root user’s home directory) has been added to the path variable. Now you can execute your script as a command, without specifying the full path to the script. The export command ensures that the variables are passed to the child processes without affecting the existing environment variables.
Let’s take another example where you like to add a directory (/home/tom/scripts) for a user named ‘tom’ that may contain his scripts or binaries. You can place custom script path at the front of the PATH if the system wants to find it first.
How to Set PATH permanent
/.bashrc if you are using Bash. For zsh, add it to
You can appropriately use /etc/environment and /etc/profile which defines global environment variables.
/.bashrc file in your favorite text editor and run the following command:
And, to load variable to the current shell run source command, type:
Conclusion
In this tutorial, we have learned how to add a directory to the PATH environment variable in Linux. Hope you enjoyed reading this and please leave your suggestion in the below comment section.
Dave McKay first used computers when punched paper tape was in vogue, and he has been programming ever since. After over 30 years in the IT industry, he is now a full-time technology journalist. During his career, he has worked as a freelance programmer, manager of an international software development team, an IT services project manager, and, most recently, as a Data Protection Officer. His writing has been published by howtogeek.com, cloudsavvyit.com, itenterpriser.com, and opensource.com. Dave is a Linux evangelist and open source advocate. Read more.
$PATH is one of the silent manipulators in the background of your Linux computer. It quietly affects your user experience, but there’s nothing shady about it. We’ll explain what it does, and how you can adjust it.
When you type a command in a terminal window and press Enter, you kick off quite a lot of activity before your command is even executed.
Bash is the default shell on most Linux distributions. It interprets the line of text you entered and identifies the command names intermingled with the parameters, pipes, redirections, and whatever else is there. It then locates the executable binaries for those commands and launches them with the parameters you supplied.
The first step the shell takes to locate the executable is identifying whether a binary is even involved. If the command you use is within the shell itself (a “shell builtin”) no further search is required.
Shell builtins are the easiest to find because they’re integral to the shell. It’s like having them in a toolbelt—they’re always with you.
If you want to see whether a command is a shell builtin, an alias, a function, or a standalone binary mv /work/unfile, you can use the type command as shown below:
Unsurprisingly, cd is a shell builtin.
The output is a list of colon ( : ) delimited file system locations. The shell searches from left to right through the path, checking each file system location for a matching executable to perform your command.
We can pick our way through the listing to see the file system locations that will be searched, and the order in which they will be searched:
Something that might not be immediately obvious is the search doesn’t start in the current working directory. Rather, it works its way through the listed directories, and only the listed directories.
If the current working directory isn’t in your path, it won’t be searched. Also, if you have commands stored in directories that aren’t in the path, the shell won’t find them.
We type the following which command to show us which version of our program the shell will find and use:
The shell reports the version it found is the one in the directory that’s in the path.
We type the following to fire it up:
To run any other version of rf on this computer, we’ll have to use the path to the executable on the command line, as shown below:
Now that we’ve told the shell where to find the version of rf we want to run, it uses version 1.1. If we prefer this version, we can copy it into the /usr/local/bin directory and overwrite the old one.
Or, perhaps we’ve downloaded a new version of rf and want to do some verification testing on it before we make it publicly available.
This is easy to do. For our example, we type the following to add our directory to the start of the path so it’s the first location searched:
Let’s see what the path looks like now:
Our /home/dave/work directory is added to the start of the path. The colon we provided separates it the rest of the path.
We type the following to verify our version of rf is the first one found:
To add our directory to the end of the path, we just move it to the end of the command, like so:
Making the Changes Permanent
Scroll to the bottom of the file, and then add the following export command we used earlier:
Then, type the following echo command to check the path:
This adds the /home/dave/work directory to the start of the path.
Setting the Path for Everyone
To set the path for everyone who uses the system, you can edit the /etc/profile file.
When the gedit editor launches, add the export command to the bottom of the file.
Save and close the file. The changes will take effect for others the next time they log in.
A Note on Security
Make sure you don’t accidentally add a leading colon “ : ” to the path, as shown below.
If you do, this will search the current directory first, which introduces a security risk. Say you downloaded an archive file and unzipped it into a directory. You look at the files and see another zipped file. You call unzip once more to extract that archive.
If the first archive contained an executable file called unzip that was a malicious executable, you’d accidentally fire up that one instead of the real unzip executable. This would happen because the shell would look in the current directory first.
How to Add a Directory to PATH in Linux [Quick Tip]
The PATH variable in Linux stores the path to the directories where it should look for executables when you run a command.
As you can see, the PATH consists of several directories (like /usr/local/sbin, /usr/bin and more) separated by a colon (:).
If you want to run some executables as commands 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:
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:
Things to pay attention here:
Once you have set the PATH with the new value, please check that the PATH has been correctly updated.
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 log out from the system, the PATH will revert, and the changes will be lost.
You can use a text editor like Nano or Vim for this task.
» data-medium-file=»https://itsfoss.com/wp-content/uploads/2021/05/add-directory-to-path-linux-300×169.png» data-large-file=»https://itsfoss.com/wp-content/uploads/2021/05/add-directory-to-path-linux-800×451.png» width=»800″ height=»451″ src=»https://itsfoss.com/wp-content/uploads/2021/05/add-directory-to-path-linux-800×451.png» alt=»add directory to path linux» data-lazy-srcset=»https://itsfoss.com/wp-content/uploads/2021/05/add-directory-to-path-linux-800×451.png 800w, https://itsfoss.com/wp-content/uploads/2021/05/add-directory-to-path-linux-300×169.png 300w, https://itsfoss.com/wp-content/uploads/2021/05/add-directory-to-path-linux-768×433.png 768w, https://itsfoss.com/wp-content/uploads/2021/05/add-directory-to-path-linux.png 978w» data-lazy-sizes=»(max-width: 800px) 100vw, 800px» data-lazy-src=»https://itsfoss.com/wp-content/uploads/2021/05/add-directory-to-path-linux-800×451.png?is-pending-load=1″ srcset=»data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7″>
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 a 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:
Was it clear enough?
I have tried to explain things with the necessary details but not going too deep into details. Does it make the topic clear, or are you more confused than before? If you still have doubts, please let me know in the comments.
Creator of It’s FOSS. An ardent Linux user & open source promoter. Huge fan of classic detective mysteries ranging from Agatha Christie and Sherlock Holmes to Detective Columbo & Ellery Queen. Also a movie buff with a soft corner for film noir.
How do I add an executable to my search path?
For reference, I know very little about Linux, and am using it to run a program written by someone else. The instructions say Add the executable ‘ttt’ to the search path. In most installations this can be accomplished by linking the file to the ‘bin’ subdirectory at user home.
How do I go about doing this?
This executable is currently in a subfolder in the host area, as it’s running on a dual-boot computer I cannot change the fact that it is a dual boot, as it is a work computer.
2 Answers 2
To make this work for the command line (terminal):
I would suggest that you do the following steps in the terminal:
Create a folder called bin in your home directory.
/bin to your PATH for all sessions of Bash (the default shell used inside of the terminal).
Add either the executable files themselves OR symlinks to the executable into
Restart your terminal session by closing out the terminal and reopening it, or run source
/.bashrc to reload the configuration for your session
That should allow your terminal to read the PATH variable for terminal sessions.
I do not know how to add it to the GUI, though, as I’m not certain of how the GUI manages the PATH variable(s), but it might be necessary to modify the path with other methods should this method here not work with the GUI.
How do I modify my PATH so that the changes are available in every Terminal session
I want to add a directory to search my search path. I know I have to modify the PATH environment variable. However, I want the change to be permanent, so that it is always in effect, for every Terminal (bash) window I open.
There is an overload of confusing and possibly conflicting information in https://help.ubuntu.com/community/EnvironmentVariables
10 Answers 10
The following command adds a path to your current path:
If you want your setup to execute this command every time, there are a number of places where you can put it. When you login, the following scripts will be executed in this order:
Notes
/.profile is only loaded if
/.bashrc is only loaded if you are running an interactive session. (something with a prompt where you can actually type something).
/.profile these days. Neat!
/.profile to activate changes without a logoff/on cycle.
/.profile is not executed on each terminal, it is executed before, when your desktop session starts. The one executed on every terminal is
I got it to work by modifying
It looks like adding
/bin to my path was a bad example, as there is already code in
/.profile to do that automatically, if the directory exists.
However, to make this take effect, I needed to log out and log back in (simply closing the Terminal window and opening a new one did NOT work).
/.profile in a given terminal, it will be effective for that terminal only
At the very end of this file, add the line:
This worked for me.
Before setting a PATH variable, you need to understand why you are setting the PATH variable. The Path variable is where the system tries to search when you issue some command in your terminal.
ps: Works in ubuntu. Open to any corrections.
Источники информации:
- http://www.howtogeek.com/658904/how-to-add-a-directory-to-your-path-in-linux/
- http://itsfoss.com/add-directory-to-path-linux/
- http://askubuntu.com/questions/322772/how-do-i-add-an-executable-to-my-search-path
- http://askubuntu.com/questions/3744/how-do-i-modify-my-path-so-that-the-changes-are-available-in-every-terminal-sess