PDF download Download Article PDF download Download Article

Printing normal text to a terminal is easy. But what if you want to emphasize something? Or use different colours for better readability? This wikiHow will teach you how to add colours and formatting to your terminal text.

  1. 1
    Understand the difference between echo and echo -e . echo without the -e prints whatever you type. Try typing echo "Hello\n" : it will print Hello\n . echo -e , however, will use special symbols if you put the correct letters or numbers after a backslash. Try typing echo -e "Hello\n" : it will print the word Hello and a new, empty line.
  2. 2
    Understand the form of escape sequences. Escape sequences are what you'll use to format your text.
    • Every escape sequence begins with the ESC character. It is not visible and not on the keyboard. But you can still enter it with a backslash and the correct number: since this character is ASCII 27, you can write it either as \033 (octal) or \x1b (hexadecimal).
    • The ESC character is followed by a [ . This square bracket marks the beginning of the actual sequence.
    • After the [ are the numbers and letters that actually decide how a text will be coloured and formatted. You can use multiple codes at once, for example, to make a text bold and red at the same time. The different numbers and letters are then separated by a ; .
    • The last letter is an m . It marks the end of the escape sequence.
    Advertisement
  3. 3
    Change the colour of the text. The numbers 30 to 37 are different colours you can use for your text. For example, type echo -e "\033[31mHello red world" to have the text Hello red world appear in red.


    Number Colour
    Text colours
    30

    black

    31

    red

    32

    green

    33

    yellow

    34

    blue

    35

    magenta

    36

    cyan

    37

    white

  4. 4
    Change the background colour. This is done the same way as changing the text colour, but instead of 30 + some number, it's 40 + some number. For example, a red background would appear with echo -e "\033[41mThis has red background" .
  5. 5
    Change the style. The text can be made bold, italic, crossed out, hidden, and underlined.


    Number Result
    Style change codes
    1

    bold text

    2

    faint text

    3

    italic text

    5

    slowly blinking text (on some systems)

    6

    fast blinking text (on some systems)

    8

    invisible text

    9

    crossed out text

  6. 6
    Reset format to default settings. When you change the background or style to some non-default value, you will notice that it is still different in the next line. Although this stops after your user- and computer name, having parts of the next line in a different format doesn't look good. It can be avoided using a reset code.


    Number Resets
    Reset codes
    0

    everything

    22

    intensity (bold/faint)

    23

    italics

    24

    underline

    25

    blinking (on the systems that support it)

    28

    invisibility

    29

    crossing out

    39

    text colour

    49

    background colour

  7. Advertisement

Expert Q&A

Ask a Question
      Advertisement

      Tips

      • You don't have to use the echo -e command to display text with escape codes in a command line. You can also use the output function of many programming languages, like print() in Python or cout in C++.
      • If the changed format doesn't stop in a new line, you can print \033[0m (the sequence that resets everything) to return to the normal format.
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!
      Advertisement

      Warnings

      • Not all these codes are supported on all terminals. If escape codes are supported, but a specific code isn't, the text will just show in the default font, style and colour.
      • Make sure to reset all formatting (with \033[0m ) after you have printed everything you wanted. This is especially important if you're writing a program or script: you don't want to confuse the user by making their terminal look different, especially if your format is hard to read.


      Advertisement

      About This Article

      Thanks to all authors for creating a page that has been read 1,172 times.

      Is this article up to date?

      Advertisement