10

As far as I know, the interactive shells may be login or not login, and the start up files for them are different.

  • If interactive + login shell → /etc/profile then the first readable of ~/.bash_profile, ~/.bash_login, and ~/.profile
  • If interactive + non-login shell → /etc/bash.bashrc then ~/.bashrc

I want to set some variables every time I use an interactive shell regardless whether it is a login shell or not.

4

1 Answer 1

10

No, there isn't. Yes, this is a design defect.

Use the following content in ~/.bash_profile:

if [ -e ~/.profile ]; then . ~/.profile; fi
if [[ -e ~/.bashrc && $- = *i* ]]; then . ~/.bashrc; fi

Beware that bash has an even weirder quirk: when it is a non-interactive login shell and the parent process is rshd or sshd, bash sources ~/.bashrc (but not ~/.bash_profile or ~/.profile). So you might want to put this at the top of your .bashrc:

if [[ $- != *i* ]]; then return; fi

See also Difference between .bashrc and .bash_profile and Difference between Login Shell and Non-Login Shell?

4
  • Those quirks are for security reasons (don't load unexpected files when running in a security-sensitive setting).
    – vonbrand
    Mar 30, 2014 at 1:41
  • 2
    @vonbrand Er, what? No, these quirks have absolutely nothing to do with security. There are no “unexpected files” involved (except that loading .bashrc in a non-interactive setting actually may well count as unexpected), nor any check of any security-sensitive context. Mar 30, 2014 at 1:50
  • Has this a method to prevent loops?
    – Braiam
    Mar 30, 2014 at 2:06
  • 1
    @Braiam What loops? Mar 30, 2014 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.