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.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
S182
  • 387
  • 1
  • 3
  • 7
  • 1
    possible duplicate of [What's the best distro/shell-agnostic way to set environment variables?](http://unix.stackexchange.com/questions/88201/whats-the-best-distro-shell-agnostic-way-to-set-environment-variables) – strugee Mar 29 '14 at 23:14
  • 1
    @strugee No, this question isn't about environment variables. – Gilles 'SO- stop being evil' Mar 29 '14 at 23:15
  • @Gilles yes, but your answer applies equally well. – strugee Mar 29 '14 at 23:17
  • 1
    @strugee That's because I went beyond the immediate question and touched on a peripheral topic. One paragraph in one answer doesn't make a duplicate. Questions are only duplicates if they have substantially identical answers, not if one answer happens to contain enough material to answer the second one. – Gilles 'SO- stop being evil' Mar 29 '14 at 23:20

1 Answers1

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?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175