Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 04 Aug 1998 16:51:46 -0700
From:      Studded <Studded@dal.net>
To:        FreeBSD Questions <freebsd-questions@FreeBSD.ORG>, sas@schell.de, wwoods@cybcon.com
Subject:   Bash 2 prompt with color and xterm features
Message-ID:  <35C79E92.5F9416B7@dal.net>

next in thread | raw e-mail | index | archive | help
I got the info on ansi escape sequences to make colored prompts from my
friend Chip Norkus, so I'm passing it on as advertised. I am repeating
my previous post with the color stuff added mostly so that I'll have it
for future reference. :)  

if [ $UID != 0 ]; then
# If not root, make prompt blue
export PROMPT_COLOR="0;34m"
else
# If root, make it bright red
export PROMPT_COLOR="1;31m"
fi

if [ $TERM = xterm ]; then
# This is escaped onto two lines so it won't wrap.
# It works as is.
export PS1="\[\e[${PROMPT_COLOR}\e]1;My Desk\a\
\e]2;$PWD\a\][\u@ME \w]\n \#\\$ "
else
export PS1="\[\e[${PROMPT_COLOR}\][\u@ME \w]\n \#\\$ \[\e[0m\]"
fi

Here are the details:

\[ - start a sequence of non-printing characters

Color sequence
==============
\e - an ASCII escape character (033)
[ - start the ansi escape sequence
${PROMPT_COLOR} - Set according to the user's ID

xterm sequences
===============
\e - an ASCII escape character (033)
]1; - xterm escape sequence for the name of the icon
        (works for wm's like afterstep)
My Desk - literal text string
\a - an ASCII bell character (07)
        This ends the first xterm sequence

\e]2;$PWD\a
Put the present directory in the xterm titlebar. I like to use $PWD
rather than \w because otherwise it puts '~' in the title when you use
just 'cd' to return to your home. 

\] - ends the first non-printing character sequence

[\u@ME \w]
[ - literal [ character
\u - the username of the current user
@ME - literal characters
\w - the current working directory
] - literal ] character
\n - newline
\# - the command number of this command
\\$ - if  the effective UID is 0, a #, otherwise a $
	Needs to be escaped so that it works with the ""

Terminate the ansi sequence
===========================
\[\e[0m\]
\[ - Begin non-printing characters
\e[0m - Cancel ansi escape sequence so that text on
	the prompt line isn't colored. 
\] - End non-printing characters

Example while I'm in my home directory:
[myusername@ME ~]
 22$ 

Another example:
[myusername@ME /usr/ports/shells/bash2]
 23$ 

Here's some info from misc.c in xterm's source about the escape codes
for the title and icon:

        case 0: /* new icon name and title*/
        case 1: /* new icon name only */
        case 2: /* new title only */

And here are the remaining ansi colors, thanks to Chip. In Bash 2 it's
easier to use \e in place of the '^[' escape code, but I'm too lazy to
change them all. :)

# colors
#black
export k="^[[0;30m"
#red
export r="^[[0;31m"
#green
export g="^[[0;32m"
#orange
export y="^[[0;33m"
#blue
export b="^[[0;34m"
#purple
export p="^[[0;35m"
#cyan
export c="^[[0;36m"
#grey
export w="^[[0;37m"
#dark grey
export K="^[[1;30m"
#bright red
export R="^[[1;31m"
#bright green
export G="^[[1;32m"
#yellow
export Y="^[[1;33m"
#bright blue
export B="^[[1;34m"
#bright purple
export P="^[[1;35m"
#bright cyan
export C="^[[1;36m"
#white
export W="^[[1;37m"
#all attributes off
export n="^[[0m"

	I hope this is of use to someone. 

Doug
-- 
***           Chief Operations Officer, DALnet IRC network          ***

When you don't know where you're going, every road will take you there.
     - Yiddish Proverb

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?35C79E92.5F9416B7>