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
I am using Ubuntu 10.04. Suppose I want to add /usr/local/foo to my PATH. Which file (.bashrc, .profile, .bash_login, etc...) should I modify and what should the new line(s) look like?
10 Answers
The following command adds a path to your current path:
export PATH=$PATH:/my/custom/pathIf 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
~/.profileis only loaded if~/.bash_profileand~/.bash_loginDO NOT EXIST. Otherwise, at least bash, will load them instead. It is advisable to use.profileand not the bash specific scripts. So, if in these attempts you created.bash_login, please delete it now.~/.bashrcis only loaded if you are running an interactive session. (something with a prompt where you can actually type something).~/.bashrcis 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,.bashrcis 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
.profileif you want them to work outside of the interactive sessions. (say when you press Alt+F2 in GNOME)
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/fooHowever, 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).
5To reload .profile and take changes effects without logout/login, run:
source ~/.profile 1 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.
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.
2Before 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.
Example: whereis ls command shows ls is there inside /bin.
The ls command only works if /bin is registered in the path variable.
echo $PATH gives the currently registered locations. If you want to add another custom location to your path variable there are several ways you can try.
PATH="$PATH:/someLocation"
New Path variable is only valid till your terminal closes. No other terminal will be affected. No subprocess can use the new variable.export PATH="$PATH:/someLocation"
New Path variable is valid till your terminal close also all subprocesses will get the new Path variable. No other terminal will get a new variable.export PATH="$PATH:/someLocation"
Add this line into the .bashrc file present in your home folder. Which is called every time a new bash shell is created. This means you get a new Path variable exported every time a new terminal is opened. But this variable is created for only bash shells. You can use the old Path variable in other shells(ksh, sh, ssh ..).
export PATH="$PATH:/someLocation"
Add this line into the .profile file present in your home folder. Which is called every time you log in. This means you get a new Path variable exported every time your session is created. Which is available everywhere.
If you can't find the .profile or .bashrc file in your home folder, try to create a new one. Sometimes these files won't be created by the system.
ps: Works in ubuntu. Open to any corrections.
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 variable
In the Terminal, run to create the variable
echo 'export JAVA_HOME=“/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home' | sudo tee -a ~/.profilerun to add the variable to the Path
echo 'export PATH="${JAVA_HOME}/bin:$PATH"' | sudo tee -a ~/.profilethen
source ~/.profileTo see if the variable is set correctly run
vi .profilethen :q to quit
To modify the .profile file (In case of a correction) run
sudo vi .profilePress I to insert.
After modifications press Esc and :wq to save and quit.
First add a shell script to the /etc/profile.d directory.
echo 'export PATH=$PATH:/path/to/app' | sudo tee /etc/profile.d/custom-apps-path.sh > /dev/nullThe app will then be available via direct invocation the next time you login to the shell.
To make the app instantly available in the current shell, run the following command:
source /etc/profile.d/custom-apps-path.sh 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/environmentYou may check by executing the following command:
echo $PATH