JAVA_HOME environment variable showing twice

I set up my JAVA_HOME environment variable in /etc/bash.bashrc.local

JAVA_HOME=/usr/lib64/jvm/java-1.6.0-openjdk
export PATH=$PATH:$JAVA_HOME/bin

echo $PATH
/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games:/usr/lib64/jvm/java-1.6.0-openjdk/bin:/usr/lib64/jvm/java-1.6.0-openjdk/bin

However, when I call $PATH I see it displayed twice. How do I get rid of this duplication and only show it once in PATH?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The JAVA_HOME path that you specified was already in the PATH variable it
would seem… there’s no reason to concatenate it in there again. Having
duplicates doesn’t hurt things really.

Good luck.

On 04/15/2011 08:36 PM, imterpsfan2 wrote:
>
> I set up my JAVA_HOME environment variable in /etc/bash.bashrc.local
>
> JAVA_HOME=/usr/lib64/jvm/java-1.6.0-openjdk
> export PATH=$PATH:$JAVA_HOME/bin
>
> echo $PATH
> /sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games:/usr/lib64/jvm/java-1.6.0-openjdk/bin:/usr/lib64/jvm/java-1.6.0-openjdk/bin
>
> However, when I call $PATH I see it displayed twice. How do I get rid
> of this duplication and only show it once in PATH?
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJNqRYkAAoJEF+XTK08PnB5O0cQAIsb4GfD88BKIT3CXud2UyRj
dOEcjLQRWQShXWdRw8ZTTJIcGClkA77ydnGu+HWIXUnv9pLapEpkGspDx4S0/Tph
qgG/1Y/0pX6sEr+j701v9fiQnJk/fNSv/EZcTLcSWIr+XUJek/EjKN3nO0wJlL33
97uuHZ6IkQ8xfidXq/VKxZS/r0vY+dkumU7HzY9Ik3EMgHiSsbjT7O8FEXo3g709
ccGvkmhO3Zs+y7CjxjIUQzM+UOwZ8/LLUH6wZ1cGeJJB+k7U2OMqN0rZ1WQK/DUf
PifFhLABcybZ/6E7sUt3lwSAPv09+0A0qE+KB5Nd/i2hNg1TeC8nKqHE0nrqzk1i
U+8y1z2oDhivt1K5v4jUT1190GzjsdBdK/0RHknNHQVe7ncorH/gnzBRrIZAzO3t
aUB2W4x+/E24IKSxb/wCxNOQUEPIWaUQSR6YlvIE8NxdKpdELTdlHjudxOjxMEXD
XVNmJoFSzOYZhjxLY4tmZxabJ4/R9mWEX4fEVeSHZTPaM/zLddnFxguJDszhF5MY
EHbOMF8S6CczdCIADz5VpyagcwdGtNvSwFAyoWA6kJeKWxYoHh1WuvE1OO3mLgQA
mxLX65M2VpsGjoxa0TKyz5I4i42vBCJzPGsTs8DROWksRqRyzjgb2iYsh0QoGX7b
CL9PB/o/2MJ0CA5uI2A/
=2GRy
-----END PGP SIGNATURE-----

bash.bashrc.local is probably being called twice - once when starting your desktop, and then again when you start a shell. So the particular path element is being added twice.

Instead of:


export PATH=$PATH:$JAVA_HOME/bin

try:


case "$PATH" in
  *java*) ;; ## nothing - already in path
  *) export PATH=$PATH:$JAVA_HOME/bin ;; ## add to path
esac

Note: I did not test this.

Thanks nrickert, this was able to point me in the right direction. I modified your syntax to add multiple paths. Your solution really worked for me. You’re right, it seems that bashrc.local gets called when you login as well as when you open a terminal session.

JAVA_HOME=/usr/lib64/jvm/java-1.6.0-openjdk
JRUBY_HOME=/home/charles/netbeans-6.7.1/ruby2/jruby-1.2.0
case “$PATH” in
java);; ## java already in path – don’t add it
*) export PATH=$PATH:$JAVA_HOME/bin;; ## add to path
esac
case “$PATH” in
jruby);; ## jruby already in path – don’t add it
*) export PATH=$PATH:$JRUBY_HOME/bin;; ## add jruby to path
esac

imterpsfan2 wrote:
> I set up my JAVA_HOME environment variable in /etc/bash.bashrc.local
>
> JAVA_HOME=/usr/lib64/jvm/java-1.6.0-openjdk
> export PATH=$PATH:$JAVA_HOME/bin
>
> echo $PATH
> /sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games:/usr/lib64/jvm/java-1.6.0-openjdk/bin:/usr/lib64/jvm/java-1.6.0-openjdk/bin
>
> However, when I call $PATH I see it displayed twice. How do I get rid
> of this duplication and only show it once in PATH?

Nothing to do with your question, but that’s a lousy set of paths!

The first three are meant to be restricted to root (or other privileged
user). They’re intended be used only in an environment that does NOT
include other paths that may have weak security.

The root user should have a strictly limited environment, and other
users shouldn’t see the commands in the sbin directories, or root’s
private bin come to that.

