# Peeking at command-line ANSI escape sequences

DevFeed: [Peeking at command-line ANSI escape sequences](<https://devfeed.tech/articles/peeking-at-command-line-ansi-escape-sequences-20951.md>)

Original publisher: [Read original article](<https://jakewharton.com/peeking-at-colorful-command-line-output/>)

Published: 2020-10-28T00:00:00Z

Content type: tutorial

Language: en

Sources: [Jake Wharton](<https://devfeed.tech/sources/jake-wharton.md>)

Topics: [Command-line interface](<https://devfeed.tech/topics/cli.md>), [ls](<https://devfeed.tech/topics/ls.md>), [Docker Compose](<https://devfeed.tech/topics/docker-compose.md>), [Terminal](<https://devfeed.tech/topics/terminal.md>)

Tags: [command-line](<https://devfeed.tech/tags/command-line.md>), [commands](<https://devfeed.tech/tags/commands.md>), [compare](<https://devfeed.tech/tags/compare.md>), [display](<https://devfeed.tech/tags/display.md>), [examples](<https://devfeed.tech/tags/examples.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [ls](<https://devfeed.tech/tags/ls.md>), [techniques](<https://devfeed.tech/tags/techniques.md>), [terminal](<https://devfeed.tech/tags/terminal.md>)

## AI overview

This tutorial explains how command-line programs use ANSI escape sequences to add color, bold formatting, cursor movement, and dynamic output. It demonstrates these effects with ls and docker-compose, then shows how to inspect escape sequences by replacing the escape character before piping output.

## Source excerpt

Command-line programs use color to convey additional information and to look pretty. For example, compare the output of ls with and without the --color flag: The color helps convey information in this compact output that would otherwise only be available in more verbose forms (-l). In addition to color, a program may update existing output. You can see this when updating images with docker-compose: Both of these effects are created using something called ANSI escape sequences. ANSI escape crash course Reading the Wikipedia entry on ANSI escapes is a great starting point for learning how to recreate these examples. Each escape sequence starts with a 0x1B (escape) character followed usually by [ and then one or more commands using letters or numbers. The ls example above uses green and blue text as well as making the colored entries bold which we can recreate. echo -e "\e[1;32mbinary\e[0m file \e[1;34mfolder\e[0m" Let's break down the interesting parts: echo -e - Adding the -e flag to echo instructs it to enable backslash escapes. \e[1;32m - \e is a backslash escape for the 0x1B escape character and the [ starts a sequence. 1 enables bold and 32 is the color green. Numbers are separated by ; and terminated by m. Anything that follows will now be displayed as bold and green. \e[0m - Once again \e[ starts a sequence and m terminates it. The 0 clears all previous formatting. \e[1;34m - Nearly identical to the sequence from before except it uses 34 for a blue color. The docker-compose example moves the cursor to rewrite previous output which we can begin to recreate. echo "Pulling zulu-jdk-15 ... downloading" && \ echo "Pulling zulu-jdk-11 ... downloading" && \ echo "Pulling zulu-jdk-8 ... downloading" && \ sleep 2 && \ echo -e "\e[2A\e[24C\e[32mdone\e[0m\e[K" && \ sleep 1 && \ echo -e "\e[24C\e[32mdone\e[0m\e[K" && \ sleep 1 && \ echo -e "\e[3A\e[24C\e[32mdone\e[0m\e[K\n\n" Let's break down the interesting parts for this example: \e[2A - Each echo emits a trailing newlin