9

This question is similar to Can I configure my shell to print STDERR and STDOUT in different colors?

But I am looking for a solution that will allow me to change both STDOUT and STDERR to ONE color. I am not trying to set different color for STDOUT and STDERR, as this is problematic and may create problems, mentioned in the answer from the link above.

I apologize, as I don't know much about Unix and Linux. I am looking for a solution that can allow me to add couple lines to my .bashrc or .bash_profile and permanently change the color of STDOUT and STDERR.

3 Answers3

9

STDOUT and STDERR don't have colors. What has color is your terminal (emulator); it has one foreground (and one background color) set at a time.

It should also be noted that STDOUT and STDERR are not singular -- they're per process output streams. There is no global STDOUT that applies to all programs. These streams are routed to your terminal, but they are distinct for each process.

Terminal emulators usually have ways to set the palette via a menu, such that the default foreground (applied to data received via both STDOUT and STDERR streams) could be whatever. You can also apply ANSI color codes in your prompt and if you don't reset the color with \[\033[0m\] at the end, it will continue to apply. Note that some applications use these and reset, so this is not a very ideal method, since the reset will go back to the terminal's default.

If setting the terminal foreground is not what you want, you can filter STDOUT/STDERR on a per process basis; see the suggestions here.

goldilocks
  • 87,661
  • 30
  • 204
  • 262
  • So can you write me a code such that the STDOUT and STDERR will be displayed in grey color everytime I execute a python script please? I want this to be permanent, something that I just add to my .bashrc. – user1769197 Jul 02 '14 at 17:43
  • @user1769197, it's not generally a good idea to mess with the output of programs, since they may output color sequences of their own, which would get messy; also, detecting Java/Python/etc programs is unreliable since some are invoked from wrappers in other languages (typically shell scripts). Your safest, simplest bet is to configure the default fg/bg colors in your terminal application as goldilocks suggested. – arielCo Jul 02 '14 at 18:08
  • So, how should I go about configuring the default foreground and background in my terminal application such that EVERY TIME I run a python script then the output will appear in different color and return to the default fg/bg color after execution? Is this something that I just add onto the .bashrc script and the effect will be permanent ? – user1769197 Jul 02 '14 at 22:53
  • Is this something that I just add onto the .bashrc script and the effect will be permanent? No, there is no such possibility WRT targeting python scripts specifically, etc. That would be a non-existent feature of python. However, you could use a short shell script that sets the color, then executes your command. Setting the color requires echoing ANSI escape codes mentioned in various links and answers (and an intro here, but again, beware that whatever you run can override these). – goldilocks Jul 03 '14 at 01:15
7

With zsh on terminals that support 16 colors or more à la xterm:

preexec() printf '\e[90m' # set foreground color to grey before running
                          # the command

precmd() printf '\e[m' # reset the foreground color before issuing the
                       # next prompt.

Note that commands may change the terminal's foreground color themselves.

1

This seems to be a question with the XY-problem - it's not asking the right thing to get the desired solution:

Assume the output is colored, and the empty part of the screen is of different color or filled with a character.

Think about how you expect the output to look for these cases:

An empty line? Lines filled with only space characters? Lines with one space character only? With one tab? With one space and one tab? With one tab and one space? Yes, they all look different!

Or do you want an empty line look like - just an empty line?
If that's the case, you do not want to color the output streams, really, and this answer applies.


I'll give a similar approach to the solution of @StéphaneChazelas, but "inverse":

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.
You just have not much control over the output color - so it's hard to get right at best.

But: You have control over all other colors used, right?
And you need only one color in this case? Great!

Set the terminals default color to the STDOUT/STDERR color, and color the rest like you prefer.

You can color you shell prompt - and use the prompt code to color other parts also.
Admittedly, it's somewhat tricky to color the command line, where you edit your command, but it's possible certainly. But don't try that with bash if you can use zsh, as the prompt handling code it much more flexible there - that's what you'll need.

Now, set your terminals default foreground color to gray, and possibly background - but try to leave that as transparent/undefined.

To set the background color of the command line, look at this example from Change the background color of command line in zsh when I change the prompt theme:

PROMPT="%{$bg[cyan]%}%{$fg[red]%}%n%{$reset_color%}%{$bg[cyan]%}@%{$fg[red]%}%m %{$fg[yellow]%}%~ %{$reset_color%}%{$bg[cyan]%}%% "

shell prompt example

(The hard part is getting the unused end sections of lins in various situations right.)

Volker Siegel
  • 17,283