Skip to content
Tue, Dec 12 2006

Linux console codes

Ever wonder how shell scripts can beep at you or show bold text? Then look no further!

To use these features you must use console codes. These are not commands but rather special characters or strings that can be used for a special function or to manipulate the console.

Firstly is the beep. Try opening a terminal window and pressing Control + G. You should hear the system beep now. Not very useful is it. What we want to do is include that in our shell scripts. To do this we need to insert the special character. This is done by pressing Control + V followed by Control + G. You should see a ^G appear. Note that this is not two characters but rather one special character represented by ^G, so you must follow the Control + V, Control + G routine to insert it.

Now we can use this special character we can use it in our shell scripts, for example echo ^G, which when included in our shell scripts should beep like there is no tomorrow.

Next up is colours & formatting. Before you can use colours you must insert another special character, ^[, which is inserted by pressing Control + V followed by Escape. Now you must insert the relevant codes in the format ^[[formatting code;fg colour code;bg colour codem

The formatting codes are as follows

0 reset all attributes
1 bold
2 half-bright
4 underscore
5 blink
7 reverse colours

The colour codes are as follows

Foreground colours
30 black
31 red
32 green
33 brown
34 blue
35 magenta
36 cyan
37 white

Background colours
40 black
41 red
42 green
43 brown
44 blue
45 magenta
46 cyan
47 white

For example to write “New Linux User” in bold with cool green on black writing we would use echo "^[[1;32;40m New Linux User ^[[0m"
. Notice the ^[[0m at the end to return to normal attributes.

So now you can spice up your bash scripts!

For a more in depth look check out the console_codes man page

Happy colouring,
George Bunyan

Around The Web
Share This Post:
  • Digg
  • email
  • Facebook
  • StumbleUpon
  • Tumblr
Technology

Comments

  1. By Adam k

    Hi George, not sure where else to contact you so I’ll leave it in a comment. I’ve tagged you in the blog tag game, check out my post for more info.
    http://www.foogazi.com/2006/12/28/tag-im-it-5-things-you-did-not-know-about-me/