Where and How Bash convert strings to colors

216 views Asked by At

I am working on Bash 5.0 from GNU repository. I wanted to find the place where Bash reads a string with ASCII colors and convert it to colors, like in the following case where it convert "Hello" to red:

root@ubuntu:~/Desktop/bash-5.0# ./bash
root@ubuntu:~/Desktop/bash-5.0# echo $BASH_VERSION                                                         
5.0.0(8)-release
root@ubuntu:~/Desktop/bash-5.0# ./bash -c 'echo -e "\033[31mHello\e[0m World"'
Hello World

I searched inside the source code and found two files that seems to be related:

  1. bash-5.0/lib/readline/colors.c - link
  2. bash-5.0/lib/readline/parse-colors.c - link

But they are not, they work only on the first time I load Bash and you need to write the following rows in the file ~/.inputrc for it to work:

set colored-completion-prefix on
set colored-stats on

Any idea where in the code Bash takes string like that "\033[31mHello" and convert it to red?

1

There are 1 answers

0
knittl On BEST ANSWER

It's not the shell that's converting anything to colors, it is your terminal. The shell only outputs ANSI escape codes which are then picked up by the terminal.

Depending on your point of view and philosophical interpretations, \033[31mHello already is a colored string (for the shell, at least, it is)