Charles Trois schreef:
> Hello!
> 
> I am getting confused with profile, bashrc, etc. The prompt string I 
> want to use is
> 
> PS1="[EMAIL PROTECTED] \W]\$ "
> 
> I therefore wrote it in /etc/profile (at two levels, root and 
> non-root), ~/.bash_profile and ~/.bashrc.
> 
> If I log in as a plain user (moi), I get this:
> 
> [EMAIL PROTECTED] moi]$
> 
> which is all right. But, if I log in as root, I get the basic default
> 
> 
> 
> 
> 
> bash-2.05b# .
> 
> I thought that /etc/profile should provide the default, but I was 
> obviously wrong. Trying to mend things, I created two files 
> /root/.bash_profile and /root/.bashrc, writing just PS1 in each. Now,
>  logging in as root, the result is
> 
> [EMAIL PROTECTED] root]$
> 
> which is wrong, since "$" appears in place of "#", as though my 
> syntax of PS1 were incorrect, but I don't see that it is.

No, it's not incorrect, but if you wrote the exact same PS1 in the root
entry as in the user entry, perhaps you see that there's a 'literal'
dollar sign character at the end:

PS1="[EMAIL PROTECTED] \W]\$ "

which is going to be printed as itself, as you are not using any code to
change it to the 'correct' character based on user (perhaps bash thinks
you've escaped the closing bracket, not the following "$". A space
between the bracket and the "\$" might solve this, but I've never really
got /$ to work properly. It may, however, be because of the 'login shell
issue' -- see below, but basically, if you're using su and not su -, your
UID is not changing, so the /$ is not changing either).

Now, as to /etc/profile, /.bash_profile, and /.bashrc;

OK, this is a bit complex. /etc/profile should in fact provide the
default if no other is provided (bash_profile overrides, but since if a
bash_profile is provided, as it seems to be on Gentoo, it only contains
a little scriptlet that says "look at (source) ~/.bashrc", making
~/.bashrc the
*real* override. This is how it works on my system, and this seems to be
a generally default behaviour, as I've seen it under all distros where I
tried to customize the bash prompt, as I usually do if I stick with the
distro for any length of time).

My gentoo /etc/profile says:

if [ -n "${BASH_VERSION}" ] ; then
        # Newer bash ebuilds include /etc/bash/bashrc which will setup PS1
        # including color.  We leave out color here because not all
        # terminals support it.
        if [ -f /etc/bash/bashrc ] ; then
                # Bash login shells run only /etc/profile
                # Bash non-login shells run only /etc/bash/bashrc
                # Since we want to run /etc/bash/bashrc regardless, we
source it
                # from here.  It is unfortunate that there is no way to do
                # this *after* the user's .bash_profile runs (without
putting
                # it in the user's dot-files), but it shouldn't make any
                # difference.
                . /etc/bash/bashrc
        else
                PS1='[EMAIL PROTECTED] \w \$ '
        fi
else
        # Setup a bland default prompt.  Since this prompt should be useable
        # on color and non-color terminals, as well as shells that don't
        # understand sequences such as \h, don't put anything special in it.
        PS1="[EMAIL PROTECTED] -n | cut -f1 -d.` \$ "
fi

So, if there's a /etc/bash/bashrc found, it is sourced under all
circumstances, but if for some reason that doesn't work (like why??) the
prompt is set at

[EMAIL PROTECTED] workdir appropriate_symbol_for_user_class

Fine.

If there is no /etc/bash/bashrc found (as in the case of using another
shell), I think that the prompt is set to be the same thing, but just
with different language, since as mentioned, every shell doesn't
understand bash sequences.

OK. So far so good.

So this tells us that the first thing you did 'incorrectly' was changing
/etc/profile, instead of /etc/bash/bashrc (or /etc/bashrc, if that's
where bash 2 puts it).

This fits with what I know, insofar as I know that bashrc (preferably in
~/) trumps everything, generally. So it makes sense to me that if you
don't have a .bashrc in your home, there's a default one in /etc that
basically trumps everything.

So let's look at /etc/bash/bashrc (I'm using bash 3.0). The relevant
entries would seem to be:

if ${use_color} ; then
        if [[ ${EUID} == 0 ]] ; then
                PS1='\[\033[01;31m\]\h \[\033[01;34m\]\W \$ \[\033[00m\]'
        else
                PS1='\[\033[01;[EMAIL PROTECTED] \[\033[01;34m\]\w \$ 
