Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This isn't the biggest deal in the world but when making a shell script to modify the output a bit from the ping command, I use red and green color for up or down host status via /e[0;xxm.
It works well for displaying in the shell but when I >> to a file I don't see the color I see the ^[[0;31m and not the color.
Anything I can do to fix that lil headache?
I'm invoking color in the script via a variable RED='\e[0;xxm' and calling it in my echo as ${RED}.
Last edited by Zethien; 11-23-2016 at 10:41 AM.
Reason: syntax errors
I think this depends a bit on the file. For instance I do not believe a simple text file would display any colors; however there are various document format types, such as ODT or HTML, maybe XML(?), where you can add color attributes and be able to see those when you load the document in the appropriate reader.
The color sequences only have meaning to a color terminal. Ctrl-[ and '\e' are both names for the same ASCII value 27. I suspect if you cat the file it will show red. You are seeing ^[[0;31m when you look at the file with an editor or some other program that doesn't know how to interpret the color sequence.
if you do a "cat file" in a terminal, you should see the colors again.
ultimately you have to ask yourself, what is the goal of the script? where should its output show up? maybe you have to use html or pango instead of shell color escape sequences.
This isn't the biggest deal in the world but when making a shell script to modify the output a bit from the ping command, I use red and green color for up or down host status via /e[0;xxm.
It works well for displaying in the shell but when I >> to a file I don't see the color I see the ^[[0;31m and not the color.
Anything I can do to fix that lil headache?
I'm invoking color in the script via a variable RED='\e[0;xxm' and calling it in my echo as ${RED}.
That is not a headache, that is to be expected.
To expand upon what others have already said: bash does not do colors, (some) terminals do colors. When you use bash to echo screen control codes appropriate for the terminal type you can display colors, but it is the terminal that is converting the codes into colors. Pipe that to a file and look at the file and you will see the text (including codes) that generated the colors because they are not being sent to the terminal in a way that it will interpret.
My personal solution: you can output html with color codes so that any browser will interpret the colors, but then you must be prepared to look at the output using a browser. VI or any other text editor will see the html text, but any browser will display with color, formatting, etc. (I generate my web pages this way, using scripts that output the html and css.)
I question if that would be worth your time.
Rather than modifying the output of the ping, why not detect the results of the ping and output your own "success" or "FAIL" text (colored or not) to simplify the problem. If up or down status is all you need, all of the other ping verbiage is wasted anyway.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.