Of course, it’s up to you haow you set up your machine …

On 04/18/2011 12:07 PM, Dave Howorth wrote:
> Nothing to do with your question, but that’s a lousy set of paths!
>
> The first three are meant to be restricted to root

maybe that is root’s pass…
perhaps he is logged in as root :wink:

many do, you know(?)

i mean, they did/do it all the time in their other dual boot system, so
why not here they ask…and, listen to the answer and follow advice, or
not…


CAVEAT: http://is.gd/bpoMD
[openSUSE 11.3 + KDE4.5.5 + Thunderbird3.1.8 via NNTP]
A Penguin Being Tickled - http://www.youtube.com/watch?v=0GILA0rrR6w

DenverD wrote:
> On 04/18/2011 12:07 PM, Dave Howorth wrote:
>> Nothing to do with your question, but that’s a lousy set of paths!
>>
>> The first three are meant to be restricted to root
>
> maybe that is root’s pass…
> perhaps he is logged in as root :wink:
>
> many do, you know(?)
>
> i mean, they did/do it all the time in their other dual boot system, so
> why not here they ask…and, listen to the answer and follow advice, or
> not…
>

Yes, it could be root’s path but then it shouldn’t include /usr/local/*
or /usr/games or …

On 04/18/2011 12:27 PM, Dave Howorth wrote:

> Yes, it could be root’s path but then it shouldn’t include /usr/local/*
> or /usr/games or …

ah, but there is more than one way to skin a cat and more than one
openSUSE image being distributed…my 11.3 KDE from a Linux Magazine
(october 2010) DVD and my root path this moment is:

openSUSE11-3:~ # echo $PATH
/usr/lib/mpi/gcc/openmpi/bin:/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games:/usr/lib/jvm/jre/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin

and, i can promise you that i didn’t add /usr/local /usr/games or …

i wouldn’t know how to get the mag’s dvd builder (in the UK) to stop
messin’ with the man!


CAVEAT: http://is.gd/bpoMD
[openSUSE 11.3 + KDE4.5.5 + Thunderbird3.1.8 via NNTP]
A Penguin Being Tickled - http://www.youtube.com/watch?v=0GILA0rrR6w

Any Linux/Unix OS that depends on those directories not being in a user’s path for security has broken programs. Having /sbin:/usr/sbin:/usr/local/sbin in a user’s $PATH may trip up the user, but it should not cause any breach of security. It’s up to each program to check what the uid of the invoker is and do the right thing. There are some programs in there that are useful to non-root users, e.g. ifconfig, route, modinfo, etc. None of those will allow privileged operations if you are not root.

I do not however have them on my $PATH. I create symlinks to the useful programs from ~/bin. There are however well-known distros that do put these directories in users’ $PATH and don’t suffer security problems on that account.

  1. The reason you saw root in the $PATH because I was logged in as root at the time.

  2. No, JAVA_HOME wasn’t already in the $PATH. The reason why it was duplicated was because as the first respondent mentioned…bash.bashrc.local gets called twice, once when you login and another time when you open the console.

Hi,

I may be wrong, in which case I’m sure someone will correct me, but I
think if you put your custom $PATH in ~/.bash_profile it will only be
called once.

This seems to be the case on my laptop.


Regards,
Barry D. Nichols

It depends on how you assigned to $PATH. If you did this:

export PATH=‘blah:blah:blah’

then there will be no duplication. But if you did an append:

export PATH="$PATH:blah"

then if this gets executed twice, you will see it twice in $PATH.

If I just used export PATH:etc wouldn’t it just write over the existing path? By appending I am not overwriting that’s in bash.bashrc. The reason I use bash.bashrc.local is because I found this message in bash.bashrc

PLEASE DO NOT CHANGE /etc/bash.bashrc There are chances that your changes

will be lost during system upgrades. Instead use /etc/bash.bashrc.local

for your local settings.

I’m assuming I’m getting the first part of my PATH from this script.

I come from the Windows world so I don’t consider myself an expert on all this. I just can say that now I see no duplication in my PATH.

Appending to PATH, as you are doing, is correct. Putting that in the “bash.bashrc.local” file should be fine.

It is just the way that linux (and unix) works, that the script is effectively being run twice.

If the PATH were defined in “~/.bash_profile”, as one person suggested, that would not have helped.

The bash startup scripts are run at the start of your GUI session, because any environment definitions should apply to every process and not just to shell processes. And the startup scripts are run when starting a shell because you might have other shell settings that should apply to that particular shell.

Hi,

As I understand it the ~/.bash_profile is only executed when you log in, whereas the ~/.bashrc is executed with every bash instance; therefore placing something in the ~/.bash_profile would at least reduce the number of times it is called, and in the case of users who login once and then use a terminal window the ~/.bash_profile would be executed only once.

Am I missing something?


Regards,
Barry D. Nichols

You would have to test to see what actually happens. The GUI is started by a script, and that script can source your shell startup files.

This is a case for experimental investigation, rather that theoretical hypothesizing.