Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 everytime I use an interactive shell regardless if it is a login shell or not.

share|improve this question
@strugee No, this question isn't about environment variables. – Gilles Mar 29 '14 at 23:15
@Gilles yes, but your answer applies equally well. – strugee Mar 29 '14 at 23:17
@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 Mar 29 '14 at 23:20
up vote 7 down vote accepted

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?

share|improve this answer
Those quirks are for security reasons (don't load unexpected files when running in a security-sensitive setting). – vonbrand Mar 30 '14 at 1:41
@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. – Gilles Mar 30 '14 at 1:50
Has this a method to prevent loops? – Braiam Mar 30 '14 at 2:06
@Braiam What loops? – Gilles Mar 30 '14 at 2:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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