From owner-freebsd-questions Tue Aug 4 16:52:13 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id QAA19250 for freebsd-questions-outgoing; Tue, 4 Aug 1998 16:52:13 -0700 (PDT) (envelope-from owner-freebsd-questions@FreeBSD.ORG) Received: from dt053nd2.san.rr.com (dt053nd2.san.rr.com [204.210.34.210]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id QAA19155 for ; Tue, 4 Aug 1998 16:52:07 -0700 (PDT) (envelope-from Studded@dal.net) Received: from dal.net (Studded@localhost [127.0.0.1]) by dt053nd2.san.rr.com (8.8.8/8.8.8) with ESMTP id QAA07285; Tue, 4 Aug 1998 16:51:47 -0700 (PDT) (envelope-from Studded@dal.net) Message-ID: <35C79E92.5F9416B7@dal.net> Date: Tue, 04 Aug 1998 16:51:46 -0700 From: Studded Organization: Triborough Bridge & Tunnel Authority X-Mailer: Mozilla 4.05 [en] (X11; I; FreeBSD 2.2.6-STABLE-0507 i386) MIME-Version: 1.0 To: FreeBSD Questions , sas@schell.de, wwoods@cybcon.com Subject: Bash 2 prompt with color and xterm features Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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