What's the difference between .bashrc and .bash_profile and which one should I use?
|
|
||||
|
Traditionally, when you log into a Unix system, the system would start one program for you. That program is a shell, i.e., a program designed to start other programs. It's a command line shell: you start another program by typing its name. The default shell, a Bourne shell, reads commands from Bash is a Bourne-like shell. It reads commands from You can invoke a shell directly at any time, for example by launching a terminal emulator inside a GUI environment. If the shell is not a login shell, it doesn't read Therefore:
On modern unices, there's an added complication related to Note that you may see here and there recommendations to either put environment variable definitions in ¹ For completeness, by request: if |
|||||||||||||||||||||
|
|
From this short article
|
|||||
|
|
Back in the old days, when pseudo tty's weren't pseudo and actually, well, typed, and UNIXes were accessed by modems so slow you could see each letter being printed to your screen, efficiency was paramount. To help efficiency somewhat you had a concept of a main login window and whatever other windows you used to actually work. In your main window, you'd like notifications to any new mail, possibly run some other programs in the background. To support this, shells sourced a file This is a bit of an anachronism now. You don't log into a main shell as much as you log into a gui window manager. There is no main window any different than any other window. My suggestion - don't worry about this difference, it's based on an older style of using unix. Eliminate the difference in your files. The entire contents of .bash_profile should be:
And put everything you actually want to set in .bashrc Remember that .bashrc is sourced for all shells, interactive and non-interactive. You can short circuit the sourcing for non-interactive shells by putting this code near the top of .bashrc:
|
|||||||||||||||||||||
|
|
Have a look at this excellent blog post by ShreevatsaR. Here's an extract, but go to the blog post, it includes an explanation for terms like "login shell", a flow chart, and a similar table for Zsh.
|
|||||||||||||
|
|
A BETTER COMMENT FOR THE HEAD OF /ETC/PROFILE Building on Flimm's great answer above, I insered this new comment at the head of my Debian /etc/profile, (you might need to adjust it for your distro.):
And this note at the head of each of the other setup files to refer to it:
Worth noting I think is that Debian's /etc/profile by default sources (includes) /etc/bash.bashrc (that's when /etc/bash.bashrc exists). So login scripts read both /etc files, while non-login reads only bash.bashrc. Also of note is that /etc/bash.bashrc is set to do nothing when it's not run interactively. So these two files are only for interactive scripts. |
|||
|
|
.profile, have a look at this question: superuser.com/questions/789448/… – Flimm Jul 13 '16 at 8:53