Alex Rivera | Logout

How to set font color for STDOUT and STDERR

Asked 2011-07-27T07:57:18.157
68

I want to differentiate the STDOUT and STDERR messages in my terminal. If a script or command is printing a message in terminal I want to differentiate by colors; is it possible?

(E.g. stderr font color is red, and stdout font color is blue.)

Example (using bold):

$date
Wed Jul 27 12:36:50 IST 2011

$datee
bash: datee: command not found

$alias ls
alias ls='ls --color=auto -F'

$aliass ls
bash: aliass: command not found

Edit
Report

3 Answers

33

Here's a hack that I thought of and it seems to work:

Given the following aliases for readability:

alias blue='echo -en "\033[36m"'
alias red='echo -en "\033[31m"'
alias formatOutput='while read line; do blue; echo $line; red; done'

Now, you need to first set the font color in your terminal to red (as the default, which will be used for stderr). Then, run your command and pipe the stdout through formatOutput defined above (which simply prints each line as blue and then resets the font color to red):

shell$ red
shell$ ls / somenonexistingfile | formatOutput

The above command will print in both stderr and stdout and you'll see that the lines are coloured differently.

Hope this helps


UPDATE:

To make this reusable, I've put it all in a small script:

$ cat bin/run 
#!/bin/bash
echo -en "\033[31m"  ## red
eval $* | while read line; do
    echo -en "\033[36m"  ## blue
    echo $line
    echo -en "\033[31m"  ## red
done
echo -en "\033[0m"  ## reset color

Now you can use this with any command:

$ run yourCommand
answered 2011-07-27T11:52:46.327
10

You should check out stderred: https://github.com/sickill/stderred

answered 2011-12-13T21:47:18.907
4

Yes it's not possible natively. You'll have to hack the tty management (in the kernel).

I somehow finished some little C wrapper before I saw the other answers :-) Might be buggy, and values are hardcoded, don't use this except for testing.

#include "unistd.h"
#include "stdio.h"
#include <sys/select.h>

int main(int argc, char **argv)
{

char buf[1024];
int pout[2], perr[2];
pipe(pout); pipe(perr);

if (fork()!=0)
{
    close(1); close(2);
    dup2(pout[1],1); dup2(perr[1],2);
    close(pout[1]); close(perr[1]);
    execvp(argv[1], argv+1);
    fprintf(stderr,"exec failed\n");
    return 0;
}

close(pout[1]); close(perr[1]);

while (1)
{
    fd_set fds;
    FD_ZERO(&fds);
    FD_SET(pout[0], &fds);
    FD_SET(perr[0], &fds);
    int max = pout[0] > perr[0] ? pout[0] : perr[0];
    int v = select(max+1, &fds, NULL, NULL, NULL);
    if (FD_ISSET(pout[0], &fds))
    {
        int r;
        r = read(pout[0], buf, 1024);
        if (!r) {close(pout[0]); continue;}
        write(1, "\033[33m", 5);
        write(1, buf, r);
        write(1, "\033[0m", 4);
    }
    if (FD_ISSET(perr[0], &fds))
    {
        int r;
        r = read(perr[0], buf, 1024);
        if (!r) {close(perr[0]); continue;}
        write(2, "\033[31m", 5);
        write(2, buf, r);
        write(2, "\033[0m", 4);
    }

    if (v <= 0) break;
}

return 0;
}

Edit: Compared to the shell solution, this one will preserve the order of lines/characters more often. (It's not possible to be as accurate as direct tty reading.) Hitting ^C won't show an ugly error message, and it behaves correctly on this example:

./c_color_script sh -c "while true; do (echo -n a; echo -n b 1>&2) done"
answered 2011-07-27T12:40:43.707

Your Answer