How to set environmental variable "PATH" permanently?

Asked by Karthik. B on 2007-01-15

I wrote my own shell scripts and kept those in bin directory in my home directory. I set the PATH variable as PATH=$PATH:$HOME/bin so that i can run those scripts directly as usual commands whatever may be the present working directory. But it is working for that session only. If I close the shell prompt and reopen it, the previous PATH variable that i set is disappearing from PATH. Now, the scripts which are in bin directory, can no more run as usual commands. I need to again set the PATH variable to that bin directory.
Is there any way to set environmental variable "PATH" permanently so that it will not disappear from PATH variable listing?

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Karthik. B
Solved:
2007-01-17
Last query:
2007-01-17
Last reply:
2007-01-15
Jordan Kanter (jikanter) said : #1

Hello

assuming you are using bash as your shell, put this in your .bashrc file. Your .bashrc file should be located in /home/Yourusername/.bashrc or ~/.bashrc for short.

this is what you should put in that file:

export PATH=$PATH:/home/YourUserName/bin

This should work, let me know if it does not.

Jordan

Paolo Sammicheli (xdatap1) said : #2

Hi,

If you want to set the path of a user you can edit your .bash_profile in home directory.

In my .bash_profile i have:

# set PATH so it includes user's private bin if it exists
if [ -d ~/bin ] ; then
    PATH=~/bin:"${PATH}"
fi

If you don't have it copy and paste this code.

Let me know if you solved this problem.

Bye

Best Karthik. B (karthik-bhuvana) said : #3

Thanku very much.
I alread had

f [ -d ~/bin ] ; then
    PATH=~/bin:"${PATH}"
fi

lines of code in .bash_profile file. Still problem exists. So I made an entry "export PATH=$PATH:/home/YourUserName/bin" in .bashrc file as per Jordan's suggestion. Now the problem solved.

The .bash_profile file is read and executed when bash is invoked as a login shell, otherwise the .bashrc file will be read and executed.

That's why this code:

if [ -d ~/bin ] ; then
    PATH=~/bin:"${PATH}"
fi

which is located in .bash_profile, doesn't seem to work...it's not being read. But, if you enter this into your terminal:

bash --login

then all your scripts in ~/bin will execute properly for your current terminal session.

A good solution is to cut those 3 lines of code out of .bash_profile and put them in your .bashrc file. If you don't cut (or comment out) the code in .bash_profile it will be read twice since .bash_profile includes the .bashrc file.

I've looked into this a little more and I'm unsure about the specific reasoning behind running bash as a login shell or not (and the reason for these different startup procedures.) I assume it's for security and limiting access a little bit.

In any case, it seems the default (and standard) way to modify your PATH is in .bash_profile as the default .bash_profile file would seem to suggest. If you would like to stick to this way of doing things, don't bother copying the PATH code from the .bash_profile file to the .bashrc file. Just run your X terminal, go to it's preferences and find and check the setting that says something similar to "Run command as login shell" and you'll be good to go.

Evan Carroll (evancarroll) said : #6

Are you doing this through X (gnome) or not?

chika.tambun (chika) said : #7

i just add this to my ~/.bashrc

export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
export PATH=$PATH:$JAVA_HOME/bin
export CATALINA_HOME=/opt/apache-tomcat-5.5.26
export PATH=$PATH:$CATALINA_HOME/bin
export ANT_HOME=/usr/share/ant
export PATH=$PATH:$ANT_HOME/bin

$CATALINA_HOME/bin/startup.sh

then
$ source ~/.bashrc

it works

Matsobane (matsobane-khwinana) said : #8

Hi Guys,

I am new to Linux, I want to set PATH as well.

I have tried Jordan's solution but the system does not allow me to edit "~/.bashrc" file.

I am the only user on the system.

Please help me.

Thanx.

Matsobane
Centurion, Pretoria. South Africa.

George Fragos (anvo) said : #9

Matsobane: If Gnome is your desktop, enter "sudo gedit ~/.bashrc" in terminal followed by your password.

Ryan Kavanagh (ryanakca) said : #10

On Wed, Aug 05, 2009 at 01:09:26PM -0000, George Fragos wrote:
> George Fragos posted a new comment:
> Matsobane: If Gnome is your desktop, enter "sudo gedit ~/.bashrc" in
> terminal followed by your password.

Hi there,
1) Last I heard, you shouldn't use 'sudo' with X apps, use gksudo or kdesudo
with X apps instead, this prevents error messages of the type:
"** WARNING **: Unable to lock ICE authority file: /home/shane/.ICEauthority"
2) You don't need to be root / use sudo to edit ~/.bashrc

Cheers,
--
|_)|_/ Ryan Kavanagh | Gnupg key
| \| \ http://blog.ryanak.ca/ | E95EDDC9

Mtshepana (tshepo-madigage) said : #11

Hi All,

I need to add javaee.home variable to the PATH.

But I'm also new to linux and the content of the ~/.bashrc looks intimidating.

Do i just add the export commands after the last export, or is there as specific position where it needs to be added?

Regards,

Tshepo

sudo gedit /etc/environment

Add it to your path. Mine looks like this:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/ahnfelt/.cabal/bin"

Mtshepana (tshepo-madigage) said : #13

Thanks Joakim,

I tried this, and i got more confusing..

your command displayed:PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/gamtes"

but echo $PATH displayed : echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/user/Sun/GlassFishESBv21/glassfish/bin

And its the "/home/user/Sun/GlassFishESBv21/glassfish/bin
" i want to remove from the PATH as i now have more than one instance of it.

elmagistral (elmagistral) said : #14

Sorry for a newbie question:

How would be the appropriate way to change where a specific command is first looked at? My $PATH looks like usr/local/sbin:/usr/local/bin:usr/sbin:usr/sbin and so on.

If I have for some reason a command twice installed with different versions in /usr/bin and in /usr/local/bin , most time I'm OK with /usr/local/bin having priority over /usr/bin . If for some time I want 'mycommand' to first be looked at /usr/bin , not in /usr/local/bin , what shall I do? An alias? An example would be appreciated.

Bill Leach (wrleach) said : #15

elmagistral: I had that problem years ago on an old DEC Ultrix box. Can't even remember what the command was but I remember that I only needed the "system installed" version rarely. So, while there are always many ways to accomplish the same thing for me the simplest was to just specify the full path when I wanted the system version. For example:
If you had your own special version of "ls" in /usr/local/bin but needed the system installed version you would type: "/bin/ls".

Of course for most commands you would need to type "/usr/bin/<command name>".

If you have write access to "/usr/local/bin" you could just create a script (using "ls" as an example again):
called "sysls":
#! /bin/sh
# Script to run the system version of "ls"
/bin/ls

and don't forget to set the executable bits on the script.