old-www/HOWTO/Bash-Prompt-HOWTO/x329.html

452 lines
8.5 KiB
HTML

<HTML
><HEAD
><TITLE
>Colours</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Bash Prompt HOWTO"
HREF="index.html"><LINK
REL="UP"
TITLE="ANSI Escape Sequences: Colours and Cursor Movement"
HREF="c327.html"><LINK
REL="PREVIOUS"
TITLE="ANSI Escape Sequences: Colours and Cursor Movement"
HREF="c327.html"><LINK
REL="NEXT"
TITLE="Cursor Movement"
HREF="x361.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Bash Prompt HOWTO: </TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="c327.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 6. ANSI Escape Sequences: Colours and Cursor Movement</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x361.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN329"
></A
>6.1. Colours</H1
><P
>As mentioned before, non-printing escape sequences have to be enclosed in
<TT
CLASS="USERINPUT"
><B
>\[\033[</B
></TT
> and <TT
CLASS="USERINPUT"
><B
>\]</B
></TT
>. For colour
escape sequences, they should also be followed by a lowercase
<TT
CLASS="USERINPUT"
><B
>m</B
></TT
>. </P
><P
>If you try out the following prompts in an xterm and find that you aren't
seeing the colours named, check out your
<TT
CLASS="FILENAME"
>~/.Xdefaults</TT
> file (and
possibly its bretheren) for lines like
<TT
CLASS="COMPUTEROUTPUT"
>XTerm*Foreground: BlanchedAlmond</TT
>.
This can be commented out by placing an exclamation mark ("!") in front of
it. Of course, this will also be dependent on what terminal emulator
you're using. This is the likeliest place that your term foreground
colours would be overridden.</P
><P
>To include blue text in the prompt:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>PS1="\[\033[34m\][\$(date +%H%M)][\u@\h:\w]$ "</PRE
></FONT
></TD
></TR
></TABLE
><P
>The problem with this prompt is that the blue colour that starts with the
34 colour code is never switched back to the regular colour, so any text
you type after the prompt is still in the colour of the prompt. This is
also a dark shade of blue, so combining it with the
<EM
>bold</EM
> code might help:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>PS1="\[\033[1;34m\][\$(date +%H%M)][\u@\h:\w]$\[\033[0m\] "</PRE
></FONT
></TD
></TR
></TABLE
><P
>The prompt is now in light blue, and it ends by switching the colour
back to nothing (whatever foreground colour you had previously).</P
><P
>Here are the rest of the colour equivalences:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>Black 0;30 Dark Gray 1;30
Blue 0;34 Light Blue 1;34
Green 0;32 Light Green 1;32
Cyan 0;36 Light Cyan 1;36
Red 0;31 Light Red 1;31
Purple 0;35 Light Purple 1;35
Brown 0;33 Yellow 1;33
Light Gray 0;37 White 1;37</PRE
></FONT
></TD
></TR
></TABLE
><P
>Daniel Dui (ddui@iee.org) points out that to be strictly accurate, we must
mention that the list above is for colours at the console. In an xterm,
the code <TT
CLASS="USERINPUT"
><B
>1;31</B
></TT
> isn't "Light Red," but "Bold Red."
This is true of all the colours.</P
><P
>You can also set background colours by using 44 for Blue background, 41 for
a Red background, etc. There are no bold background colours. Combinations
can be used, like Light Red text on a Blue background:
<TT
CLASS="USERINPUT"
><B
>\[\033[44;1;31m\]</B
></TT
>, although setting the colours
separately seems to work better (ie.
<TT
CLASS="USERINPUT"
><B
>\[\033[44m\]\[\033[1;31m\]</B
></TT
>). Other codes
available include 4: Underscore, 5: Blink, 7: Inverse, and 8: Concealed.</P
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>Many people (myself included) object strongly to the
"blink" attribute because it's extremely distracting and irritating.
Fortunately, it doesn't work in any terminal emulators
that I'm aware of - but it will still work on the console. </P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>If you were wondering (as I did) "What use is a 'Concealed' attribute?!" -
I saw it used in an example shell script (not a prompt) to allow someone to
type in a password without it being echoed to the screen. However, this
attribute doesn't seem to be honoured by many terms other than "Xterm."&#13;</P
></TD
></TR
></TABLE
></DIV
><P
>Based on a prompt called "elite2" in the Bashprompt package (which I
have modified to work better on a standard console, rather than with the
special xterm fonts required to view the original properly), this is a
prompt I've used a lot:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>
function elite
{
local GRAY="\[\033[1;30m\]"
local LIGHT_GRAY="\[\033[0;37m\]"
local CYAN="\[\033[0;36m\]"
local LIGHT_CYAN="\[\033[1;36m\]"
local NO_COLOUR="\[\033[0m\]"
case $TERM in
xterm*|rxvt*)
local TITLEBAR='\[\033]0;\u@\h:\w\007\]'
;;
*)
local TITLEBAR=""
;;
esac
local temp=$(tty)
local GRAD1=${temp:5}
PS1="$TITLEBAR\
$GRAY-$CYAN-$LIGHT_CYAN(\
$CYAN\u$GRAY@$CYAN\h\
$LIGHT_CYAN)$CYAN-$LIGHT_CYAN(\
$CYAN\#$GRAY/$CYAN$GRAD1\
$LIGHT_CYAN)$CYAN-$LIGHT_CYAN(\
$CYAN\$(date +%H%M)$GRAY/$CYAN\$(date +%d-%b-%y)\
$LIGHT_CYAN)$CYAN-$GRAY-\
$LIGHT_GRAY\n\
$GRAY-$CYAN-$LIGHT_CYAN(\
$CYAN\$$GRAY:$CYAN\w\
$LIGHT_CYAN)$CYAN-$GRAY-$LIGHT_GRAY "
PS2="$LIGHT_CYAN-$CYAN-$GRAY-$NO_COLOUR "
}</PRE
></FONT
></TD
></TR
></TABLE
><P
>I define the colours as temporary shell variables in the name of
readability. It's easier to work with. The "GRAD1" variable is a check to
determine what terminal you're on. Like the test to determine if you're
working in an Xterm, it only needs to be done once. The prompt you see
look like this, except in colour:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>--(giles@gcsu202014)-(30/pts/6)-(0816/01-Aug-01)--
--($:~/tmp)--</PRE
></FONT
></TD
></TR
></TABLE
><P
>To help myself remember what colours are available, I wrote a script that
output all the colours to the screen. Daniel Crisman has supplied a much
nicer version which I include below:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>#!/bin/bash
#
# This file echoes a bunch of color codes to the
# terminal to demonstrate what's available. Each
# line is the color code of one forground color,
# out of 17 (default + 16 escapes), followed by a
# test use of that color on all nine background
# colors (default + 8 escapes).
#
T='gYw' # The test text
echo -e "\n 40m 41m 42m 43m\
44m 45m 46m 47m";
for FGs in ' m' ' 1m' ' 30m' '1;30m' ' 31m' '1;31m' ' 32m' \
'1;32m' ' 33m' '1;33m' ' 34m' '1;34m' ' 35m' '1;35m' \
' 36m' '1;36m' ' 37m' '1;37m';
do FG=${FGs// /}
echo -en " $FGs \033[$FG $T "
for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
do echo -en "$EINS \033[$FG\033[$BG $T \033[0m";
done
echo;
done
echo&#13;</PRE
></FONT
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="c327.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x361.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>ANSI Escape Sequences: Colours and Cursor Movement</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c327.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Cursor Movement</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>