• Display color output in the script

    From hongyi.zhao@gmail.com@21:1/5 to All on Sun Aug 29 19:13:51 2021
    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?

    Regards,
    HY

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From hongyi.zhao@gmail.com@21:1/5 to Janis Papanagnou on Sun Aug 29 19:42:21 2021
    On Monday, August 30, 2021 at 10:29:18 AM UTC+8, Janis Papanagnou wrote:
    On 30.08.2021 04:13, hongy...@gmail.com wrote:
    The following command will print red text output on terminal:

    $ echo -e '\e[31mRED TEXT\e[m'
    This is no portable shell syntax. My Kornshell, for example, wants

    echo $'\e[31mRED TEXT\e[m'
    or
    printf "\e[31mRED TEXT\e[m"

    They are all work in bash too.

    RED TEXT

    But why it uses such a strange syntax,
    The '\e' character is ESC and '[' introduces the CSI; You may find
    more detailled answers for CSI (Control Sequence Introducer) here: https://en.wikipedia.org/wiki/ANSI_escape_code
    and how to understand/remember it conveniently?
    Look it up in tables (e.g. on the page linked above).

    Janis


    Regards,
    HY


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to hongy...@gmail.com on Mon Aug 30 04:29:13 2021
    On 30.08.2021 04:13, hongy...@gmail.com wrote:
    The following command will print red text output on terminal:

    $ echo -e '\e[31mRED TEXT\e[m'

    This is no portable shell syntax. My Kornshell, for example, wants

    echo $'\e[31mRED TEXT\e[m'
    or
    printf "\e[31mRED TEXT\e[m"


    RED TEXT

    But why it uses such a strange syntax,

    The '\e' character is ESC and '[' introduces the CSI; You may find
    more detailled answers for CSI (Control Sequence Introducer) here: https://en.wikipedia.org/wiki/ANSI_escape_code

    and how to understand/remember it conveniently?

    Look it up in tables (e.g. on the page linked above).

    Janis


    Regards,
    HY


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to hongy...@gmail.com on Mon Aug 30 05:35:39 2021
    On 30.08.2021 04:42, hongy...@gmail.com wrote:
    On Monday, August 30, 2021 at 10:29:18 AM UTC+8, Janis Papanagnou wrote:
    On 30.08.2021 04:13, hongy...@gmail.com wrote:
    The following command will print red text output on terminal:

    $ echo -e '\e[31mRED TEXT\e[m'
    This is no portable shell syntax. My Kornshell, for example, wants

    echo $'\e[31mRED TEXT\e[m'
    or
    printf "\e[31mRED TEXT\e[m"

    They are all work in bash too.

    So better use a more widely portable form; printf is often suggested.

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Grant Taylor@21:1/5 to hongy...@gmail.com on Sun Aug 29 22:28:58 2021
    On 8/29/21 8:13 PM, hongy...@gmail.com 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)
  • From Grant Taylor@21:1/5 to Grant Taylor on Sun Aug 29 22:31:12 2021
    On 8/29/21 10:28 PM, Grant Taylor wrote:
    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.)

    Once you go far enough down the rabbit hole of what you can do with
    control sequences, you may end up doing something like checking the
    answer back from terminal (emulators) to see if it matches a configured
    list to play with different capabilities. Then you can do really fancy
    things like having Zsh (or maybe even Bash) display a small graphic
    (again, not Unicode cheat) that is a yield / stop sign if $? of the last command was > 0. ;-)



    --
    Grant. . . .
    unix || die

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eli the Bearded@21:1/5 to hongy...@gmail.com on Mon Aug 30 04:33:30 2021
    In comp.unix.shell, hongy...@gmail.com <hongyi.zhao@gmail.com> 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?

    I use tput commands to emit them easily / conveniently. tput looks up
    the sequence from the terminal definition and substitutes variables
    (when needed) then emits the escape sequence.

    Use "setaf" for set ansi foreground or "setab" for set ansi background,
    and a color code from 0 to 7.

    red_fg=$(tput setaf 1) # red foreground
    white_fg=$(tput setaf 7) # white foreground
    yellow_bg=$(tput setab 3) # yellow background
    black_bg=$(tput setab 0) # yellow background
    revert=$(tput sgr0) # revert to defaults

    echo "${red_fg}${yellow_bg}red on yellow${revert}"
    echo "${white_fg}${black_bg}white on black${revert}"

    Handy code cheat sheet (nothing bash specific about it): https://cheatography.com/chrissoper1/cheat-sheets/bash-script-colors/

    More information in terminfo and tput man pages.

    Elijah
    ------
    more likely to use bold or underlining than colors

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)