\[\033[00m\]'
        fi
else
        if [[ ${EUID} == 0 ]] ; then
                # show root@ when we don't have colors
                PS1='[EMAIL PROTECTED] \W \$ '
        else
                PS1='[EMAIL PROTECTED] \w \$ '
        fi
fi

Fine, so if the user is root, and it's a color term, the prompt is

hostname short_path_to_workdir symbol (in this case presumably '#')

with colors, and if the user is not root, it's

[EMAIL PROTECTED] full_path_to_workdir symbol (in this case, presumably '$')
with colors.

If there's no color, for root it's

[EMAIL PROTECTED] short_path_to_cwd symbol

and for user

[EMAIL PROTECTED] full_path_to_cwd symbol.

I have no idea (and I have never had any idea) why, despite all these
files that set up PS1, you would ever get

bash-2.05b#

which I also hate, because it's the most uninformative prompt that is
possible. As if I give the first hairy hoot as to what version of bash
it is (rather than my cwd or the hostname, for example).

But OK. Moving on to the files in your ~/ folder (user or root).

The setup on my system (and I think this is default) was that I got  a
~/.bash_profile, which sources ~/.bashrc, and a ~/.bashrc, which
contained mostly aliases, allowed for bash completion (if I uncommented
it), and set up the same boring bash prompt as the one in /etc
([EMAIL PROTECTED] cwd symbol).

This would likely be, then, the second thing you did 'incorrectly'-- your
/root/.bash_profile does not source ~/.bashrc as it (apparently) should,
but contains a PS1 entry alone.

I basically just copied my ~/.bash_profile to /root and left it at that:

# /etc/skel/.bash_profile:
# $Header: /home/cvsroot/gentoo-src/rc-scripts/etc/skel/.bash_profile,v
1.10 2002/11/18 19:39:22 azarah Exp $

#This file is sourced by bash when you log in interactively.
[ -f ~/.bashrc ] && . ~/.bashrc

So ~/.bashrc is king of the hill, as it is apparently meant to be, so
we'll talk about that. As I said, the default ~/.bashrc contained a
number of aliases, and because I use a lot of aliases, I worked with
~/.bashrc a lot (for both home and root). Using a lot of links (posted
below) I was able to get a nice 3-line prompt that I like. I then copied
the prompt to root's bashrc and made minor changes (the colors, mostly).
and that was fine.

Then I tackled the problem of the login shell.

You see, ~/.bashrc is only sourced when you login (to the terminal,
which, don't forget, is a virtual console). So if you're in the term as
a user, and then you su normally to root, root's bashrc is not going to
be sourced, and (among other issues) you're not going to get the right
prompt (seemingly because your UID doesn't actively change to UID 0, but
rather you remain the regular user with elevated permissions).

The solution to this is (for me, as I really like bash but am not all
that good with it as yet, so any more 'elegant' solutions are unknown to
me), is to make sure my su is always a login prompt (alias su="su -"),
so that root's bashrc is sourced when I su, so I can use root's aliases,
and get root's $PATH, since there's nothing more annoying to me than
su-ing to root and still getting a 'file not found' error because
whatever I'm trying to do is still not in my $PATH, because root's PATH
was somehow not exported by su-ing. I also added an ENV_SUPATH variable
in my ~/.bashrc, but as I understand it, this variable is deprecated (it's
going out in 4.0.9 of pam-login? anyway version 4.0.9 iirc of whatever
provides or reads /etc/login.defs which version I have not yet upgraded
to, but likely will at some point), so I felt it wise to consider setting
ENV_SUPATH a fallback setting that will continue to work until such time
as I upgrade that program to the version where it won't work anymore--
which I probably won't notice doing, all things considered-- and aliasing su
to be the main setting to work around this issue).

For reference, here's my prompt (all one line, but naturally it's going
to be wrapped by the mail client):

PS1="\n\[\033[1;35m\]\$(/bin/date +%a\ %D\
%R)\n\[\033[32m\]\w\n\[\033[1;[EMAIL 
PROTECTED];37m\]\h\[\033[1;34m\]\[\033[1;33m\]
-> \[\033[0m\]"

and it looks like this on-screen:

wo 09/14/05 12:20
/usr/local/games/morrowind
[EMAIL PROTECTED] -> cd

(in color; date is magenta, cwd is green, user@ is white, host ->  is
yellow, command is the traditional light gray).

if I su, the prompt remains the same:

wo 09/14/05 12:22
~
[EMAIL PROTECTED] ->

except that the date is now green (because it's OK), cwd is now yellow
(warning), and 'root@' is now red (big warning), as is the ->, so that I
have some visual clue that I'm running as root and I should be careful.

The /$ thing never worked for me, so I just got rid of it and use
another method (color) to distinguish which user is active, and replaced
the symbol with another set of symbols to distinguish the command from
the prompt.

Now, as for the links that I used to actually set up the prompt:

http://www.pages.drexel.edu/~jec43/bash.htm

http://www.linuxhelp.net/guides/bashprompt/

http://www.linuxjournal.com/node/3215/print

http://forums.gentoo.org/viewtopic-t-5850-postdays-0-postorder-asc-highlight-bash+prompt-start-0.html

http://www.linuxlookup.com/html/articles/custom-prompts.html (site is
currently down due to hardware failure)

http://www.linuxselfhelp.com/howtos/Bash-Prompt/Bash-Prompt-HOWTO-2.html

http://www-128.ibm.com/developerworks/linux/library/l-tip-prompt/

http://www.linuxfocus.org/English/May2004/article335.shtml#335lfindex3

and of course you can always read man bash or the Advanced Bash
Scripting guide (emerge abs-guide) to learn more about what you can do
with bash.

Hope this helps; bash prompt setup is one of those (many?) things in
Linux that is terribly confusing until you get the hang of its internal
consistency.

Holly
-- 
gentoo-user@gentoo.org mailing list

Reply via email to