2

I've noticed that when I open a terminal on my ubuntu machine locally, it sources .bashrc, but when I connect via ssh, it sources .bash_profile. I added a line in .bashrc to source .bash_profile so I have both files sources when working locally. I'd like to have the same behavior when accessing the machine remotely. Of course, if I just source .bashrc in .bash_profile I'll have an infinite loop. What's the proper way to set this up?

Prvt_Yadav
  • 5,732
  • 7
  • 32
  • 48
Mei Zhang
  • 177
  • 2
  • 6

1 Answers1

0

You can use command shopt login_shell. If the shell is a non login-shell then it will print login_shell off, and if it is a login-shell then it will print login_shell on.

.bash_profile is sourced by bash whenever it is started in interactive login mode or accessed via ssh. So you can put an if condition in .bash_profile like :

if [ "$(shopt login_shell | cut -f2)" = "on" ]
 then
  source .bashrc
fi

.bashrc is sourced whenever bash is started in terminal, so you can put an if condition in .bashrc

if [ "$(shopt login_shell | cut -f2)" = "off" ]
then
 source .bash_profile
fi
Prvt_Yadav
  • 5,732
  • 7
  • 32
  • 48