Bash ansi escape codes: Difference between revisions

From wikinotes
 
 
(6 intermediate revisions by the same user not shown)
Line 4: Line 4:


= Colours =
= Colours =
<blockquote>
== Termcap Database (portable-ish) ==
<blockquote>
<blockquote>
See list of colours https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
See list of colours https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
tput looks up escape codes within the termcap database.<br>
using tput ensures your code is portable across the unixes.
<source lang="bash">
<source lang="bash">
echo -e '\e[32m'    #  
echo '\e[1m'      # bold
echo -e '\e[m'      # reset colour
echo '\e[32m'    # color '2'
echo '\e[m'      # reset colour
# or assign to var
blue=$(echo -e '\e[36m')


tput setab 7 # set bg colour
tput setab 7 # set bg colour
Line 18: Line 26:
echo "${yellow}foo"
echo "${yellow}foo"
</source>
</source>
I usually do something like this for help text
<syntaxhighlight lang="bash">
setup_colours() {
    if [ $(tput colors) -gt 8 ] ; then
        C=$(tput setaf 6)  # code
        H=$(tput setaf 3)  # heading
        E=$(tput setaf 1)  # error
        B=$(tput bold)    # bold
        R=$(tput sgr0)    # reset
    else
        C=$(tput sgr0)
        H=$(tput sgr0)
        E=$(tput sgr0)
        B=$(tput sgr0)
        R=$(tput sgr0)
    fi
}
</syntaxhighlight>
</blockquote><!-- Termcap Database (portable) -->
== VT220(?) non-portable ==
<blockquote>
The non-portable way of handling this is as follows (but most terminals use this set of escape codes).
<syntaxhighlight lang="bash">
ESC='\033'
RED="${ESC}[31m"
GREEN="${ESC}[32m"
RESET="${ESC}[0m"
echo "\
${RED}red\
${GREEN}green\
${RESET}reset"
</syntaxhighlight>
</blockquote><!-- VT220(?) non-portable -->
</blockquote><!-- Colours -->
</blockquote><!-- Colours -->

Latest revision as of 02:01, 19 January 2024

Terminals are controled ansi escape sequences.
These sequences vary from terminal to terminal.
The tput command uses your terminal's termcap library to expose a common interface.

Colours

Termcap Database (portable-ish)

See list of colours https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit

tput looks up escape codes within the termcap database.
using tput ensures your code is portable across the unixes.

echo '\e[1m'      # bold
echo '\e[32m'     # color '2'
echo '\e[m'       # reset colour
# or assign to var
blue=$(echo -e '\e[36m')

tput setab 7 # set bg colour
tput setaf 7 # set fg colour
tput bold    # set bold
tput sgr0    # reset colours

yellow=$(tput setaf 3)
echo "${yellow}foo"

I usually do something like this for help text

setup_colours() {
    if [ $(tput colors) -gt 8 ] ; then
        C=$(tput setaf 6)  # code
        H=$(tput setaf 3)  # heading
        E=$(tput setaf 1)  # error
        B=$(tput bold)     # bold
        R=$(tput sgr0)     # reset
    else
        C=$(tput sgr0)
        H=$(tput sgr0)
        E=$(tput sgr0)
        B=$(tput sgr0)
        R=$(tput sgr0)
    fi
}

VT220(?) non-portable

The non-portable way of handling this is as follows (but most terminals use this set of escape codes).

ESC='\033'
RED="${ESC}[31m"
GREEN="${ESC}[32m"
RESET="${ESC}[0m"

echo "\
${RED}red\
${GREEN}green\
${RESET}reset"