I have the following in my .bashrc file (Note I installed atlas manually so duplicates of atlas in the variables are not from a package):
ADD="/usr/lib64/mpi/gcc/openmpi/lib64/openmpi:/usr/local/lib64/atlas"
if ! -z "${ADD}" ]
then
if -z "$LIBRARY_PATH" ]
then
export LIBRARY_PATH="${ADD}"
else
export LIBRARY_PATH="${LIBRARY_PATH}:${ADD}"
fi
fi
Then in Konsole (in KDE) when I open a terminal I get the following:
This indicates that my additions have been added twice. However when I go to a login terminal with CTRL+ALT+F1 I find that these variables are not executed twice. So it seems that in KDE or KONSOLE or something, is running my .bashrc file twice and thus adopting my settings multiple times. Can someone explain what is happening and tell me how I can fix this…
I have had bad experiences when environment variables are not set correctly so I like it when everything is perfect.
While wrong environment variables can be bad, it depends on what they are used for. And as LIBRAY_PATH is a list of libraries to search in, there can’t be no problem here, except that the same library might be searched twice when the search item is not in there.
And for the script part. I assume it is always best to check first if the entry is already in the list. Simple good programing practise.
So when I login from ssh the environment variables are not added twice. So it seems that KDE is running '.bashrc' then later KONSOLE is inheriting these variables... Can someone confirm/explain...
Henk van Velden:
Yes thanks for the advice. I agree for the specific example it might not be so much a problem... but I do this for several other variables and I know the order does matter... so I would like to know. I will have to make smarter BASH code to get what I want... but it is a pain that I have to. I think it is just better that the system is set-up so '.bashrc' is only ran once... or when KONSOLE starts a new shell it does not inherit the environment variables (thus simulating a login shell).
In case you are interested I have a solution. In bashrc I define the following function:
function ADD_PATH
{
LIST="${1//:/ }"
ADD="${2//:/ }"
UNIQ_ADD=""
for F in $ADD
do
GOOD=1
for G in $LIST
do
if "${F}" == "${G}" ]
then
GOOD=0
fi
done
if $GOOD == "1" ]
then
if -z "$UNIQ_ADD" ]
then
UNIQ_ADD="${F}"
else
UNIQ_ADD="${UNIQ_ADD}:${F}"
fi
fi
done
if -z "$UNIQ_ADD" ]
then
RETVAL="$1"
else
RETVAL="${1}:${UNIQ_ADD}"
fi
}
Then I modify the code as follows:
# This controls where GCC looks for static libraries when linking
ADD="/usr/lib64/mpi/gcc/openmpi/lib64/openmpi:/usr/local/lib64/atlas"
if ! -z "${ADD}" ]
then
if -z "$LIBRARY_PATH" ]
then
export LIBRARY_PATH="${ADD}"
else
ADD_PATH "${LIBRARY_PATH}" "${ADD}"
export LIBRARY_PATH="$RETVAL"
fi
fi
Then since I do not want to pollute my environment too much I delete my function when I no longer need it anymore:
unset -f ADD_PATH
This seems to work well… Again I would appreciate if someone could explain where/when/why .bashrc seems to be run twice…
I’d bet your KDE login is setting the variables once, so anything you do
from that point will have the variables set a second time if .bashrc is
sourced as part of opening the shell (as it should be and apparently is).
As a result, I’ll bet this has less to do with running twice at shell
load time, and more to do with running twice because it ran once before
your environment loaded at all.
Try running xterm (Alt+F2, then xterm) since that is not KDE-specific and
see if the problem persists, as I presume it will. Alternatively, while
in KDE go and comment out the line in your ~/.bashrc file and, without
exiting KDE, spawn a new shell. Is it still set once? In that case it
also would seem to prove my point since it is not being set at all when
the new shell loads, but it is still set somehow and inherited from the GUI…
–
Good luck.
If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below…
Thank-you for your posts and insights... So I did a series of experiments (as suggested) and I think it is clear that KDE runs .bashrc when it starts... then everything else just adopts these environment variables. So if you want unique additions you need to check if variables are already there. I don't think it is all that important in most cases... but if you are a little bit of a perfectionist/OCD/pedantic like me see the solution above... :)
Need to understand inheritance. The parents environment is inherited buy all it’s child processes. But if a child changes the environment it does not effect the parent process only children of the child. Also multiple setting a variable does no harm only change the value stored. In any name space you can only have one variable of a given name.