How to set font color for STDOUT and STDERR

zsh color stderr
change color of output linux
bash change color output
change color of echo text
change terminal output color bash
how to change font color in linux shell
bash color output
add color to echo output

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

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

unix - How to set font color for STDOUT and STDERR, 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"'  You can, technically, color STDOUT and STDERR, line by line, with adding the right color escape codes to start and end of line on both streams. You would need to take care to reset color at least each line, because otherwise any program using color, like ls , would mess up your terminal .

How to set font color for STDIN,STDOUT and STDERR?, 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  2>&1 — Redirects the stderr of the command to stdout so that it becomes sed 's stdin. >&3 — Shorthand for 1>&3, this redirects stdout to a new temporary file descriptor 3. 3 gets routed back into stdout later. sed — Because of the redirects above, sed 's stdin is the stderr of the executed command.

I color stderr red by linking the file descriptor to a custom function that adds color to everything that goes through it. Add to following to your .bashrc:

export COLOR_RED="$(tput setaf 1)"
export COLOR_RESET="$(tput sgr0)"

exec 9>&2
exec 8> >(
    perl -e '$|=1; while(sysread STDIN,$a,9999) {print 
"$ENV{COLOR_RED}$a$ENV{COLOR_RESET}"}'
)
function undirect(){ exec 2>&9; }
function redirect(){ exec 2>&8; } 
trap "redirect;" DEBUG
PROMPT_COMMAND='undirect;'

So what is happening? The debug trap is executed just before and immediately after executing a command. stderr is thus redirected before a command is executed to enable red output. PROMPT_COMMAND is evaluated before the prompt is shown and with this I restore stderr to its normal state. This is necessary because PS1 and PS2 (your prompt) are printed over stderr and I do not want a red prompt. voila, red output over stderr!

Can I configure my shell to print STDERR and STDOUT in different , Terminals use color changing escape sequences to determine the display color, order the text written by the application on stdout and stderr will be preserved,  Even if it's the same process that processes the two pipes, there's no guarantee that the order the text written by the application on stdout and stderr will be preserved, as the reading process can't know (if there's something to be read from both) whether to start reading from the "stdout" pipe or the "stderr" pipe.

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

How to set font color for STDOUT and STDERR - unix - csharp, 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.)  2>&1 — Redirects the stderr of the command to stdout so that it becomes sed's stdin. >&3 — Shorthand for 1>&3, this redirects stdout to a new temporary file descriptor 3. 3 gets routed back into stdout later. sed — Because of the redirects above, sed's stdin is the stderr of the executed command.

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"

bash: print stderr in red color, The bash way of making stderr permanently red is using 'exec' to redirect I have posted on this previously: How to set font color for STDOUT and STDERR. If you want to change the colors directly yourself without a module try. console.log('\x1b[36m', 'sometext' ,'\x1b[0m'); First \x1b[36m to change the colors to 36 and then back to terminal color 0. Here's a list of ANSI color codes

Utilities, Click now emulates output streams on Windows to support unicode to the Windows Starting with Click 3.0 you can also easily print to standard error by passing err=True : Click's echo() function will automatically strip ANSI color codes if the stream is not connected to a terminal. To install colorama , run this command:. Since Python is interpreted and run in C, it is possible to set colors without a module. You can define a class for colors like this:

termcolor - Rust, This crate provides a cross platform abstraction for writing colored text to a terminal. A BufferWriter can create buffers and write buffers to stdout or stderr. Change text style. After installing the colorama package, you can use it inside your Python programs like this: import colorama # If using Windows, init() will cause anything sent to stdout or stderr # will have ANSI color codes converted to the Windows versions. Hooray! # If you are already using an ANSI compliant shell, it won't do anything

colorama · PyPI, Cross-platform colored terminal text. pip install colorama. Copy PIP This will restore stdout and stderr to their original values, so that Colorama is disabled. I am trying to print stderr and stdout into one variable. I want to check if rtest exists and capture the output, so i can exit the script if it isnt found. checkdate=`rtest 2&>1` The problem is that if i run my script stderr will still get printed on the terminal../script.ksh[26]: rtest: not found

Comments
  • I doubt it's possible. Maybe with patching bash source.. but even that isn't trivial.
  • No, it's not possible natively. Here is a link to a serverfault question though, that solves it with a hack.
  • -1 because you accepted the hack, and it has problems, rather than the excellent solution by russ.
  • More colour options here: stackoverflow.com/a/12827233/1120248
  • @HeathHunnicutt - Who is russ? And how can the op accept an answer for a question that isn't his/hers?
  • while using pipe , if the second command is wrong means it's printing in blue color only not in red. example run ps | grepp run
  • You need to invoke each command as run <cmd>. In your example, grepp is not invoked through run. Try this: run ps | run grepp run.
  • @ungalnanban: If you really want to execute ps | grepp run through the run script, you simply need to put that in quotes: run "ps | grepp run".
  • Notice the color mismatch on this example run "while true; do (echo a; echo b 1>&2) done". See also the comment below the C solution.
  • @Stéphane Gimenez: Yep, you're right. I did say it's just a hack. Also, another drawback that I spotted is that the error messages tend to be displayed first, compared to running the same command without the run script. However, you have my upvote for your solution.
  • russ I wish I could give you a huge bounty for this. +1
  • +1 I must admit, your answer is better than mine (but I cannot transfer its acceptance). And your legend is something I would put on a T-shirt. :)
  • @killdash9 - Thanks for sharing the script. I have found that it consistently swaps the order of some stdout and stderr when they are interleaved. Is there easy fix for this? I will post one here if I figure one out.