28

I'm setting up a new Linux machine I got from our IT dept, and noticed .profile is not loaded when I start a new terminal session. The current shell is Bash, though I changed it from the default sh it came with. How do I make it load .profile on startup?

I access the shell via SSH: ssh myusername@remotemachine. I have administrator privileges on it.

1
  • Show us your the command you use to start your shell.
    – Nifle
    Aug 16, 2010 at 14:33

2 Answers 2

24

When Bash starts as an interactive login shell, one of the files it may process is ~/.profile.

When it starts as an interactive non-login shell it doesn't. It processes /etc/bash.bashrc (if that file or a similar file is enabled in your version of Bash) and ~/.bashrc.

You could add the following to your ~/.bashrc (but be careful of loops or values being changed inadvertently):

. $HOME/.profile
4
  • 8
    If you're going to have your .bashrc source your .profile (which i don't recommend) you should have some guard against double sourcing. Set some guard variable or so [ -z "$SOME_VAR_SET_IN_PROFILE" ] && . ~/.profile Aug 16, 2010 at 17:29
  • 2
    Yeah, I don't really recommend it either. Aug 16, 2010 at 18:32
  • 17
    A login shell will try ~/.bash_profile, ~/.bash_login and ~/.profile in order and only open the first one it finds.
    – Beano
    Aug 17, 2010 at 8:35
  • 3
    As others have said, this is NOT recommended, since the usual way is for profile and "friends" (.bash_profile) to source .bashrc, and not the other way. It could be that your terminal program is NOT a login shell, but an interactive non-login shell. Often there's a preference you can set -- make it a login shell to get .profile, .login and/or .bash_profile to execute.
    – rholmes
    Oct 10, 2016 at 15:09
19

It kind of depends how you start your shell. As others have said, a login shell will load your profile (it will look for .bash_profile first, then will try .profile). If it finds one of these, it loads them. A non-login shell (either interactive or non-interactive) will source .bashrc.

I'd suggest putting everything into .bashrc. The .profile/.bashrc split was kind of arbitrary and made more sense in the old days of UNIX when tty wasn't just a device name and meant an actual TeleType. It was meant to start certain things (like checking mail) on the 'main' login to a server, and just normal setup stuff for other shells. In most Linuxes you will log in now, you're not really logging into a shell, as you're logging into some graphical interface (KDE, gnome, CDE 'shudder'). The "spawn login processes" is now taken care of by your session manager. It's much less relevant now.

My suggestion: Make your .profile consist of solely:

[ -f $HOME/.bashrc ] && . $HOME/.bashrc

as the first line of .bashrc, guard against weird stuff happening when running a bash script by jumping out early:

[[ $- != *i* ]] && return
4
  • 6
    .profile should be kept bash agnostic. I suggest to configure .bash_profile to load .profile and then load .bashrc. put only bash agnostic stuff in .profile, like PATH and the LC_* stuff. Put the rest in .bashrc.
    – Lesmana
    Jan 17, 2011 at 20:05
  • @Rich Homolka Why is [ -f $HOME/.bashrc ] twice in your command? How is the command you posted different from just '. $HOME/.bashrc' ? Apr 29, 2015 at 17:56
  • 1
    Protip: Don't put anything in your .bashrc that writes to stdout or stderr, as that can break non-interactive clients for things such as SCP. Things with output (for example, I like to be greeted with a fortune cookie and uptime) should only go in .profile / .bash_profile Mar 8, 2016 at 15:07
  • @Lotharyx true. We had a standard-ish kshrc that exported to stdout, broke out Xserver (hummingbird exceed) Mar 8, 2016 at 15:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .