LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   $PATH clean up? (https://www.linuxquestions.org/questions/linux-newbie-8/%24path-clean-up-501853/)

johnpaulodonnell 11-15-2006 10:21 AM

$PATH clean up?
 
Hi.

Running Suse 10.1 on an x86_64 machine. Basically my $PATH variable is cluttered with junk and repeats. I have only made a small number of additions to the variable via export PATH="$PATH:/somepath" in .bashrc. But when I do so and echo $PATH that entry will show up numerous times...here is the tail end of the output from echo $PATH for example:

..........
/usr/local/GMT3.4/bin:/usr/local/sac/bin:/usr/local/ham/ham-2.2.2/src:/usr/local/GMT3.4/bin:/usr/local/sac/bin:
/usr/local/ham/ham-2.2.2/src:/usr/local/GMT3.4/bin:/usr/local/sac/bin:/usr/local/ham/ham-2.2.2/src:
/usr/local/rdseed:/usr/local/GMT3.4/bin:/usr/local/sac/bin:/usr/local/ham/ham-2.2.2/src:/usr/local/rdseed/rdseed4.6:
/usr/local/GMT3.4/bin:/usr/local/sac/bin:/usr/local/ham/ham-2.2.2/src:/usr/local/rdseed/rdseed4.6:
/usr/local/GMT3.4/bin:/usr/local/sac/bin:/usr/local/rdseed/rdseed4.6:/usr/local/GMT3.4/bin:/usr/local/sac/bin:
/usr/local/SH-SHM/sh:/usr/X11/lib:/usr/X11/lib64:/usr/local/ham/ham-2.2.2/src:/usr/local/rdseed/rdseed4.6

Plus other entries that I've subsequently commented out of .bashrc remain - eg
/usr/local/rdseed was a mistake, to be replaced by usr/local/rdseed/rdseed4.6, and yet it remains part of the $PATH variable.

.bashrc is 'source'd after each edit.

Does anyone know why this is happening?

Thanks

pixellany 11-15-2006 10:56 AM

Sometimes startup scripts will modify $PATH. Sometimes, I set out to find out exactly what is happening and usually wind up frustrated---but the answer is out there somewhere in a script.

matthewg42 11-15-2006 02:11 PM

I'm not 100% clear what you mean by this "bashrc is 'source'd after each edit". Do you mean you do something like this
  1. start shell
  2. edit .bashrc, after which there are some 'export PATH="$PATH:something"' lines in it.
  3. source .bashrc
  4. check PATH contents
  5. edit again
  6. source .bashrc again

If you are doing this all in the same shell (i.e. not logging out / re-starting your terminal program/session), then it's clear.

The command
Code:

export PATH="$PATH:more"
sets the PATH to the existing value + ":more". Say your PATH starts out as "mypath:theoriginal"... after one execution of the export command the PATH would be "mypath:theoriginal:more". After another execution it would be "mypath:theoriginal:more:more" etc.

Sourceing the .bashrc simply feeds the contents of that file into the exitsing instance of the shell, so each time youo do it, you are appending more stuff to the PATH.

You should find that when you start a new shell (e.g. open a new terminal window or log out and log back in again), it will have the default PATH as set in the global shell config files (/etc/profile and friends), plus any customisations in your .bashrc).

One thing to note - be careful editing your .bashrc. If you make a typo which causes the shell to exit for any reason, you'll not be able to log in, because the shell will start and immediately quit! To protect against this, when editing it, always keep a window open which you can edit the .bashrc with while opening a new session to test your changes. The .bashrc will be read by the new session, but the existing session will stay unaltered while you test the new file... if you find a problem you can still edit the file in the original session.

The caution applies less to sessions in graphical environments that when you just have console access since the .bashrc probably won't affect a running X-Windows session, and you'll probably be able to launch editors and so on without having to start a shell which reads the [potentially broken] .bashrc.

slakmagik 11-15-2006 02:46 PM

matthewg42 is probably right about the extent of redundancy you're seeing, but you'd probably see some in some cases, regardless.

I export cumulative things, such as paths, from ~/.bash_profile. This is generally executed only on login, so I don't get redundant entries with deeper shell levels. Knome terms stupidly default to 'TERM -ls', though, which would require changing or cancel out the distinction. Another option is to set the path in your .bashrc conditionally, with something like
Code:

if [ ! `echo $PATH|grep 'MYADDITION'` ]; then
      export PATH="$PATH:MYADDITION"
fi

which only appends it if it isn't already there.

matthewg42 11-15-2006 03:58 PM

Good point digiot. I like the term "knome", he he.


All times are GMT -5. The time now is 12:17 AM.