How can certain programs set background and font colors, write characters to the same location on screen (e.g. to the third row, fifth column) more than once?

Some examples are the progress bar displayed by curl google.com > a, some installation screens and nearly any text editor.


What you're asking about isn't really a graphics API, it's just terminal control characters.

There's a lot of history behind it, but terminals back in the day were Teletype machines. Basically a typewriter with track-fed paper connected to the computer with a serial connection. Typing a character would send that binary value to the computer (as well as type it on the page). The computer would print characters back as the output of whatever you requested.

Special characters were used for controlling the terminal (which is where the control key comes from, it was for producing those characters). For example, ^H or ^? would be a backspace, ^M is a carriage return (moves the cursor to the beginning of the line) and ^J is a line feed (move the page up one line). Some control codes have an escape sequence in C (which is shared by almost all programming languages) to generate the control characters. The controls listed previously would be \b, \r, \n respectively.

The terminal emulator you use today is just what that sounds like, it's software that is pretending to be an old teletype machine. In a way, it's almost as if Unix still thinks you're using a typewriter to communicate with it.

In the case of curl, it writes an entire line then sends \r (carriage return) taking the cursor to the beginning of the line then writes another line. Since a line feed isn't sent it keeps writing over the same line on the screen.

Here's something you can try to demonstrate this:

echo -n  "First" ; sleep 5 ; echo -en "\rSecond\n"

You will see the word "First" printed (but no new line is sent). 5 seconds later it will be replaced with the word "Second" and a new line is sent. You can repeat this pattern indefinitely. Try adding "Third" yourself, you'll see something that you probably didn't expect ;-)

For more information about these and other control characters see the ascii(7) manual.


Are you looking for something like ncurses?

  • 3
    These days, you'll want the new curses, or ncurses. Jul 5 '12 at 19:23
  • Exactly. Thanks for corrections. I forgot they different. Jul 6 '12 at 9:08

That question is actually several questions, and "control characters" addresses only a small part of it, e.g., the progress-bar for curl. More generally, these are common features of terminals (and terminal emulators).

Most of these common features are standardized in ECMA-48: Control Functions for Coded Character Sets. However, other features are not. They are implementation-defined.

ECMA-48 refers to control functions. That includes control characters and control sequences (often referred to as escape sequences, ANSI sequences, etc).

Some control characters are used for simple operations, e.g.,

  • move the cursor to the previous column on the same row
  • move the cursor to the first column on the same row
  • move the cursor to the next tab stop on the same row
  • move the cursor to the next row (and scroll the screen if on the last row already)

The progress-bar for curl is built using these simple operations. But control characters can do only so much, no more. Control sequences do more, e.g.,

  • move the cursor to any row/column on the screen
  • move the cursor to any row in the same column on the screen
  • move the cursor to any column in the same row on the screen
  • set tab-stops at any column on the screen
  • move the cursor to the previous line
  • make the screen scroll up or down without moving the cursor

So much for parallels between simple/complex. Control sequences also are used to change the color of text and background, erase text from the screen, show text in reverse-video (or bold, underline, blink).

Programs that draw a reverse-video (or colored) progress-bar use control sequences.

Although control sequences can do more, they can do only specific things. Putting them together to make text-editors, installation screens (and programs that draw colored progress-bars) gets complicated. Some of that is made simpler by using libraries that know about these things. Initially, we had termcap (and a database of a few hundred types of terminal), extended to terminfo (and a database of around a thousand types of terminal).

Even with standardization, there are dozens of terminal descriptions that you could use. So we continue to use libraries for all but the most trivial of these applications. One is ncurses ("new-curses"), another is slang (technically "S-Lang").

Further reading:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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