Gentoo Archives: gentoo-user

From: Willie Wong <wwong@×××××××××.EDU>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Bash prompt
Date: Wed, 14 Sep 2005 12:41:57
Message-Id: 20050914123732.GA29176@princeton.edu
In Reply to: Re: [gentoo-user] Bash prompt by Holly Bostick
1 On Wed, Sep 14, 2005 at 01:25:00PM +0200, Holly Bostick wrote:
2 > Charles Trois schreef:
3 > > Hello!
4 > >
5 > > I am getting confused with profile, bashrc, etc. The prompt string I
6 > > want to use is
7 > >
8 > > PS1="[\u@\h \W]\$ "
9 > >
10 > > I therefore wrote it in /etc/profile (at two levels, root and
11 > > non-root), ~/.bash_profile and ~/.bashrc.
12 > >
13 > > If I log in as a plain user (moi), I get this:
14 > >
15 > > [moi@sirrah moi]$
16 > >
17 > > which is all right. But, if I log in as root, I get the basic default
18 > >
19 > >
20 > >
21 > >
22 > >
23 > > bash-2.05b# .
24 > >
25 > > I thought that /etc/profile should provide the default, but I was
26 > > obviously wrong. Trying to mend things, I created two files
27 > > /root/.bash_profile and /root/.bashrc, writing just PS1 in each. Now,
28 > > logging in as root, the result is
29 > >
30 > > [root@sirrah root]$
31 > >
32 > > which is wrong, since "$" appears in place of "#", as though my
33 > > syntax of PS1 were incorrect, but I don't see that it is.
34 >
35 > No, it's not incorrect, but if you wrote the exact same PS1 in the root
36 > entry as in the user entry, perhaps you see that there's a 'literal'
37 > dollar sign character at the end:
38 >
39 > PS1="[\u@\h \W]\$ "
40 >
41 > which is going to be printed as itself, as you are not using any code to
42 > change it to the 'correct' character based on user (perhaps bash thinks
43 > you've escaped the closing bracket, not the following "$". A space
44 > between the bracket and the "\$" might solve this, but I've never really
45 > got /$ to work properly. It may, however, be because of the 'login shell
46 > issue' -- see below, but basically, if you're using su and not su -, your
47 > UID is not changing, so the /$ is not changing either).
48
49 No. That's wrong.
50
51 [08:13 AM]wwong ~ $ id
52 uid=1001(wwong) gid=0(root) groups=0(root),6(disk),10(wheel),11(floppy),16(cron),18(audio),19(cdrom),27(video),35(games),245(locate),250(portage),408(web),440(speech),443(slocate)
53 [08:13 AM]wwong ~ $ su
54 Password:
55 sep wwong # id
56 uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),11(floppy),20(dialout),26(tape),27(video)
57 sep wwong #
58
59
60 <snip>
61
62 > So ~/.bashrc is king of the hill, as it is apparently meant to be, so
63 > we'll talk about that. As I said, the default ~/.bashrc contained a
64 > number of aliases, and because I use a lot of aliases, I worked with
65 > ~/.bashrc a lot (for both home and root). Using a lot of links (posted
66 > below) I was able to get a nice 3-line prompt that I like. I then copied
67 > the prompt to root's bashrc and made minor changes (the colors, mostly).
68 > and that was fine.
69 >
70 > Then I tackled the problem of the login shell.
71 >
72 > You see, ~/.bashrc is only sourced when you login (to the terminal,
73 > which, don't forget, is a virtual console). So if you're in the term as
74 > a user, and then you su normally to root, root's bashrc is not going to
75 > be sourced, and (among other issues) you're not going to get the right
76 > prompt (seemingly because your UID doesn't actively change to UID 0, but
77 > rather you remain the regular user with elevated permissions).
78
79 1. Your UID does change. See the experiment above.
80 2. .bashrc is sourced for ALL interactive shells. .bash_profile is
81 sourced ONLY for login shells. So: opening an xterm sources .bashrc.
82 Logging in remotely via ssh sources .bash_profile.
83 3. su opens an interactive shell. It DOES source .bashrc
84
85 > The solution to this is (for me, as I really like bash but am not all
86 > that good with it as yet, so any more 'elegant' solutions are unknown to
87 > me), is to make sure my su is always a login prompt (alias su="su -"),
88 > so that root's bashrc is sourced when I su, so I can use root's aliases,
89
90 4. "su -" would open a login shell, and thus source .bash_profile
91 instead of .bashrc
92
93 > and get root's $PATH, since there's nothing more annoying to me than
94 > su-ing to root and still getting a 'file not found' error because
95 > whatever I'm trying to do is still not in my $PATH, because root's PATH
96 > was somehow not exported by su-ing. I also added an ENV_SUPATH variable
97
98 I would say something is borked on your setup. Be default, su would
99 look in /etc/login.def for ENV_SUPATH and ENV_PATH for default PATHs
100 for superusers and users respectively. Here there's also a difference
101 between "su" and "su -". "su" loads ENV_SUPATH from /etc/login.def,
102 which by default is /sbin;/bin;/usr/sbin;/usr/bin. For "su -". because
103 it invokes a login shell, it should grab the path from /etc/profile,
104 which in turn grabs it from /etc/profile.env, which is created by
105 env-update from the information in /etc/env.d
106
107 --
108 I've got a patent pending on swallowing, oxidation, and chewing gum.
109 Sortir en Pantoufles: up 33 days, 15:15
110 --
111 gentoo-user@g.o mailing list

Replies

Subject Author
Re: [gentoo-user] Bash prompt Holly Bostick <motub@××××××.nl>