2
<meta name="GENERATOR" content="SGML-Tools 1.0.9"><title>Bash Prompt HOWTO: ANSI Escape Sequences: Colours and Cursor Movement</title>
4
<link href="http://new.linuxnow.com/docs/content/Bash-Prompt-HOWTO-html/Bash-Prompt-HOWTO-7.html" rel="next">
5
<link href="http://new.linuxnow.com/docs/content/Bash-Prompt-HOWTO-html/Bash-Prompt-HOWTO-5.html" rel="previous">
6
<link href="http://new.linuxnow.com/docs/content/Bash-Prompt-HOWTO-html/Bash-Prompt-HOWTO.html#toc6" rel="contents"></head>
7
<body bgcolor="#ffffff" marginwidth="0" marginheight="0" topmargin="0" leftmargin="0">
37
<table width="100%" cellpadding="0" cellspacing="0" border="0" background="Bash-Prompt-HOWTO-6_files/ln_expando.gif">
39
<td><a href="http://new.linuxnow.com/index.shtml"><img src="Bash-Prompt-HOWTO-6_files/ln_logo.gif" width="241" height="74" border="0"></a></td>
40
<td align="right"><a href="http://new.linuxnow.com/library.shtml"><img src="Bash-Prompt-HOWTO-6_files/ln_filelib_off.gif" width="100" height="74" border="0"></a><a href="http://new.linuxnow.com/docs.shtml"><img src="Bash-Prompt-HOWTO-6_files/ln_doc_on.gif" width="111" height="74" border="0"></a><a href="http://new.linuxnow.com/wwwthreads/welcome.pl"><img src="Bash-Prompt-HOWTO-6_files/ln_forum_off.gif" width="56" height="74" border="0"></a><a href="http://new.linuxnow.com/features.shtml"><img src="Bash-Prompt-HOWTO-6_files/ln_features_off.gif" width="66" height="74" border="0"></a><a href="http://new.linuxnow.com/tutorial/preface.html"><img src="Bash-Prompt-HOWTO-6_files/ln_newbies_off.gif" width="108" height="74" border="0"></a></td>
50
<!-- SUB NAV: LOCATOR + SEARCH + BANNER -->
51
<table width="100%" cellpadding="0" cellspacing="0" border="0">
55
<td width="19%" align="center" valign="CENTER">
58
<img src="Bash-Prompt-HOWTO-6_files/ln_doc_big.gif" width="160" height="60" border="0">
65
<td width="54%" valign="CENTER" align="center"><a href="http://new.linuxnow.com/cgi-bin/redir.cgi?dest=http://www.linuxmall.com&ad=LMA_01" target="_top"><img src="Bash-Prompt-HOWTO-6_files/gotlm.gif" border="0" width="468" height="60" alt="The Greatest Geek Mall We Know"></a></td>
69
<form action="/cgi-bin/search.cgi"></form>
70
<td width="23%" valign="CENTER" align="right">
71
<table cellspacing="0" cellpadding="3" border="0">
74
<font face="Helvetica">
75
<b>Search Linux Now:</b><br>
76
<font face="Helvetica" size="-1">
77
<input type="text" name="keywords" size="15">
78
<input type="submit" name="Search" value="Go">
79
<input type="hidden" name="metatype" value="all">
92
<!-- SUB NAV: SEARCH + BANNER -->
103
<a href="http://new.linuxnow.com/index.shtml"><font face="Helvetica"><b>Linux Now</b></font></a> <font face="Helvetica"><b>:</b></font> <a href="http://new.linuxnow.com/docs.shtml"><font face="Helvetica"><b>Documentation</b></font></a> <font face="Helvetica"><b> : </b> <a href="http://new.linuxnow.com/docs/content/System-Administration.html"><font face="Helvetica"><b>System Administration</b></font></a> <font face="Helvetica"><b> : Bash-Prompt-HOWTO</b></font></font><hr><font face="Helvetica"><br><br>
110
<a href="http://new.linuxnow.com/docs/content/Bash-Prompt-HOWTO-html/Bash-Prompt-HOWTO-7.html">Next</a>
111
<a href="http://new.linuxnow.com/docs/content/Bash-Prompt-HOWTO-html/Bash-Prompt-HOWTO-5.html">Previous</a>
112
<a href="http://new.linuxnow.com/docs/content/Bash-Prompt-HOWTO-html/Bash-Prompt-HOWTO.html#toc6">Contents</a>
114
<h2><font face="Helvetica"><a name="s6">6. ANSI Escape Sequences: Colours and Cursor Movement</a></font></h2>
116
<h2><font face="Helvetica"><a name="ss6.1">6.1 Colours</a>
119
<p><font face="Helvetica">As mentioned before, non-printing escape sequences have to be enclosed in
120
<b>\[\033[</b> and <b>\]</b>. For colour escape sequences, they should
121
also be followed by a lowercase <b>m</b>.
123
</p><p><font face="Helvetica">If you try out the following prompts in an xterm and find that you aren't
124
seeing the colours named, check out your ~/.Xdefaults file (and
125
possibly its bretheren) for lines like "XTerm*Foreground: BlanchedAlmond".
126
This can be commented out by placing an exclamation mark ("!") in front of
127
it. Of course, this will also be dependent on what terminal emulator
128
you're using. This is the likeliest place that your term foreground
129
colours would be overridden.
131
</p><p><font face="Helvetica">To include blue text in the prompt:
133
</p><blockquote><code>
134
<pre><font face="Helvetica">PS1="\[\033[34m\][\$(date +%H%M)][\u@\h:\w]$ "
137
<p><font face="Helvetica">The problem with this prompt is that the blue colour that starts with the
138
34 colour code is never switched back to the regular colour, so any text
139
you type after the prompt is still in the colour of the prompt. This is
140
also a dark shade of blue, so combining it with the <b>bold</b> code
143
</p><blockquote><code>
144
<pre><font face="Helvetica">PS1="\[\033[1;34m\][\$(date +%H%M)][\u@\h:\w]$\[\033[0m\] "
148
</p><p><font face="Helvetica">The prompt is now in light blue, and it ends by switching the colour
149
back to nothing (whatever foreground colour you had previously).
151
</p><p><font face="Helvetica">Here are the rest of the colour equivalences:
153
</p><blockquote><code>
154
<pre><font face="Helvetica">Black 0;30 Dark Gray 1;30
155
Blue 0;34 Light Blue 1;34
156
Green 0;32 Light Green 1;32
157
Cyan 0;36 Light Cyan 1;36
158
Red 0;31 Light Red 1;31
159
Purple 0;35 Light Purple 1;35
160
Brown 0;33 Yellow 1;33
161
Light Gray 0;37 White 1;37
165
</p><p><font face="Helvetica">Daniel Dui (ddui@iee.org) points out that to be strictly accurate, we must
166
mention that the list above is for colours at the console. In an xterm,
167
the code <b>1;31</b> isn't "Light Red," but "Bold Red." This is true of
170
</p><p><font face="Helvetica">You can also set background colours by using 44 for Blue background, 41 for
171
a Red background, etc. There are no bold background colours. Combinations
172
can be used, like Light Red text on a Blue background:
173
<b>\[\033[44;1;31m\]</b>, although setting the colours separately seems
174
to work better (ie. <b>\[\033[44m\]\[\033[1;31m\]</b>). Other codes
175
available include 4: Underscore, 5: Blink, 7: Inverse, and 8: Concealed.
177
</p><p><font face="Helvetica"><b>Aside:</b> Many people (myself included) object strongly to the
178
"blink" attribute. Fortunately, it doesn't work in any terminal emulators
179
that I'm aware of - but it will still work on the console. And, if you
180
were wondering (as I did) "What use is a 'Concealed' attribute?!" - I saw
181
it used in an example shell script (not a prompt) to allow someone to type
182
in a password without it being echoed to the screen.
184
</p><p><font face="Helvetica">Based on a prompt called "elite2" in the Bashprompt package (which I
185
have modified to work better on a standard console, rather than with the
186
special xterm fonts required to view the original properly), this is a
187
prompt I've used a lot:
190
<pre><font face="Helvetica">
194
local GRAY="\[\033[1;30m\]"
195
local LIGHT_GRAY="\[\033[0;37m\]"
196
local CYAN="\[\033[0;36m\]"
197
local LIGHT_CYAN="\[\033[1;36m\]"
201
local TITLEBAR='\[\033]0;\u@\h:\w\007\]'
208
local GRAD1=$(tty|cut -d/ -f3)
210
$GRAY-$CYAN-$LIGHT_CYAN(\
211
$CYAN\u$GRAY@$CYAN\h\
212
$LIGHT_CYAN)$CYAN-$LIGHT_CYAN(\
213
$CYAN\#$GRAY/$CYAN$GRAD1\
214
$LIGHT_CYAN)$CYAN-$LIGHT_CYAN(\
215
$CYAN\$(date +%H%M)$GRAY/$CYAN\$(date +%d-%b-%y)\
216
$LIGHT_CYAN)$CYAN-$GRAY-\
218
$GRAY-$CYAN-$LIGHT_CYAN(\
219
$CYAN\$$GRAY:$CYAN\w\
220
$LIGHT_CYAN)$CYAN-$GRAY-$LIGHT_GRAY "
221
PS2="$LIGHT_CYAN-$CYAN-$GRAY-$LIGHT_GRAY "
226
</p><p><font face="Helvetica">I define the colours as temporary shell variables in the name of
227
readability. It's easier to work with. The "GRAD1" variable is a check to
228
determine what terminal you're on. Like the test to determine if you're
229
working in an Xterm, it only needs to be done once. The prompt you see
230
look like this, except in colour:
232
</p><blockquote><code>
233
<pre><font face="Helvetica">--(giles@nikola)-(75/ttyp7)-(1908/12-Oct-98)--
238
</p><p><font face="Helvetica">To help myself remember what colours are available, I wrote the following
239
script which echoes all the colours to screen:
242
<pre><font face="Helvetica">#!/bin/bash
244
# This file echoes a bunch of colour codes to the terminal to demonstrate
245
# what's available. Each line is one colour on black and gray
246
# backgrounds, with the code in the middle. Verified to work on white,
247
# black, and green BGs (2 Dec 98).
249
echo " On Light Gray: On Black:"
250
echo -e "\033[47m\033[1;37m White \033[0m\
252
\033[40m\033[1;37m White \033[0m"
253
echo -e "\033[47m\033[37m Light Gray \033[0m\
255
\033[40m\033[37m Light Gray \033[0m"
256
echo -e "\033[47m\033[1;30m Gray \033[0m\
258
\033[40m\033[1;30m Gray \033[0m"
259
echo -e "\033[47m\033[30m Black \033[0m\
261
\033[40m\033[30m Black \033[0m"
262
echo -e "\033[47m\033[31m Red \033[0m\
264
\033[40m\033[31m Red \033[0m"
265
echo -e "\033[47m\033[1;31m Light Red \033[0m\
267
\033[40m\033[1;31m Light Red \033[0m"
268
echo -e "\033[47m\033[32m Green \033[0m\
270
\033[40m\033[32m Green \033[0m"
271
echo -e "\033[47m\033[1;32m Light Green \033[0m\
273
\033[40m\033[1;32m Light Green \033[0m"
274
echo -e "\033[47m\033[33m Brown \033[0m\
276
\033[40m\033[33m Brown \033[0m"
277
echo -e "\033[47m\033[1;33m Yellow \033[0m\
279
\033[40m\033[1;33m Yellow \033[0m"
280
echo -e "\033[47m\033[34m Blue \033[0m\
282
\033[40m\033[34m Blue \033[0m"
283
echo -e "\033[47m\033[1;34m Light Blue \033[0m\
285
\033[40m\033[1;34m Light Blue \033[0m"
286
echo -e "\033[47m\033[35m Purple \033[0m\
288
\033[40m\033[35m Purple \033[0m"
289
echo -e "\033[47m\033[1;35m Pink \033[0m\
291
\033[40m\033[1;35m Pink \033[0m"
292
echo -e "\033[47m\033[36m Cyan \033[0m\
294
\033[40m\033[36m Cyan \033[0m"
295
echo -e "\033[47m\033[1;36m Light Cyan \033[0m\
297
\033[40m\033[1;36m Light Cyan \033[0m"
301
</p><h2><font face="Helvetica"><a name="ss6.2">6.2 Cursor Movement</a>
304
<p><font face="Helvetica">ANSI escape sequences allow you to move the cursor around the screen at
305
will. This is more useful for full screen user interfaces generated by
306
shell scripts, but can also be used in prompts. The movement escape
307
sequences are as follows:
309
</p><blockquote><code>
310
<pre><font face="Helvetica">- Position the Cursor:
311
\033[<L>;<C>H
313
\033[<L>;<C>f
314
puts the cursor at line L and column C.
315
- Move the cursor up N lines:
317
- Move the cursor down N lines:
319
- Move the cursor forward N columns:
321
- Move the cursor backward N columns:
324
- Clear the screen, move to (0,0):
326
- Erase to end of line:
329
- Save cursor position:
331
- Restore cursor position:
336
</p><p><font face="Helvetica">The latter two codes are NOT honoured by many terminal emulators. The only
337
ones that I'm aware of that do are xterm and nxterm - even though the
338
majority of terminal emulators are based on xterm code. As far as I can
339
tell, rxvt, kvt, xiterm, and Eterm do not support them. They are supported
342
</p><p><font face="Helvetica">Try putting in the following line of code at the prompt (it's a little
343
clearer what it does if the prompt is several lines down the terminal when
344
you put this in): <code>echo -en "\033[7A\033[1;35m BASH \033[7B\033[6D"</code>
345
This should move the cursor seven lines up screen, print the word " BASH ",
346
and then return to where it started to produce a normal prompt. This isn't
347
a prompt: it's just a demonstration of moving the cursor on screen, using
348
colour to emphasize what has been done.
350
</p><p><font face="Helvetica">Save this in a file called "clock":
353
<pre><font face="Helvetica">#!/bin/bash
355
function prompt_command {
356
let prompt_x=$COLUMNS-5
359
PROMPT_COMMAND=prompt_command
362
local BLUE="\[\033[0;34m\]"
363
local RED="\[\033[0;31m\]"
364
local LIGHT_RED="\[\033[1;31m\]"
365
local WHITE="\[\033[1;37m\]"
366
local NO_COLOUR="\[\033[0m\]"
369
TITLEBAR='\[\033]0;\u@\h:\w\007\]'
377
\[\033[s\033[1;\$(echo -n \${prompt_x})H\]\
378
$BLUE[$LIGHT_RED\$(date +%H%M)$BLUE]\[\033[u\033[1A\]
379
$BLUE[$LIGHT_RED\u@\h:\w$BLUE]\
387
</p><p><font face="Helvetica">This prompt is fairly plain, except that it keeps a 24 hour clock in the
388
upper right corner of the terminal (even if the terminal is resized). This
389
will NOT work on the terminal emulators that I mentioned that don't accept
390
the save and restore cursor position codes. If you try to run this prompt
391
in any of those terminal emulators, the clock will appear correctly, but
392
the prompt will be trapped on the second line of the terminal.
394
</p><p><font face="Helvetica">See also
395
<a href="http://new.linuxnow.com/docs/content/Bash-Prompt-HOWTO-html/Bash-Prompt-HOWTO-12.html#clock3">The Elegant Useless Clock Prompt</a> for a
396
more extensive use of these codes.
398
</p><h2><font face="Helvetica"><a name="ss6.3">6.3 Moving the Cursor With tput</a>
401
<p><font face="Helvetica">As with so many things in Unix, there is more than one way to achieve the
402
same ends. A utility called "tput" can also be used to move the cursor
403
around the screen, or get back information about the status of the
404
terminal. "tput" for cursor positioning is less flexible than ANSI escape
405
sequences: you can only move the cursor to an absolute position, you can't
406
move it relative to its current position. I don't use "tput," so I'm not
407
going to explain it in detail. Type "man tput" and you'll know as much as
413
<font face="Helvetica"><a href="http://new.linuxnow.com/docs/content/Bash-Prompt-HOWTO-html/Bash-Prompt-HOWTO-7.html">Next</a>
414
<a href="http://new.linuxnow.com/docs/content/Bash-Prompt-HOWTO-html/Bash-Prompt-HOWTO-5.html">Previous</a>
415
<a href="http://new.linuxnow.com/docs/content/Bash-Prompt-HOWTO-html/Bash-Prompt-HOWTO.html#toc6">Contents</a>
421
<td width="1%">�</td>
428
<table width="100%" border="0">
430
<td colspan="3"><img src="Bash-Prompt-HOWTO-6_files/red.gif" width="100%" height="2"></td>
433
<td align="left" width="25%"><font face="Helvetica" size="-1">Linux is a trademark of Linus Torvalds<br>LinuxNOW.com TM<br> Theme by Technogeeks Inc.</font></td>
435
<td align="center" width="50%">
437
<center><font face="Helvetica"><a href="http://new.linuxnow.com/library.shtml">file library</a> | <a href="http://new.linuxnow.com/docs.shtml">documentation</a> | <a href="http://new.linuxnow.com/wwwthreads/welcome.pl">forum</a><font></font></font></center><center><font face="Helvetica"><font><a href="http://new.linuxnow.com/features.shtml">features</a> | <a href="http://new.linuxnow.com/about.shtml">about</a> | <a href="http://new.linuxnow.com/index.shtml">home</a> | <a href="http://new.linuxnow.com/contact.shtml">contact us</a><br><a href="http://custom.linuxnow.com/">create distro</a> | <a href="http://jobs.linuxnow.com/">get a job</a> | <a href="http://new.linuxnow.com/lugs.html">LUGS</a> | <a href="http://new.linuxnow.com/cgi-bin/editorial.cgi?article=themes">choose theme</a></font></font></center>
440
<font face="Helvetica"> </font></td>
441
<td align="right" width="25%"><a href="http://www.technogeeks.com/"><img border="0" src="Bash-Prompt-HOWTO-6_files/ln_technogeeks.gif" width="193" heigth="45"></a></td>
b'\\ No newline at end of file'