I am developing a utility in a bash shell script (.sh file), I can generate colors when the output goes to the console, for example with code like:
echo -e "\e[1;33m Aqui el mensaje \e[0m";
But my idea is to create a log file as identical as possible to the output that I emit on the console, for which I would like to have the text in colors in the log file as well. I am using this statement to issue both to the console and to the log file:
echo -e "\e[1;33m Aqui el mensaje \e[0m" 2>&1 | tee --append $nombreArchivo;
The ' tee ' command is what makes it possible for the input stream to go to the console and to the log file.
The problem with this is that in the log file the characters ' \e[1;33m ' for example, are not interpreted, so those characters are written as they are to the file and it looks awful; but I guess if it should be possible because the command ' ls -l 2>&1 | tee --append $filename ' if you write the colors of the ' ls ' command to the file as they are issued by the console.
I guess you have to use a different nomenclature to use the colors, but I don't know it.
There is a small misconception which, I believe, is the cause of the confusion.
The colors of the output
ls -l
that you see in Visual Studio Code are not a product of special command-specific naming that can be replicated from a shell script.You can tell this because if you open the file in a simpler editor the color doesn't show up, plus there are discrepancies in the coloring (in the date column, "2018" is blue and the hours are green; filenames are not colored in their entirety, only the part of
nombre.ext
for those with an extension) and because itls
only colors filenames according to their type (at least in the GNU implementation).What is actually happening is that VS Code is adding syntax highlighting on its own. I don't know what language it's rendering the text in (maybe it's some kind of generic highlighting), but it's definitely not something you can arbitrarily use to your advantage without first modifying the editor itself.
In a previous comment I mentioned that my recommendation was to add custom syntax highlighting to your editor, but on second thought that is not very good advice as it does not apply to arbitrary cases and for every change you make to the script there is a chance that you will also need to modify the highlighted.
Possible solutions depend on the purpose of the log file: will it be processed by other tools or is it intended to be for human consumption only?
If the file is or will be processed by other tools (
awk
,grep
,sed
, etc.): the ideal is to keep it as simple as possible (plain text) to facilitate processing. In this case, the color would be an obstacle.If the file is intended only for people: adapt the output of the log file to the markup language of your choice and make sure you use an appropriate interpreter to display the file. For ANSI escape codes (the ones you used initially), the terminal is the appropriate interpreter (not a plain text editor or IDE).
In the case of HTML, there are terminals that allow you to save the information, color included, in this language (
xfce4-terminal
, for example). Or you could manage everything from the script itself: