3

I see many people do the opposite. Are there any reasons why it is a bad idea to source ~/.profile from ~/.bashrc?

# .bashrc

if [ -f ~/.profile ]; then
    source ~/.profile
fi

I've done a lot of searching and usually people do the following:

# .bash_profile

if [ -f ~/.profile ]; then
    source ~/.profile
fi

if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

I need some env variables in ~/.profile to be defined for an interactive shell that is not a login shell (thus I'm using ~/.bashrc). Is this opening me to any dangers?

terdon
  • 100,812
user34295
  • 141
  • 1
  • 3

1 Answers1

4

Without knowing your .bashrc and your .profile, we can't say. For example, the default .profile on Ubuntu sources .bashrc. Now if you source .profile from .bashrc, you get an infinite loop:

$ bash  
zsh: segmentation fault (core dumped)  bash

(I use zsh, that's why zsh reports the segfault.)

You could place guards that ensure these files get sourced at most once, but who knows what else may interact weirdly?

For your environment variables problem, it depends on how you're starting this interactive non-login shell. Try .pam_environment instead.

muru
  • 197,895
  • 55
  • 485
  • 740