Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display colors using cat command

Tags:

linux

bash

colors

I try to create a log file in Bash. I want to have colored text (green for good news, yellow for warning etc.) I am using console codes. When I execute in the terminal the command:

echo -e "\033[1;31m Some text here \033[0m"

I have a good output. My text is colored. The problems start when I want to put such a line in the file. I put a line: \033[1;31m Some text here \033[0m into a file. Then I opened this file using cat command. The output is:

\033[1;31m Some text here \033[0m

This is exactly the same line that I entered into the file. Colors are not displayed. Is it possible to display colored text (console-codes) with the command cat? What I am doing wrong? Thanks for your help!

like image 782
Mikołaj Głodziak Avatar asked Sep 05 '25 03:09

Mikołaj Głodziak


1 Answers

Is it possible to display colored text (console-codes) with the command cat?

Sure. Even with echo -e "\033[1;31m Some text here \033[0m" | cat.

What I am doing wrong?

You are expecting cat to replace \033 (4 characters \ 0 3 3) to be replaced by one byte 0x1b. cat is not tr, it doesn't do translation. If you want cat to output something output the same thing in your file. Instead of inputting \033 (4 characters) into your file, open the file in a hex editor and set the byte to 0x1b (or by other means... just echo -e "\033[1;31m Some text here \033[0m" > file...).

And, it's not a properly of echo nor cat to display colored text. It's your terminal that translates the characters into color - echo and cat only output bytes.

like image 141
KamilCuk Avatar answered Sep 07 '25 20:09

KamilCuk