There is a small misconception which, I believe, is the cause of the confusion.
The colors of the ls -l
output that you see in Visual Studio Code are not the product of a special command-specific nomenclature that can be replicated from a shell script.
You can realize this because if you open the file in a simpler editor the color is not shown, in addition to the discrepancies that are in the coloring (in the date column, "2018" is in blue and the hours in green, file names are not completely colored, only the nombre.ext
part for those with an extension) and because ls
only colors the file names according to their type (at least in the GNU implementation) ).
What actually happens is that VS Code is adding syntax highlighting on its own. < I do not know what language the text is interpreting (maybe it's some kind of generic highlight), but it's definitely not something you can use at your convenience arbitrarily without first modifying the editor itself.
In a previous comment I mentioned that my recommendation was to add a custom syntax highlight to your editor, but thinking about it is not very good advice as it does not apply to arbitrary cases and for every change you make to the script it is possible that you need to modify the highlight.
The possible solutions depend on the purpose of the log file: Will it be processed by other tools or is it intended to be solely for human consumption?
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 only intended for people: adapt the output of the log file to markup language of your preference and make sure you use an appropriate interpreter to view the file. In the case of ANSI escape codes (the ones you originally used), the terminal is the right 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:
#!/usr/bin/env bash
log='log.html'
printf '<!doctype html>\n' >> "${log}"
printf '<html>\n' >> "${log}"
printf '<head>\n' >> "${log}"
printf '<meta charset="utf-8"/>\n' >> "${log}"
printf '<title>¡Hola, mundo!</title>\n' >> "${log}"
printf '</head>\n' >> "${log}"
printf '<body style="background-color: #131926;">\n' >> "${log}"
printf '<pre>' >> "${log}"
# Este texto va hacia la terminal
printf '\e[1;32m¡Hola, mundo!\e[0m\n'
# Este texto va hacia el archivo log
printf '<strong style="color: #4CE64C;">¡Hola, mundo!</strong>\n' >> "${log}"
printf '</pre>\n' >> "${log}"
printf '</body>\n' >> "${log}"
printf '</html>\n' >> "${log}"