Different Color for command and output

You can pass the output to a program which does the colouring. For example, there are programs like pycolor:

pycolor module.py

To build your own:

red="$(tput setaf 1)"
reset="$(tput sgr0)"
echo $'foo\nbar' | sed -e "s/\(bar\)/$red\1$reset/"

More on colour output.


Not really; the color of a given program's output is controlled by the command, not by the terminal.

That said, assuming your terminal understands ANSI escape codes for color (most do), you could use escape codes to set your desired prompt color at the beginning of the prompt, and your desired output color at the end; this would result in your command lines also being in whatever color you set for output, but that's probably about as close as you're going to get to what you're looking for. For example, the following:

$ export PS1='\[\033[00;35m\]\u\[\033[00m\]@\[\033[00;35m\]\H\[\033[00m\]:\[\033[00;33m\]\W\[\033[00m\] \$\[\033[00;34m '

will give you a prompt that looks like:

user@host:wd $ _

with 'user' and 'host' in purple, 'wd' (your cwd) in brown (dark yellow), and everything after the '$ ' in whatever your terminal uses for light blue. (A default xterm will render this in cyan; gnome-terminal seems to default to a rather nice shade of cornflower blue.)

The actual color code, as specified in the necessary format for bash, is, e.g., \[\033[00;35m\], where 00;35 is the actual color specification; the leading 00 rarely changes, but can produce IIRC bold (01), underline (??), and reverse video (??) effects, while the trailing '35' is the actual color, which in this case is purple.

It's surprisingly hard to find a decent list of ANSI color codes, but foreground colors run from 30 through 37, and background ones from 40 through 47, as follows:

color        fg  bg
black        30  40
red          31  41
green        32  42
yellow       33  43
blue         34  44
purple       35  45
cyan         36  46
gray/white   37  47

Do keep in mind that, since you're setting a default color for everything that follows your prompt, programs you run which don't set their own colors via escape codes are going to take that color -- for example, if you run a pager, you're likely to see its output in the same color you've set for other commands. Meanwhile, commands which do set their own colors via escape codes will ignore your efforts entirely.


Yes I have this working for bash, on linux and mac OS at least.

First, set some attribute variables, and the PS1 prompt to leave the terminal set with the desired command-line attributes, for example:

bold="\[$(tput bold)\]"
none="$(tput sgr0)"
export PS1="\$ $bold"

Then, use the bash DEBUG trap, which gets executed just before every command, to change text attributes for command output:

trap 'echo -ne "${none}"' DEBUG