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
I am using Ubuntu 10.04. Suppose I want to add
/usr/local/foo
to myPATH
. Which file (.bashrc
,.profile
,.bash_login
, etc...) should I modify and what should the new line(s) look like?The following command adds a path to your current path:
export PATH=$PATH:/my/custom/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:
/etc/profile (which starts by loading everything in /etc/profile.d) ~/.profile (which starts by loading ~/.bashrc if you are running bash)
Notes
~/.profile
is only loaded if~/.bash_profile
and~/.bash_login
DO NOT EXIST. Otherwise, at least bash, will load them instead. It is advisable to use.profile
and not the bash specific scripts. So, if in these attempts you created.bash_login
, please delete it now.~/.bashrc
is only loaded if you are running an interactive session. (something with a prompt where you can actually type something).~/.bashrc
is loaded again and again, every time you open up a new terminal. So a new tab in gnome-terminal, a new virtual terminal, etc. So even if you don't login again,.bashrc
is loaded (and thereby resets its environment) every time you open a new shell.Things like byobu should really go into
.profile
, (otherwise it won't work ;-)Things like paths should go into
.profile
if you want them to work outside of the interactive sessions. (say when you press Alt+F2 in GNOME)
I'll mark this as the answer if you update it to include the requested export line that should be added to .profile.
This used to be valid only for console logins (e.g. ssh, or the virtual terminals accessible for Ctrl+Alt+Fx). I didn't know that /etc/gdm/Xsession sources ~/.profile these days. Neat!
Yeah, i didn't mention /etc/gdm/Xsession specifically or ~/.Xprofile because there are better ways to make graphical programs launch at start up, which does garantuee that the rest of the environment is already loaded.
to make this answer more comprehensive please add MattH's comment about sourcing ~/.profile to activate changes without a logoff/on cycle.
I am running 10.10 and I made sure ~/.bash_profile and ~/bash_login don't exist but my ~/.profile still doesn't seem to be executed when I open the terminal I tested by adding `echo hello` to the bottom of it and its not showing up.
@JoshuaFlanagan: `export` is not necessary for `PATH`. `PATH` is already an environment variable (as opposed to a shell variable)
@schwiz: `~/.profile` is not executed on each terminal, it is executed before, when your desktop session starts. The one executed on every terminal is `~/.bashrc`
I got it to work by modifying
~/.profile
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.
To add the usr/local/foo directory to my path for every session going forward, I add/edit the following line at the end of my .profile:
export PATH=$PATH:/usr/local/foo
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).
Make that `export PATH="$PATH:/usr/foo"`, in case you ever have spaces or other special characters in `$PATH`.
You can reload the current environment without logging out by typing ". ~/.profile"
@MattH: no you can't. if you source `~/.profile` in a given terminal, it will be effective for that terminal *only*
@MestreLion - you are right. I was mentioning it for convenience for the current terminal. Forgot to add that.
What if I already have something in PATH? Could I append to it like PATHS work in Windows? For example I have `PATH="$HOME/bin:$HOME/.local/bin:$PATH"` already.
To reload .profile and take changes effects without logout/login, run:
source ~/.profile
this one should be a comment to previous answer
You can add the path to
/etc/environment
, but be aware that no shell expansions will work; the variable will be set to literally the characters you enter.Out of the two methods(adding export command in .profile and adding full path name to PATH in etc/environment), which should be preferred?
You can modify the
.bashrc
file in your$HOME
directory.At the very end of this file, add the line:
export PATH="$HOME/directory_to_include_in_path/:$PATH"
You can also modify the
.profile
file, also in your$HOME
directory, including the following line:PATH="$HOME/directory_to_include_in_path/:$PATH"
This worked for me.
I don't see what this adds to the other answers which explain more thoroughly.
This is correct answer if you want change to affect terminal windows. Changing `.profile` has no effect in terminal windows.
If you have ohmyzsh goto your home directory via the terminal and type
nano .zshrc
At the end of the file enter
export PATH="$HOME/directory_to_include_in_path/:$PATH"
Finally restart your terminal. Worked for me. Hope this was helpful.
This is what worked for me
While setting
JAVA_HOME
variableIn the Terminal, run to create the variable
echo 'export JAVA_HOME=“/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home' | sudo tee -a ~/.profile
run to add the variable to the Path
echo 'export PATH="${JAVA_HOME}/bin:$PATH"' | sudo tee -a ~/.profile
then
source ~/.profile
To see if the variable is set correctly run
vi .profile
then
:q
to quitTo modify the .profile file (In case of a correction) run
sudo vi .profile
Press I to insert.
After modifications press Esc and
:wq
to save and quit.Going through the basics, I will suggest the following steps:
- It's recommended to set environment variables in
/etc/environment
Open the file as superuser in an editor as it's a read only file e.g. gedit:
gksu gedit /etc/environment
- System will need password to open it in editable mode. Enter your superuser password and get file opened in a new gedit window.
- Add new line at the end of file with
export PATH=$PATH:/usr/local/foo
- Save and close the window. It will get command back to terminal.
Refresh the environment by running the following command:
. /etc/environment
You may check by executing the following command:
echo $PATH
This is incorrect and will not work. Parameter expansion is not performed in `/etc/environment`
- It's recommended to set environment variables in
License under CC-BY-SA with attribution
Content dated before 6/26/2020 9:53 AM
Joshua Flanagan 10 years ago
I'll mark this as the answer if you update it to include the requested export line that should be added to .profile.