Results 1 to 8 of 8

Thread: .bashrc runs twice?

  1. #1
    Join Date
    Jun 2009
    Location
    Canada or Denmark
    Posts
    102

    Default .bashrc runs twice?

    Hello folks...

    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):

    Code:
    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:

    Code:
    %> echo $LIBRARY_PATH
    /usr/lib64/mpi/gcc/openmpi/lib64/openmpi:/usr/local/lib64/atlas:/usr/lib64/mpi/gcc/openmpi/lib64/openmpi:/usr/local/lib64/atlas
    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.

    Thanks for you help!

  2. #2
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,378

    Default Re: .bashrc runs twice?

    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.
    Henk van Velden

  3. #3
    Join Date
    Jun 2009
    Location
    Canada or Denmark
    Posts
    102

    Default Re: .bashrc runs twice?

    Update:

    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).

    Take Care
    Mike

  4. #4
    Join Date
    Jun 2009
    Location
    Canada or Denmark
    Posts
    102

    Default Re: .bashrc runs twice?

    Hello All

    In case you are interested I have a solution. In bashrc I define the following function:

    Code:
    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:

    Code:
    # 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:

    Code:
    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...

    Thanks

  5. #5

    Default Re: .bashrc runs twice?

    Wildly guessing:

    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...

  6. #6
    Join Date
    Aug 2010
    Location
    Chicago suburbs
    Posts
    12,873
    Blog Entries
    3

    Default Re: .bashrc runs twice?

    Quote Originally Posted by pilotmm View Post
    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...
    Yes, it is normal for X session startup to run your shell startup scripts. And then when you start a terminal session, they are run again.

    So you need your script to check whether the LIBRARY_PATH has already been set the way that you want.
    openSUSE Leap 15.1; KDE Plasma 5;
    testing Leap 15.2Alpha

  7. #7
    Join Date
    Jun 2009
    Location
    Canada or Denmark
    Posts
    102

    Default Re: .bashrc runs twice?

    Hello all

    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...

    Take Care
    Mike

  8. #8
    Join Date
    Nov 2009
    Location
    West Virginia Sector 13
    Posts
    15,768

    Default Re: .bashrc runs twice?

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •