Gentoo Archives: gentoo-user

From: Holly Bostick <motub@××××××.nl>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Bash prompt
Date: Wed, 14 Sep 2005 11:29:54
Message-Id: 4328088C.40607@planet.nl
In Reply to: [gentoo-user] Bash prompt by Charles Trois
1 Charles Trois schreef:
2 > Hello!
3 >
4 > I am getting confused with profile, bashrc, etc. The prompt string I
5 > want to use is
6 >
7 > PS1="[\u@\h \W]\$ "
8 >
9 > I therefore wrote it in /etc/profile (at two levels, root and
10 > non-root), ~/.bash_profile and ~/.bashrc.
11 >
12 > If I log in as a plain user (moi), I get this:
13 >
14 > [moi@sirrah moi]$
15 >
16 > which is all right. But, if I log in as root, I get the basic default
17 >
18 >
19 >
20 >
21 >
22 > bash-2.05b# .
23 >
24 > I thought that /etc/profile should provide the default, but I was
25 > obviously wrong. Trying to mend things, I created two files
26 > /root/.bash_profile and /root/.bashrc, writing just PS1 in each. Now,
27 > logging in as root, the result is
28 >
29 > [root@sirrah root]$
30 >
31 > which is wrong, since "$" appears in place of "#", as though my
32 > syntax of PS1 were incorrect, but I don't see that it is.
33
34 No, it's not incorrect, but if you wrote the exact same PS1 in the root
35 entry as in the user entry, perhaps you see that there's a 'literal'
36 dollar sign character at the end:
37
38 PS1="[\u@\h \W]\$ "
39
40 which is going to be printed as itself, as you are not using any code to
41 change it to the 'correct' character based on user (perhaps bash thinks
42 you've escaped the closing bracket, not the following "$". A space
43 between the bracket and the "\$" might solve this, but I've never really
44 got /$ to work properly. It may, however, be because of the 'login shell
45 issue' -- see below, but basically, if you're using su and not su -, your
46 UID is not changing, so the /$ is not changing either).
47
48 Now, as to /etc/profile, /.bash_profile, and /.bashrc;
49
50 OK, this is a bit complex. /etc/profile should in fact provide the
51 default if no other is provided (bash_profile overrides, but since if a
52 bash_profile is provided, as it seems to be on Gentoo, it only contains
53 a little scriptlet that says "look at (source) ~/.bashrc", making
54 ~/.bashrc the
55 *real* override. This is how it works on my system, and this seems to be
56 a generally default behaviour, as I've seen it under all distros where I
57 tried to customize the bash prompt, as I usually do if I stick with the
58 distro for any length of time).
59
60 My gentoo /etc/profile says:
61
62 if [ -n "${BASH_VERSION}" ] ; then
63 # Newer bash ebuilds include /etc/bash/bashrc which will setup PS1
64 # including color. We leave out color here because not all
65 # terminals support it.
66 if [ -f /etc/bash/bashrc ] ; then
67 # Bash login shells run only /etc/profile
68 # Bash non-login shells run only /etc/bash/bashrc
69 # Since we want to run /etc/bash/bashrc regardless, we
70 source it
71 # from here. It is unfortunate that there is no way to do
72 # this *after* the user's .bash_profile runs (without
73 putting
74 # it in the user's dot-files), but it shouldn't make any
75 # difference.
76 . /etc/bash/bashrc
77 else
78 PS1='\u@\h \w \$ '
79 fi
80 else
81 # Setup a bland default prompt. Since this prompt should be useable
82 # on color and non-color terminals, as well as shells that don't
83 # understand sequences such as \h, don't put anything special in it.
84 PS1="`whoami`@`uname -n | cut -f1 -d.` \$ "
85 fi
86
87 So, if there's a /etc/bash/bashrc found, it is sourced under all
88 circumstances, but if for some reason that doesn't work (like why??) the
89 prompt is set at
90
91 user@host workdir appropriate_symbol_for_user_class
92
93 Fine.
94
95 If there is no /etc/bash/bashrc found (as in the case of using another
96 shell), I think that the prompt is set to be the same thing, but just
97 with different language, since as mentioned, every shell doesn't
98 understand bash sequences.
99
100 OK. So far so good.
101
102 So this tells us that the first thing you did 'incorrectly' was changing
103 /etc/profile, instead of /etc/bash/bashrc (or /etc/bashrc, if that's
104 where bash 2 puts it).
105
106 This fits with what I know, insofar as I know that bashrc (preferably in
107 ~/) trumps everything, generally. So it makes sense to me that if you
108 don't have a .bashrc in your home, there's a default one in /etc that
109 basically trumps everything.
110
111 So let's look at /etc/bash/bashrc (I'm using bash 3.0). The relevant
112 entries would seem to be:
113
114 if ${use_color} ; then
115 if [[ ${EUID} == 0 ]] ; then
116 PS1='\[\033[01;31m\]\h \[\033[01;34m\]\W \$ \[\033[00m\]'
117 else
118 PS1='\[\033[01;32m\]\u@\h \[\033[01;34m\]\w \$ \[\033[00m\]'
119 fi
120 else
121 if [[ ${EUID} == 0 ]] ; then
122 # show root@ when we don't have colors
123 PS1='\u@\h \W \$ '
124 else
125 PS1='\u@\h \w \$ '
126 fi
127 fi
128
129 Fine, so if the user is root, and it's a color term, the prompt is
130
131 hostname short_path_to_workdir symbol (in this case presumably '#')
132
133 with colors, and if the user is not root, it's
134
135 user@host full_path_to_workdir symbol (in this case, presumably '$')
136 with colors.
137
138 If there's no color, for root it's
139
140 user@hostname short_path_to_cwd symbol
141
142 and for user
143
144 user@hostname full_path_to_cwd symbol.
145
146 I have no idea (and I have never had any idea) why, despite all these
147 files that set up PS1, you would ever get
148
149 bash-2.05b#
150
151 which I also hate, because it's the most uninformative prompt that is
152 possible. As if I give the first hairy hoot as to what version of bash
153 it is (rather than my cwd or the hostname, for example).
154
155 But OK. Moving on to the files in your ~/ folder (user or root).
156
157 The setup on my system (and I think this is default) was that I got a
158 ~/.bash_profile, which sources ~/.bashrc, and a ~/.bashrc, which
159 contained mostly aliases, allowed for bash completion (if I uncommented
160 it), and set up the same boring bash prompt as the one in /etc
161 (user@hostname cwd symbol).
162
163 This would likely be, then, the second thing you did 'incorrectly'-- your
164 /root/.bash_profile does not source ~/.bashrc as it (apparently) should,
165 but contains a PS1 entry alone.
166
167 I basically just copied my ~/.bash_profile to /root and left it at that:
168
169 # /etc/skel/.bash_profile:
170 # $Header: /home/cvsroot/gentoo-src/rc-scripts/etc/skel/.bash_profile,v
171 1.10 2002/11/18 19:39:22 azarah Exp $
172
173 #This file is sourced by bash when you log in interactively.
174 [ -f ~/.bashrc ] && . ~/.bashrc
175
176 So ~/.bashrc is king of the hill, as it is apparently meant to be, so
177 we'll talk about that. As I said, the default ~/.bashrc contained a
178 number of aliases, and because I use a lot of aliases, I worked with
179 ~/.bashrc a lot (for both home and root). Using a lot of links (posted
180 below) I was able to get a nice 3-line prompt that I like. I then copied
181 the prompt to root's bashrc and made minor changes (the colors, mostly).
182 and that was fine.
183
184 Then I tackled the problem of the login shell.
185
186 You see, ~/.bashrc is only sourced when you login (to the terminal,
187 which, don't forget, is a virtual console). So if you're in the term as
188 a user, and then you su normally to root, root's bashrc is not going to
189 be sourced, and (among other issues) you're not going to get the right
190 prompt (seemingly because your UID doesn't actively change to UID 0, but
191 rather you remain the regular user with elevated permissions).
192
193 The solution to this is (for me, as I really like bash but am not all
194 that good with it as yet, so any more 'elegant' solutions are unknown to
195 me), is to make sure my su is always a login prompt (alias su="su -"),
196 so that root's bashrc is sourced when I su, so I can use root's aliases,
197 and get root's $PATH, since there's nothing more annoying to me than
198 su-ing to root and still getting a 'file not found' error because
199 whatever I'm trying to do is still not in my $PATH, because root's PATH
200 was somehow not exported by su-ing. I also added an ENV_SUPATH variable
201 in my ~/.bashrc, but as I understand it, this variable is deprecated (it's
202 going out in 4.0.9 of pam-login? anyway version 4.0.9 iirc of whatever
203 provides or reads /etc/login.defs which version I have not yet upgraded
204 to, but likely will at some point), so I felt it wise to consider setting
205 ENV_SUPATH a fallback setting that will continue to work until such time
206 as I upgrade that program to the version where it won't work anymore--
207 which I probably won't notice doing, all things considered-- and aliasing su
208 to be the main setting to work around this issue).
209
210 For reference, here's my prompt (all one line, but naturally it's going
211 to be wrapped by the mail client):
212
213 PS1="\n\[\033[1;35m\]\$(/bin/date +%a\ %D\
214 %R)\n\[\033[32m\]\w\n\[\033[1;33m\]\u@\[\033[1;37m\]\h\[\033[1;34m\]\[\033[1;33m\]
215 -> \[\033[0m\]"
216
217 and it looks like this on-screen:
218
219 wo 09/14/05 12:20
220 /usr/local/games/morrowind
221 user@hostname -> cd
222
223 (in color; date is magenta, cwd is green, user@ is white, host -> is
224 yellow, command is the traditional light gray).
225
226 if I su, the prompt remains the same:
227
228 wo 09/14/05 12:22
229 ~
230 root@hostname ->
231
232 except that the date is now green (because it's OK), cwd is now yellow
233 (warning), and 'root@' is now red (big warning), as is the ->, so that I
234 have some visual clue that I'm running as root and I should be careful.
235
236 The /$ thing never worked for me, so I just got rid of it and use
237 another method (color) to distinguish which user is active, and replaced
238 the symbol with another set of symbols to distinguish the command from
239 the prompt.
240
241 Now, as for the links that I used to actually set up the prompt:
242
243 http://www.pages.drexel.edu/~jec43/bash.htm
244
245 http://www.linuxhelp.net/guides/bashprompt/
246
247 http://www.linuxjournal.com/node/3215/print
248
249 http://forums.gentoo.org/viewtopic-t-5850-postdays-0-postorder-asc-highlight-bash+prompt-start-0.html
250
251 http://www.linuxlookup.com/html/articles/custom-prompts.html (site is
252 currently down due to hardware failure)
253
254 http://www.linuxselfhelp.com/howtos/Bash-Prompt/Bash-Prompt-HOWTO-2.html
255
256 http://www-128.ibm.com/developerworks/linux/library/l-tip-prompt/
257
258 http://www.linuxfocus.org/English/May2004/article335.shtml#335lfindex3
259
260 and of course you can always read man bash or the Advanced Bash
261 Scripting guide (emerge abs-guide) to learn more about what you can do
262 with bash.
263
264 Hope this helps; bash prompt setup is one of those (many?) things in
265 Linux that is terribly confusing until you get the hang of its internal
266 consistency.
267
268 Holly
269 --
270 gentoo-user@g.o mailing list

Replies

Subject Author
Re: [gentoo-user] Bash prompt Willie Wong <wwong@×××××××××.EDU>