Results 1 to 6 of 6

Thread: bashrc sourced twice?

  1. #1

    Question bashrc sourced twice?

    I noticed redundant entries in my $PATH...

    My .bashrc is sourced twice for non-login interactive shells. (once for login shells). Is it an openSUSE decision?

  2. #2
    Join Date
    Aug 2010
    Location
    Chicago suburbs
    Posts
    13,060
    Blog Entries
    3

    Default Re: bashrc sourced twice?

    Possibly, you are confused by what you are seeing.

    Your desktop session startup uses your login shell (presumably bash), and any environment variables set there become available to your entire desktop, including any terminal applications that run a shell. For bash, that includes what is done in ".bashrc".

    When you start a bash shell, as in a terminal application, that again uses ".bashrc". If you don't want that, you can instead use a ".profile" in place of a ".bashrc".

    This is very normal unix and linux behavior. It is not limited to openSUSE. For example, I saw similar behavior with Solaris, though I was using "csh" and ".cshrc" in place of "bash" and ".bashrc".

    It is a common practice to avoid redundant PATH entries by testing the current $PATH before you decide to change anything. Or test some other environment variable that you set as a flag to indicate that you have ALREADY_BEEN_HERE .
    openSUSE Leap 15.1; KDE Plasma 5;
    testing Leap 15.2Alpha

  3. #3

    Default Re: bashrc sourced twice?

    Quote Originally Posted by JAMcInnes View Post
    I noticed redundant entries in my $PATH...

    My .bashrc is sourced twice for non-login interactive shells. (once for login shells). Is it an openSUSE decision?
    Hi,

    Welcome to the forum. Like what said by nickert the PATH should be in

    Code:
    "$HOME/.profile"
    because every time you open a terminal which is a login shell that PATH will multiply/extend/expand or whatever you want to call it.
    You can still use ~/.bashrc but you need to do some test, yes test the value of $PATH and not just add an entry to it.
    Create a ~/script directory
    Code:
    mkdir -p "$HOME/scripts"
    Now put something like this in your ~/.bashrc and save it.

    Code:
    if [[ :$PATH: != *:$HOME/scripts:* ]]; then
      PATH=$HOME/scripts:$PATH
    fi
    You will have a ~/script directory in your "$HOME" and it will be in your PATH, and it will not duplicate the value of PATH regardless how many login shells you have open.
    this can be done in ~/.profile as well. Also there is no decision making done by openSUSE or whoever. That behavior of login/non-login shells remains the same. The decision is still yours...
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  4. #4

    Default Re: bashrc sourced twice?

    Ok thanks. Many years of Linux and I never noticed that till now!

  5. #5
    Join Date
    Sep 2012
    Posts
    5,279

    Default Re: bashrc sourced twice?

    Quote Originally Posted by nrickert View Post
    Your desktop session startup uses your login shell (presumably bash), and any environment variables set there become available to your entire desktop, including any terminal applications that run a shell. For bash, that includes what is done in ".bashrc".
    Which is SUSE specific and rather questionable. .bashrc is expected to be sourced by interactive shell and desktop session startup script is not an interactive shell by any measure. .bashrc is sourced from within /etc/profile and I do not see this being done in e.g. Ubuntu. I would argue that sourcing of .bashrc (and /etc/bash.bashrc) in /etc/profile must at least be protected by check whether shell is interactive.

    P.S. OK, Ubuntu actually is sourcing ~/.bashrc from within default ~/.profile, but it guards against non-interactive shell both in /etc/bash.bashrc and default ~/.bashrc.

  6. #6
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    11,707
    Blog Entries
    2

    Default Re: bashrc sourced twice?

    Quote Originally Posted by arvidjaar View Post
    Which is SUSE specific and rather questionable. .bashrc is expected to be sourced by interactive shell and desktop session startup script is not an interactive shell by any measure. .bashrc is sourced from within /etc/profile and I do not see this being done in e.g. Ubuntu. I would argue that sourcing of .bashrc (and /etc/bash.bashrc) in /etc/profile must at least be protected by check whether shell is interactive.

    P.S. OK, Ubuntu actually is sourcing ~/.bashrc from within default ~/.profile, but it guards against non-interactive shell both in /etc/bash.bashrc and default ~/.bashrc.
    ??

    /etc/profile applies variables to the system, without respect to interactive or non-interactive and regardless of shell.
    I could be wrong but my assumption has always been that the User path as implemented by default applies only to the Bash shell, typically interactively.
    I agree though that although in the SysVinit days the desktop session might have been started by script, today that is not necessarily the case

    TSU
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

Posting Permissions

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