Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to change the colors (background, font, foreground) of my xterm from the commandline. I've heard that this can be done using ANSI escape sequences.

If this is possible:

  • How is it done?
  • Can I use color names or must I use their RGB codes?
share|improve this question

4 Answers 4

up vote 11 down vote accepted

ANSI escape sequences consist of a sequence of characters beginning with the Escape character, character 27. The next character is often (though not always) an open-square-bracket: [

The echo command can send escape characters if you specify -e and use \e for escape.

The ANSI standard defines 8 colours, plus a bright mode, giving a total of 16 posibilities. The sequence is:


Where <number> is one of:


  • 30 Black
  • 31 Red
  • 32 Green
  • 33 Yellow
  • 34 Blue
  • 35 Magenta
  • 36 Cyan
  • 37 White


  • 40 Black
  • 41 Red
  • 42 Green
  • 43 Yellow
  • 44 Blue
  • 45 Magenta
  • 46 Cyan
  • 47 White

  • 0 Reset all

  • 1 Bold

So to make your foreground red and your background yellow:

$ echo -e "\e[31m\e[43m"

And to enable bold:

$ echo -e "\e[1m"

Of course, you can combine them all together:

$ echo -e "\e[31m\e[43m\e[1m"

There are many many other escape codes for doing other things.

For example - clear the screen and move the cursor to the top-left:

$ echo -e "\e[2J\e[1;1H"

Which is useful when changing the colour:

$ echo -e "\e[31m\e[43m\e[1m\e[2J\e[1;1H"

Which will change the colours, clear the screen, and put the cursor at the top-left. Well, almost the top left. Echo puts a carriage return in, so it moves down a line. You can add -n to echo to prevent this if you're fussy.

If you mess it all up and can't see what you're typing you can reset the terminal colours to normal by pressing:

Ctrl+v [ 0 m Return

At what you hope is the command prompt. It will whinge about an unknown command, but you will be able to see what you're doing again.

share|improve this answer
can I use color names instead of color codes? –  Nathan Fellman Apr 13 '11 at 14:02
No, but you can assign the colour codes to variables. FGRED=31; echo -e "\e[${FGRED}m" –  Majenko Apr 13 '11 at 14:15
you rule,you are great man... Nice example:) –  Parhs May 11 '11 at 14:36
It should be noted, however, that multiple color code numbers can be separated by ";". So to make your foreground red and your background yellow you can also use the shorter: echo -ne '\e[31;43m' (instead of the above echo -e "\e[31m\e[43m"). –  Andreas Spindler Feb 14 '13 at 15:41
How do I change the color of the cursor to 34 Blue? –  trusktr Jun 27 '14 at 2:48

Note that modern Xterms support 32-bit color!

Simple example. To set a nice dark purple background (hey, each to his own) of value #53186f, you do:

echo -ne "\033]11;#53186f\007"

Note that this instantly changes the color of the whole window, not just of the subsequent text. This is especially nice for making a window obviously different after ssh'ing to a server (for example). Put the above 'echo' into your .bashrc and your terminal automatically changes colors when you log into that server (but won't change back when you Ctrl-D out)

You can spend HOURS on the net looking for this - most docs only talk about the original 16 ANSI colors.

Sources: http://www.steike.com/code/xterm-colors/ and http://rtfm.etla.org/xterm/ctlseq.html (look under "Operating System Controls")

Please note: the escape sequence above is valid for XTerms, and may not work for other implementations of "XTerm-like" windowing terminal emulators that may "look" like an XTerm. For example, "gnome-terminal" or "konsole" have different escape sequences, or may not implement color change at all.

share|improve this answer
This is not working for me, it echos a blank line with no observable change in the terminal. –  Richard Dec 8 '12 at 10:09
This worked for me, whereas the others did not -- very helpful, thx –  vol7ron May 7 '13 at 14:07
+1 changes the window background, and allows color names - like "green" instead of "#00ff00". –  Rob I May 9 '13 at 15:28

NOTE: The "^[" is the escape character, and is inserted with a CTRL-V,CTRL-[, and ^G is a bell character, inserted with CTRL-V,CTRL-G

The following block is in my .bash_profile and ensures that my xterms on this system are ALWAYS white-on-black, even after logging onto another system that may have changed my colors.

perl -e '$e=chr(27);print "${e}[37m ${e}[40m ${e}[2J ${e}[1;1H";'
export PS1='^[[37m^[]0;${HOST}: ${PWD}^G^[[40m${USER}@${HOST}:${PWD}
--> '
share|improve this answer

check also a project ScriptEchoColor
it is completely made in bash scripts
it has packages for ubuntu here
but these scripts can be installed on any distro if you know how to (latest can be downloaded from its git also).

you can use shortened or extended color names like:

echoc "@rRedFg@{/blue}BlueFg@{/GREEN}GreenBg"

a sample: enter image description here

Also, make it sure to take a look at xtermcontrol, for a more extensive control over the xterm on itself (not only the characters).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.