What Are Escape Sequences?
An ANSI escape sequence consists of the character 27 followed by an open square bracket. This indicates to the system that you want to do something fun with the terminal. The next few characters are a special code that the ANSI driver understands, and will generally result in it doing something. For example, if I were to put in a shell script the following:
echo -en "\033[2J\033[5;9H"This would clear the screen and move the cursor to row 5, column 9. Immediately we can see some implications of this: we can draw anywhere on the screen, not just sequentially. Note that I had to pass -e to echo to force it to interpret \033 as character 27 instead of the literal string "\033". -n just prevents it from automatically appending a newline to the output.
Making Things Stand Out
There are two main ways of making things stand out on a Linux terminal without changing the color. The first is to make text bold. The escape sequence for that is \033[1m. The escape sequence for underlining is \033[4m. Because I'm a lazy typer, from now on the \033[ is assumed, and I'll just refer to the escape sequences as 1m, 4m, etc. Whenever you specify an attribute like this, it remains in effect until you get rid of it with the 0m attribute. To pass more than one attribute at the same time, separate the numbers with a semicolon. So if you really wanted to scream at the user, you could use 1;4m.
echo -en "\033[1mI'm bold, \033[0;4mI'm underlined\033[0m, normal...\033[1;4mSCREAMING!"
Output:
I'm bold, I'm underlined, normal...SCREAMING!
Changing the Foreground Color
Let's say I decide that my script has to put out an error message, and I want it to be in red. No problem! The ANSI driver supports sixteen colors; in the table below, the color on the left is the default, and the color on the right is displayed when you have high intensity set. Depending on the terminal, this is either 1m (yes, the same as bold) or there are a special range of high-intensity colors from 90-99, though those are less common. Play around and see what works with your terminal.
30m black | dark gray
31m dark red | red
32m dark green | green
33m pissy brown | yellow
34m dark blue | blue
35m dark magenta | magenta
36m dark cyan | cyan
37m grey | white
38m (unused)
39m (reset code)
Changing the Background Color
Changes the color displayed behind the character. These codes are just ten higher than the foreground color counterparts. (Same goes for the 90-99 as well; background is 100-110 on the terminals it works for.)
40m black | dark gray
41m dark red | red
42m dark green | green
43m pissy brown | yellow
44m dark blue | blue
45m dark magenta | magenta
46m dark cyan | cyan
47m grey | white
48m (unused)
49m (reset code)
So now we can do something really crazy, like bold underlined green text on a bright white background:
echo -e "Clashing colors\033[1;4;32;47m HERE \033[0m."
Moving the Cursor
(Note that the origin is 1,1 and not 0,0 as programmers are accustomed to.)
xA Move cursor up x rows
xB Move cursor downx rows
xC Move cursor right x columns (wraps to next line if need be)
xD Move cursor left x columns (wraps to prev line if need be)
xE Move cursor down x rows and set at beginning
xF Move cursor upx rows and set at beginning
x;yf Move cursor to row y, column x. If you leave out one or the other it will assume 1 for the omitted value. Leaving out both (\033[H) will move the cursor to the origin.
xG Move cursor to column x
x;yH Same as f (see above).
J or 0J (Yes, that's a zero.) Clears the screen from the cursor onwards.
1J Clears the screen from the origin up to the cursor.
2J Clears the entire screen. MSDOS will also move the cursor to the origin.
J or 0K (Zero again.) Clears the current line from the cursor onwards.
1K Clears the current line from the origin up to the cursor.
2K Clears the entire line.
xS Scrolls the entire screen up by x lines. Assumes 1 if x is omitted.
xT Scrolls the entire screen down by x lines. Also assumes 1.
Yep, we can do a lot with this. Here's how you can simulate a text box at row 5, column 1:
setxy() {
echo -en "\033[$1;$2H" ;
}
#set position
setxy(5,1)
#white background, black text
echo "\033[30;47m \033[0m"
#move back to beginning of textbox
setxy(5,1)
#read a string in
read VALUEOFTEXTBOX
Other Cool Stuff I Don't Have Experience Doing
- Set the console video mode to give yourself more colors, control the resolution, etc.
- You can remap keys, i.e. assign F1 to be the same as F9, or even set the L key to type "CodeCall" every time you hit it. (Great way to piss of friends.)
- According to Wikipedia, there's a way to read the cursor position by using either 6n or x;yR - it'd be as if the row and column were typed at the console. I've never done this, nor do I know if it's standard. But it's a thing to try, if you have the time.
References
Wikipedia - ANSI Escape Codes
ASCII Table and ANSI Escape Sequence Reference


Sign In
Create Account

Back to top











