Tags: bash, bash_profile, bashrc, files, linux, programming, unix
Difference between .bashrc and .bash_profile?
On Programmer » Unix & Linux
7,717 words with 9 Comments; publish: Wed, 30 Apr 2008 13:12:00 GMT; (20046.88, « »)
What's the difference between the two bash start files ?
Viet
http://unix-linux.itags.org/q_unix-linux-programming_81161.html
All Comments
Leave a comment...
- 9 Comments

- <.bash_profile> contains a list of commands to be executed when you log
in and
<bashrc> contains a list of commands to be executed every time you open
a new shell.
There is a slight difference between them: <.bash_profile> is read once
at the beginning of a session, whereas <.bashrc> is read every time you
open a new terminal (e.g. a new xterm window). In a traditional setup
you would define variables like PATH in <.bash_profile>, and things
like aliases and functions in <.bashrc>. But since <.bash_profile>
usually is pre-configured to read the content of <.bashrc> anyway, you
might as well save some effort and put all your configuration into
<.bashrc>.
#1; Wed, 30 Apr 2008 13:13:00 GMT

- On 2005-01-28, Viet Nguyen <downup.unix-linux.itags.org.turtle.com> wrote:
> What's the difference between the two bash start files ?
man bash
/INVOCATION
nb
#2; Wed, 30 Apr 2008 13:14:00 GMT

- Alex Zarutin wrote:
> <.bash_profile> contains a list of commands to be executed when you log
> in and
> <bashrc> contains a list of commands to be executed every time you open
> a new shell.
> There is a slight difference between them: <.bash_profile> is read once
> at the beginning of a session, whereas <.bashrc> is read every time you
> open a new terminal (e.g. a new xterm window). In a traditional setup
> you would define variables like PATH in <.bash_profile>, and things
> like aliases and functions in <.bashrc>. But since <.bash_profile>
> usually is pre-configured to read the content of <.bashrc> anyway, you
You mean a login shell is a shell, too, so starts with .bashrc,
and then continues with .login.
> might as well save some effort and put all your configuration into
> <.bashrc>.
>
This is less efficient, as environment that is inherited is redefined.
And expressions like PATH=/new/path:${PATH}
get a /new/path added with every instance of nested bash'es.
Michael Tosch .unix-linux.itags.org. hp : com
#3; Wed, 30 Apr 2008 13:15:00 GMT

- In comp.os.linux.misc Viet Nguyen <downup.unix-linux.itags.org.turtle.com> wrote:
> What's the difference between the two bash start files ?
There are three. No! Four. Uh, five.
I give up. What's the difference between your two hands?
Peter
#4; Wed, 30 Apr 2008 13:16:00 GMT

- Peter T. Breuer wrote:
> In comp.os.linux.misc Viet Nguyen <downup.unix-linux.itags.org.turtle.com> wrote:
> There are three. No! Four. Uh, five.
> I give up. What's the difference between your two hands?
It was a perfectly clear and sensible question.
What is the difference in function between .bashrc and .bash_profile?
What should one put in the first, and what in the second?
Timothy Murphy
e-mail (<80k only): tim /at/ birdsnest.maths.tcd.ie
tel: +353-86-2336090, +353-1-2842366
s-mail: School of Mathematics, Trinity College, Dublin 2, Ireland
#5; Wed, 30 Apr 2008 13:17:00 GMT

- downup.unix-linux.itags.org.turtle.com (Viet Nguyen) wrote:
>What's the difference between the two bash start files ?
(Read the man page for bash, under the section on INVOCATION,
or none of this makes half as much sense as it should.)
1) The profile file (which ever of the three files
is used) is read only by login shells.
It should have things that initialize the terminal or otherwise
need be done one time only when a user does a login. Typically,
initializing a terminal is slow. Hence one common example might
be "stty ^H erase". Another example is "PATH=~/bin:$PATH",
which is quick and easy, but if done for every subshell adds
unnecessary search elements to the PATH variable if subshells
are layers deep.
2) The rc file is read by every interactive subshell
(and not by login shells).
It should have things that are needed by for interactive use,
but are excess baggage for non-interactive subshells. Typically
that would be the definitions of aliases.
3) $ENV or $/BASH/_ENV specifies a file read by
non-interactive subshells, i.e., scripts.
This file can have functions, aliases, or variables that are
unneeded in an interactive shell.
The basic idea, which made obvious sense back when the
functionality was first added to /bash/, is to speed up shell
initialization by compartmentalizing it. When a 10Mb disk was
the norm, and it was *SLOW*, the time spent slowly reading in a
large _~/.profile_ for every single shell invocation, the way
/sh/ did, was significant and a very noticeable bottleneck.
Today of course not only is the disk file many many times faster
itself, but we have the kernel cashing oft used disk files and
it can be assumed that for a typical /bash/ invocation all of
the init files have already been cached in RAM, and no disk
activity is even done.
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd.unix-linux.itags.org.barrow.com
#6; Wed, 30 Apr 2008 13:18:00 GMT

- 2005-01-28, 23:14(+01), Viet Nguyen:
> What's the difference between the two bash start files ?
[...]
That's unclear what was the initial intention of bash author
about those files. You may have a look at this thread on the
bug-bash mailing list:
http://lists.gnu.org/archive/html/b...1/msg00194.html
For a more consistent start-up file policy, look at the zsh
shell.
Stphane
#7; Wed, 30 Apr 2008 13:19:00 GMT

- * Michael Tosch (2005-01-28 22:32 +0100)
> Alex Zarutin wrote:
> You mean a login shell is a shell, too, so starts with .bashrc,
> and then continues with .login.
No, first .bash_profile is executed and then .bashrc (but only if it
is sourced in .bash_profile). That's one of the bash insanities noone
really understands. As SC uses to say: "it's fixed in zsh".
> This is less efficient, as environment that is inherited is redefined.
> And expressions like PATH=/new/path:${PATH}
> get a /new/path added with every instance of nested bash'es.
That's correct (but again a bash nuisance). Put all your stuff in
.bashrc and only those things only needed in a login shell in to
.bash_profile. My .bash_profile contains only "source ~/.bashrc".
Thorsten
#8; Wed, 30 Apr 2008 13:20:00 GMT

- Viet Nguyen wrote:
> What's the difference between the two bash start files ?
root:# cat .bashrc
# Sample .bashrc for SuSE Linux
# Copyright (c) SuSE GmbH Nuernberg
# There are 3 different types of shells in bash: the login shell, normal she
ll
# and interactive shell. Login shells read ~/.profile and interactive shells
# read ~/.bashrc; in our setup, /etc/profile sources ~/.bashrc - thus all
# settings made here will also take effect in a login shell.
#
# NOTE: It is recommended to make language settings in ~/.profile rather tha
n
# here, since multilingual X sessions would not work properly if LANG is ove
r-
# ridden in every subshell.
--
<< http://michaeljtobler.homelinux.com/ >>
Exceptions prove the rule, and wreck the budget. - Miller
#9; Wed, 30 Apr 2008 13:21:00 GMT