You can use
I'm not sure [that
tput is] shipped with all major distros
It's part of ncurses, and packaged with it on all three of those operating systems and more.
tput setaf will work across a broad range of systems, with the obvious proviso that the requisite package needs to be installed.
The downside is that using it still ends up employing "non-word colour codes", as the numbers that one supplies are the 8 standard colours indices from ECMA-48 (with a possible extra 8 de facto standard colours on some terminal emulators, and up to 256 colours by using ISO 8613-6 Indexed colour on some).
On the gripping hand, the 8 standard ECMA-48 colours are well known, and what (for example) "6" means as a colour number is not all that obscure.
You can also use
However, if you are looking for more human-readable commands that use names rather than numbers, albeit in English only, there are widespread tools that can do that, too. One such is
setterm, which is used like this:
% setterm --foreground red ; echo "this is red text"
setterm that you will find in the repositories of Debian, Fedora, and Arch is the one from the util-linux toolset. That command relies upon various Linux-specific things for some of the other actions that the command can perform, and it of course is not available on the BSDs. The toolset is Linux-specific and not portable, as its name tells one.
Also, whilst its manual page claims that the terminfo database is consulted, changing colours is one of quite a few cases where the actual program does not work as documented. It does not consult the
setab capabilities in terminfo. Rather, the program hardwires the ECMA-48 SGR 30–37,39,40–47,49 control sequences.
Moreover, it does not support the additional 8 de facto standard colour indices from 8 to 15. It hasn't been kept up to date with the capabilities of the Linux KVT, which does support the "bright" colours, and which nowadays at least accepts ISO 8613-6 Indexed and Direct colour control sequences (albeit that it does not actually implement full 24-bit colour).
Furthermore, because of a bug in that very hardwiring code, the version of util-linux
setterm in Debian Stable does not emit the right SGR sequences when setting colours.
setterm. User Commands. Michael Kerrisk. 2014-05.
… even on the BSDs, now.
Finding that bug was one of the things that inspired me to write a
setterm replacement, which (coincidentally) I have just done. I didn't copy the (moderate amount of) stuff that was idiosyncratic to Linux and to its kernel virtual terminals. And conversely I did add some ECMA-48 stuff that has been around for several decades that the util-linux
setterm does not have (because the Linux KVT did not support that part of ECMA-48, and
setterm has its roots in being specific to the Linux KVT). I also added UTF-8, support for the actual 8-bit C1 control characters (as well as their 7-bit aliases), and ISO 8613-6 Direct colour (if the terminal type is known to support it).
And of course, this implementation does exist for the BSDs:
% uname ; printenv TERM
% TERM=ansi setterm --7bit --foreground red|hexdump -C
00000000 1b 5b 33 31 6d |.[31m|
% setterm --7bit --foreground red|hexdump -C
00000000 1b 5b 33 38 3b 35 3b 31 6d |.[38;5;1m|
% setterm --7bit --foreground 192|hexdump -C
00000000 1b 5b 33 38 3b 35 3b 31 39 32 6d |.[38;5;192m|
% setterm --7bit --foreground '#00BEC119'|hexdump -C
00000000 1b 5b 33 38 3b 35 3b 31 39 31 6d |.[38;5;191m|
% TERM=vte-256color setterm --7bit --foreground '#00BEC119'|hexdump -C
00000000 1b 5b 33 38 3b 32 3b 31 39 30 3b 32 35 3b 31 39 |.[38;2;190;25;19|
00000010 33 6d |3m|
% TERM=ansi setterm --7bit --foreground 'bright red' --background 'bright yellow'|hexdump -C
00000000 1b 5b 39 31 6d 1b 5b 31 30 33 6d |.[91m.[103m|
% setterm --7bit --foreground 'bright red' --background 'bright yellow'|hexdump -C
00000000 1b 5b 33 38 3b 35 3b 39 6d 1b 5b 34 38 3b 35 3b |.[38;5;9m.[48;5;|
00000010 31 31 6d |11m|
It is in version 1.39 of the nosh toolset, in the terminal management package with the name
console-control-sequence, allowing one to have this side-by side with util-linux. The
setterm shim name for the command is provided by a separate shims package. So if you write something using
setterm to change colours and someone claims that that is Linux-specific, you know where to direct them. I've put the manual page up as a preview.
- Jonathan de Boyne Pollard (2018).
setterm. nosh Guide. Softwares.
Get the SGR codes right, by the way.
SGR 0 is not "no colour". SGR 0 resets all colours and attributes. There actually is not a concept of "no" colour. There is a concept of the "default" colour, set by SGR 39 and SGR 49:
% setterm --7bit --foreground default --background default|hexdump -C
00000000 1b 5b 33 39 6d 1b 5b 34 39 6d |.[39m.[49m|