<HTML><BODY style="word-wrap: break-word; -khtml-nbsp-mode: space; -khtml-line-break: after-white-space; "><DIV><BLOCKQUOTE type="cite"><DIV>I am using BASH and would like to know how I can get a colored prompt working?</DIV><DIV><BR></DIV><DIV>I don't need the entire prompt colored just the fist part which shows</DIV><DIV>user@host ~&gt;</DIV></BLOCKQUOTE><DIV>There are escape sequences to change the color, position of the carrot, etc</DIV><DIV>So I set up some shell variables in my ~/.bash_profile</DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">WHITE="\e[1;37m\]"</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">BRIGHTGREEN="\e[1;32m\]"</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">GREEN="\e[0;32m\]"</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">CYAN="\e[0;36m\]" </SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">GRAY="\e[0;37m\]"</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">DEFAULT="\e[0m\]"</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">RED="\e[0;31m\]"</SPAN></FONT></DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>("\e" is "escape" in BASH, but isn't standard ANSI)</DIV><DIV>thus these are called "escape sequences"</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>if you just want your prompt to change color, you'd do something like:</DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">PS1="\[${GREEN}\]\u@\h \[${DEFAULT}\]"</SPAN></FONT></DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>"\[" tells bash "the following shouldn't be counted as printing characters when calculating when to wrap text, and "\]" tells it you're back to printing characters</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>I go crazy, and change the title bar to show my host and PWD, as I am usually SSHing about</DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">case $TERM in</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">        xterm*)</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">                TITLEBAR='\e]0;\h:\w\007\]\r'</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">                ;;</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">        cygwin*)</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">                TITLEBAR='\e]0;\h:\w\007\]\r'</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">                ;;</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">        *)</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">                TITLEBAR=''</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">                ;;</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">esac</SPAN></FONT></DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>then, have different prompts for error and non-error</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">function tranquil_thoughts_prompt {</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">        PS1="\[${TITLEBAR}${WHITE}\].oO(\[${GREEN}\]\u\[${BRIGHTGREEN}\]@\[${GREEN}\]\h \[${CYAN}\]\w\[${WHITE}\])\[${DEFAULT}\] "</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">}</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">function error_prompt {</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">        PS1="\[${TITLEBAR}${RED}\]\u@\h \w [exit $last] :-(\[${DEFAULT}\] "</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">}</SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Courier" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;">PROMPT_COMMAND='last="$?"; if [ $last -eq 0 ]; then tranquil_thoughts_prompt ; else error_prompt ; fi';</SPAN></FONT></DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Where PROMPT_COMMAND is evaluated just before printing the prompt</DIV><DIV>$? is the exit status of the last command (ex: perl -e 'exit 5;')</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>here are some more BASH escapes for fun and profit:</DIV><DIV>\u is username</DIV><DIV>\h is host</DIV><DIV>\w is current directory</DIV><DIV>\$ is $ unless you're root - then it's #</DIV><DIV>\d is date (Wed Sept 04)</DIV><DIV>\j is number of suspended processes (w/ ^Z)</DIV><DIV>\n is newline</DIV><DIV>\r is carriage return</DIV><DIV>\t is 24-hr time format</DIV><DIV>\T is 12-hr time format</DIV><DIV>\@ is 12-hr time format w/am-pm</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV><BR class="khtml-block-placeholder"></DIV><BR><BLOCKQUOTE type="cite"><DIV>Also I would like for this to work both in xterms and gnome-terminals</DIV><DIV>in X Windows</DIV><DIV>as well as in console ttys.</DIV></BLOCKQUOTE></DIV>these escape codes are from the VT100,<DIV>so they'll work on any VT100 compliant terminal,</DIV><DIV>which would be everything I've ever seen from Putty to Cygwin to Xterm to Terminal.app to ... etc<BR><DIV><BR class="khtml-block-placeholder"></DIV><DIV>for more colors, and blinking/underline/etc search for "VT100 escape codes" on Google or somewhere</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>If you want, I can send you my .bash_profile - it's got some other interesting things in there like odd environment variables and `shopt` shell configuration and extra options based on OS (Linux or Mac).</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>-Logan</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>PS: sorry for rich text, but it was getting ugly with all the code/explanation mixing</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>PPS: *whew* sorry about the long-winded post, but I just love tweaking my environment :-)</DIV></DIV></BODY></HTML>