Gentoo Archives: gentoo-user

From: Alan McKinnon <alan.mckinnon@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Updating $PATH variable permanently for root not working
Date: Sun, 05 Jan 2014 21:14:32
Message-Id: 52C9CB28.1020708@gmail.com
In Reply to: [gentoo-user] Updating $PATH variable permanently for root not working by Tanstaafl
1 On 05/01/2014 17:28, Tanstaafl wrote:
2 > Not sure what I'm missing...
3 >
4 > I login as normal user, then su - to root...
5 >
6 > I've created /root/.bashrc, and added the following:
7 >
8 > export PATH="${PATH}:/path/I/want/to/add"
9 >
10 > If I logout, then su - back into root, shouldn't I see the new path?
11 >
12 > Manually exporting it during the session works, so obviously I'm missing
13 > something...
14 >
15 >
16 >
17
18
19 You are running into that crazy world called "shell start-up scripts"
20 where nothing is as it seems and every install has different levels of
21 crazy.
22
23 run "man bash" and search for the section called "INVOCATION". Once you
24 wade through that byzantine mess, it may be apparent that ~/.bashrc is
25 only read when you launch an interactive non-login shell. But "su -"
26 starts a login shell, so .bashrc is never sourced.
27
28 Prove this by running "su" without the dash, your PATH should work then.
29
30 The usual solution is to source .bashrc from .bash_profile, this has the
31 effect that .bashrc is always sourced regardless of the kind of shell
32 you start.
33
34 Clear as mud right?
35
36 tl;dr
37
38 Long explanation:
39
40 long long ago in a land and time far far away, we have but
41 TheOneTrueShell(tm) and it's name was "sh". This shell was very simple
42 and it's start up scripts followed a grand Unix tradition:
43
44 /etc/profile contained the global settings for all users
45 ~/.profile contained a user's specific settings
46
47 And so the world was good. Until bash.
48
49 The authors of bash figured they should provide extra and wonderful ways
50 of providing bash-specific startup scripts. You'd think they'd do it so
51 the user could put their personal stuff for all shells into ~/.profile
52 and bash stuff into ~/.bash_profile.
53
54 But no, nothing so simple. Bash first looks for ~/.bash_profile and if
55 it finds it, it ignores ~/.profile entirely. So people would just add
56 ". ~/.profile" to .bash_profile and be done with it.
57
58 Then there's a distinction between a login shell (what you get at login,
59 or with su -) and a non-login shell (what you get with su or usually
60 with konsole, gnome-terminal etc). Why this difference exists, I do not
61 know. Maybe it's to save 512 precious bytes of environment memory.
62
63 A non-login shell reads only ~/.bashrc, presumably because the shell was
64 launched for something else (an Xsession, or a running shell) that had a
65 full environment set up already and there was no need to repeat it.
66
67 Head spinning yet? Just be done with all that nonsense and do this:
68
69 Put this line as the only non-comment line in .bash_profile
70
71 [[ -f ~/.bashrc ]] && . ~/.bashrc
72
73 and put all your shell start-up stuff in .bashrc (moving them out of
74 .bash_profile if necessary
75
76
77 This way everything is still unbelievably complex but at least the
78 visible problems mostly just go away
79
80
81 --
82 Alan McKinnon
83 alan.mckinnon@×××××.com

Replies