On 8/29/21 8:13 PM,
[email protected] wrote:
The following command will print red text output on terminal:
$ echo -e '\e[31mRED TEXT\e[m'
RED TEXT
But why it uses such a strange syntax, and how to understand/remember
it conveniently?
It's not strange once you have a vague understanding of what's happening.
The pair of bytes <Escape> followed by <[> (0x1B and 0x5B respectively)
is what's known as the Control Sequence Introducer. The CSI tells
terminals that support it that a control sequence is immediately
following the CSI.
The 31m is a Character Attribute control sequence. (I.I. lists it as
SGR). The 31 sets the foreground to red. The m is the end of the
Character Attribute control sequence.
The "RED TEXT" is just normal text that is using the current text state,
which happens to be red.
Next the CSI Character Attribute is used to change text attributes back
to (implicit) 0 which is normal / default.
This means that you're echoing a CSI+CA to change to red, followed by
your "RED TEXT", followed by a CSI+CA to reset back to defaults.
The "-e" in the echo is how you tell (that version of) echo that "\e"
should be interpreted as escape (0x1B).
My preferred reference is invisible-island's XTerm Control Sequences
page --
https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
Another very good reference is VT100's Select Graphic Rendition page --
https://vt100.net/docs/vt510-rm/SGR.html
Aside: CSI is /almost/ always <Escape>+<[>. However I have run into a
few systems / terminals that use a different CSI. One or both of the
links that I provided will talk about the pair that is used for CSI.
As for remembering this, well, once you've done it a few times, you'll
pick up on the fact that it's the <CSI><SGR>text<CSI><SGR> format.
You'll also have an idea what to look up and where to look it up.
There are a lot of things that talk about the SGR options, particularly
colors. Fewer will talk about the different things that you can do with
fonts. -- Both of the sites I linked to will talk about fonts. It's a
matter of what your terminal (emulator) supports. -- You'll find that
there are few sites that talk about more of the fancier things that you
can do. }:-) Double height and / or double width text is ... something
to behold. As are Sixel and ReGIS graphics in a text terminal. (No
Unicode pseudo graphics via Braille characters here.)
--
Grant. . . .
unix || die
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)