A little email about .bashrc vs .bash_profile:

On Tue, 2003-11-18 at 15:28, Andrew Bennetts wrote:
> The dash makes it a "login shell", which means it executes the bashrcs and
> whatnot specific to the root user, thus you'll get the environment you expect.

Without wanting to be pedantic, I got this wrong enough times to offer a
minor clarification:

A great way to figure out what your system is doing is to put

        echo "In ~andrew/.bashrc" 1>&2

As a line in your .bashrc, and similar lines in places like
.bash_profile, .profile, and if you're really stumped, /etc/profile,
.gnomerc and /etc/X11/gdm/gnomerc (but see warning [1] below!)


Sometimes you'll be surprised. For example, I was once dealing with
Solaris boxes whose default shell wasn't bash. I wanted to use bash, and
so would do

        ssh [EMAIL PROTECTED] bash

[assuming bash was even on default path, <groan>]

The thing that surprised me was that, *I* thought I was "logging in",
but in this case, .bash_profile wasn't run, only .bashrc . This because
running bash as the command via ssh is the same as if you just type
`bash` on a command line - .bashrc gets sourced, but not .bash_profile.
<shrug, whatever, fair enough>.

As you may have noticed, one of bash's options is --login (this is what
relates to the `su -` conversation). It turned out that if I wanted
.bash_profile to run, I needed a "login environment" and so needed to
amend the above command to 

        ssh [EMAIL PROTECTED] 'bash --login'

which was a bit of a pain. [So I wrote a wrapper, but that's another
story ;)]

So this is all to say that, especially with bash as a shell (but I
suspect many others), it pays to

a) put environment setting/creating things, like setting PATH, in
.bashrc

b) put login only things, like checking new mail or changing directory
to ~ or running a clock or ... in .bash_profile;

c) go to the effort to actually *get* a login shell when you want one.

Note: Interesting how the default skeleton .bash_profile always sources
.bashrc for you! That's because if bash IS in login mode, and decides to
source .bash_profile, it DOESN'T source .bashrc automatically as well -
so if you're environment settings are there, then you need to act to get
them.

--

[1] WARNING! Don't forget that since .bashrc gets sourced any time a
shell runs, it REALLY must not output anything on stdout - in other
words, that little debuggy statement I suggested above will wreck your
world if it *doesn't* go to stderr and you try to do `scp`. So don't
forget 1>&2 on any debug lines


Ah, the things we learn by trial and error. Given that this is all open
source and subject to modification without notice, your mileage may
vary. Certain forward looking statements (like "it may work") are not to
be taken as authoritative. And if this message wasn't intended for you,
then destroy it without reading it :)

AfC

-- 
Andrew Frederick Cowie
Operational Dynamics Consulting Pty Ltd

Australia: +61 2 9977 6866

http://www.operationaldynamics.com/
-- 
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug

Reply via email to