How to Output Coloured Text to a Linux Terminal

How do I output coloured text to a Linux terminal?

You need to output ANSI colour codes. Note that not all terminals support this; if colour sequences are not supported, garbage will show up.

Example:

 cout << "\033[1;31mbold red text\033[0m\n";

Here, \033 is the ESC character, ASCII 27. It is followed by [, then zero or more numbers separated by ;, and finally the letter m. The numbers describe the colour and format to switch to from that point onwards.

The codes for foreground and background colours are:

         foreground background
black 30 40
red 31 41
green 32 42
yellow 33 43
blue 34 44
magenta 35 45
cyan 36 46
white 37 47

Additionally, you can use these:

reset             0  (everything back to normal)
bold/bright 1 (often a brighter shade of the same colour)
underline 4
inverse 7 (swap foreground and background colours)
bold/bright off 21
underline off 24
inverse off 27

See the table on Wikipedia for other, less widely supported codes.


To determine whether your terminal supports colour sequences, read the value of the TERM environment variable. It should specify the particular terminal type used (e.g. vt100, gnome-terminal, xterm, screen, ...). Then look that up in the terminfo database; check the colors capability.

How to use ANSI color codes to print colored text to bash terminal from a file?

How would I read a file, color its contents and then print to the terminal?

You could leave the expansions in the file and replace the ${...} forms with the variables when reading the file:

RED=$'\E[0;31m'
NC=$'\E[0m'
GREEN=$'\E[0;32m'
sed 's/No/${RED}No${NC}/g ; s/Yes/${GREEN}Yes${NC}/g' file.txt > color_file.txt
( export RED NC GREEN ; envsubst color_file.txt )

But I believe you wanted to actually output the escape sequences into the file:

RED=$'\E[0;31m'
NC=$'\E[0m'
GREEN=$'\E[0;32m'
sed "s/No/${RED}No${NC}/g ; s/Yes/${GREEN}Yes${NC}/g" file.txt > color_file.txt
cat color_file.txt

Or maybe you wanted printf to interpret the escpe sequences in the content of a file:

RED='\033[0;31m'
NC='\033[0m'
GREEN='\033[0;32m'
sed "s/No/${RED}No${NC}/g ; s/Yes/${GREEN}Yes${NC}/g" file.txt > color_file.txt
printf "%b\n" "$(cat color_file.txt)"
# or maybe:
xargs -0 printf "%b" < color_file.txt
# or maybe:
sed 's/\\033/\e/g' color_file.txt
# etc.

Check your scripts with shellcheck - it will tell you about all mistakes that you did.

's/No/${RED}...'

Variable expansion does not expand within single quotes.

#If you would:
RED='\033[0;31m'
.... sed "....${RED}..."

sed does not interpret octal escape sequences. For sed the string \033 is a zero byte 0x00 followed by two 33.

printf "$(cat ...)"

Is an antipattern. Just execute cat ....

cat ... | sed..

Is a useless use of cat.

how to make printf read the content of the file

It is not possible to read a file with printf. It's not a tool for that.

and therefore execute the commands within it.

This feels unrelated to your issue, but to execute a file just call it with an interpreter sh ./file.sh or source it within current execution environment . ./file.sh.

PolyML colored output to terminal in Linux

\033 is a character escape sequence which in bash and many other languages is interpreted as the character with ASCII code corresponding to the octal number 33.

In OCaml however, this escape sequence is interpreted as decimal. We can either convert the number from octal (33) to decimal (27) and continue to use this syntax, or use the correct syntax for octal escape sequences (\o027). Or we could even use hex (\x1b) if we want to be a bit more adventurous.

All of these will work in OCaml, and possibly also PolyML (if you replace Printf.printf with print of course):

Printf.printf "\027[31m RED \027[0m NORMAL \n";;
Printf.printf "\o033[31m RED \o033[0m NORMAL \n";;
Printf.printf "\x1b[31m RED \x1b[0m NORMAL \n";;

Source: The OCaml manual Chapter 9.1: Lexical conventions

Color text in terminal applications in UNIX

This is a little C program that illustrates how you could use color codes:

#include <stdio.h>

#define KNRM "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KBLU "\x1B[34m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define KWHT "\x1B[37m"

int main()
{
printf("%sred\n", KRED);
printf("%sgreen\n", KGRN);
printf("%syellow\n", KYEL);
printf("%sblue\n", KBLU);
printf("%smagenta\n", KMAG);
printf("%scyan\n", KCYN);
printf("%swhite\n", KWHT);
printf("%snormal\n", KNRM);

return 0;
}


Related Topics



Leave a reply



Submit