Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 2 Jan 2002 22:43:35 -0600 (CST)
From:      Ryan Thompson <ryan@sasknow.com>
To:        "J.S." <johann@broadpark.no>
Cc:        <freebsd-questions@FreeBSD.ORG>
Subject:   Re: Shell prompt contest
Message-ID:  <20020102222226.U10111-100000@catalyst.sasknow.net>
In-Reply-To: <20020102150618.04ae22ff.johann@broadpark.no>

next in thread | previous in thread | raw e-mail | index | archive | help
J.S. wrote to freebsd-questions@FreeBSD.ORG:

> Let's see who has the prettiest one :-)
>
> -- LET THE GAMES BEGIN!

Here's one for ya.

I think it's safe to say I've got the most complicated bash prompts
yet shown. :-)

See below for a good chunk of my /etc/profile (bash), which I've been
using on quite a few servers for quite a few years (my actual profile
is several KB larger). It automatically detects most compatible color
terminals (though more could be added to the list). It will display a
plain prompt if a color term is not detected.

Best of all, these are done as shell aliases, so switching between
color and mono prompts is as easy as typing "colorprompt" or
"plainprompt".

The color changes for three classes of users. root gets a red prompt.
Staff (UID < 3000) get a green prompt.  Other shell users get a purple
prompt. (And of course you can change any of these with the 'ucolor'
variable.

It displays something like this:

	ttyp1 ryan@gateway:/staff/ryan $> su -l
	Password:
	ttyp1 root@gateway:/root #>

Of course, this is in contrasting colors. :-) I add the tty because I
do a lot of work from the console and like to know where I am. And, of
course, the hostname and path. On the different machines, I color the
prompts differently. (Although I seem to quickly run out of
permutations).

These prompts also change the title bar in color xterm windows to show
the current path, which I also find handy if I have a lot of shaded
xterms going at once, I can pick the right one.

Best of all, it's commented enough that even novice shell programmers
can make sense of it :-)

Enjoy!

- Ryan


# $Id$
#
# (C)1997,1999,2000,2001 Ryan Thompson <ryan@sasknow.com>
# All rights reserved.
#
# History:
#  * Created by Ryan Thompson in 1997, (based on a similar CSH
#    login profile that I created long prior to that).

# detectprompt() ==============================================================
# Try to detect the capabilities of the current shell.
# If known, use a color prompt. Otherwise, don't.
# We also detect xterm prompts with this function.
# -----------------------------------------------------------------------------
detectprompt()
{
	case $TERM in
	  xterm-color | cons25 | vt100 | vt200 | vt220 | ansi)
		colorprompt ;;
	  *)
		echo "Unknown terminal type. Using mono prompt."
		echo "To use a color prompt, type \"colorprompt\"."
		plainprompt ;;
	esac
}


# plainprompt() - Command to cancel the color prompt ==========================
# -----------------------------------------------------------------------------
plainprompt()
{
	if [ ${UID} == 0 ]; then
		export PS1='\! \u@\h:$PWD #> '
	elif [ ${UID} -lt 3000 ]; then
		export PS1='\! \u@\h:$PWD \$> '
	else
		export PS1='\! \u@\h:$PWD %> '
	fi
}

# colorprompt() - (re)instate the color prompt ================================
# -----------------------------------------------------------------------------
colorprompt()
{
	pxterm='\[\e]2;Terminal: \u@\h $PWD\a\]'
	phistory='\[\e[1;30m\]\!'
	ptty='\[\e[1;30m\]'`tty | cut -c 6-`
	puser='\[\e[0;${ucolor}m\]\u\[\e[1m\]@\[\e[0;${ucolor}m\]\h'
	pcolon='\[\e[0m\]:'
	ppwd='\[\e[0;36m\]$PWD'

	# Choose the appropriate prompt color and suffix
	# for root, staff, or other
	if [ ${UID} == 0 ]; then
		ucolor='31'
		psuffix='\[\e[1m\]#>\[\e[0m\] '
	elif [ ${UID} -lt 3000 ]; then
		ucolor='32'
		psuffix='\[\e[1m\]\$>\[\e[0m\] '
	else
		ucolor='35'
		psuffix='\[\e[1m\]%>\[\e[0m\] '
	fi

	if [ $TERM = xterm-color ]; then
  		export PS1="$pxterm$ptty $puser$pcolon$ppwd $psuffix"
	else
 	 	export PS1="$ptty $puser$pcolon$ppwd $psuffix"
	fi
}

# Main ------------------------------------------------------------------------

# Set the appropriate prompt type
detectprompt


-- 
  Ryan Thompson <ryan@sasknow.com>
  Network Administrator, Accounts

  SaskNow Technologies - http://www.sasknow.com
  #106-380 3120 8th St E - Saskatoon, SK - S7H 0W2

        Tel: 306-664-3600   Fax: 306-664-1161   Saskatoon
  Toll-Free: 877-727-5669     (877-SASKNOW)     North America


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020102222226.U10111-100000>