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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With