[Techtalk] shell scripting - modifying your prompt

Akkana akkana at shallowsky.com
Thu Feb 13 22:31:11 EST 2003


Sounds like it's fairly tricky to make a color prompt in bash.
In tcsh it's easy: "%B% %b" puts it into "boldface" mode, prints
a "% " (substitute whatever you want there), then takes it out of
boldface mode.  Then "boldface" gets interpreted as a color according
to my shell client (rxvt) and its normal termcap/terminfo setting.
(Not all shell clients do this, but there are plenty to choose from
which do, so I don't bother with the ones that don't.)

However, since the topic is "shell scripting", I'll show you what I
really do to set my prompt in my .cshrc.  This is a .cshrc that I"ve
been growing for many years, so it has code to handle terminal clients
in solaris, irix, hp-ux, etc.  Then it sets the prompt to the hostname
(I use telnet/rsh/ssh a lot, so I rely on the prompt to tell me where I
am).  

Oh, it also tells me the subshell level, so that if I'm three
subshells deep, it says "(imbrium''')- " instead of just "(imbrium)- ".
($PRIMES is initialized in .login.) And it has a special prompt if I'm
root, so I can use the same .cshrc for root and have my usual aliases,
but still tell easily from the prompt when I'm root and not myself.

(I know, this all sounds ridiculously complex.  That's what happens
when you gradually build up a .cshrc over 17 years ...  It also
probably has plenty of cruft that I ought to clean up.  And it's
probably all useless to anyone else since you all use bash, but
here it is anyway. :-)

	...Akkana

###############################
# Terminal setting:
###############################
set standout = ""
set standout_end = ""
if ($?tcsh) then
    #set standout = "%U"
    #set standout_end = "%u"
    set standout = "%B"
    set standout_end = "%b"
else if ( $term =~ iris* ) then
    # Set the prompt to be yellow.  This puts the prompt in boldface:
    # set standout = "^[[1m"
    # set standout_end = "^[[m"
    # but this is more reliable, setting the color explicitly:
    set standout = "^[[33m"
    set standout_end = "^[[37m"
    # colors: 30-37  Set the text color to black, red, green, yellow,
    # blue, magenta, cyan or white, respecotively (ISO 6429).
    # 40-47  Set the page color to black, red, green, yellow, blue,
    # magenta, cyan or white, respectively (ISO 6429).
else if ( $term =~ hp* ) then
    set standout = "^[&v2S"
    set standout_end = "^[&v3S"
else if ($term == "xterm") then
    set standout = "^[[7m"
    set standout_end = "^[[m"
else if ($term == "vt100") then
    # a couple of items for pcplus and netcom
    if ( -x /usr/5bin/tabs ) /usr/5bin/tabs
    set standout = "^[[7m"
    set standout_end = "^[[m"
else if ($term == "ansi" || $term == 'vt102') then
    stty -tabs
    set standout = "^[[7m"
    set standout_end = "^[[m"
endif

###############################
# Prompt setting:
###############################
# and set the prompt with the correct number of primes in it:
if (! $?PRIMES) setenv PRIMES ""
setenv PRIMES $PRIMES\'

# SunOS has different args for hostname than everyone else, sigh.
# It also doesn't have whoami.
if ($opsys == SunOS) then
  set host = `hostname`
  set me = "akkana"
else
  set host = `hostname -s`
  set me = `whoami`
endif

if ($me == 'akkana') then
  set prompt = "$standout($host$PRIMES)-$standout_end "
else if ($me == 'root') then
  # square brackets in next line cause a "variable syntax" error, so:
  set prompt = "$standout<$host#$PRIMES>-$standout_end "
else
  set prompt = "$standout($me@$host$PRIMES)-$standout_end "
endif

unset me
unset host
unset standout
unset standout_end

	...Akkana



More information about the Techtalk mailing list