I was wondering if it is possible to change the color of the output of a command in linux to a different color than that of the command line where I type in. It is som monotonous and hard to find where the output starts. like,

maybe using precmd_functions you could change prompt color and set it back before preexec_functions, or something like it. (in zsh, i don't know if there's an equivalent for other shells)
–
patalucJun 20 '13 at 15:01

i just tried my suggestion but i don't get the command coloured... :(
–
patalucJun 20 '13 at 15:08

1

@pataluc: I succeeded with preexec () { echo -n "\\e[40;34m" }; the precmd function is IMHO not necessary, it seems zsh resets the color itself before displaying the prompt. But that works only for those commands that won't alter the color itself (prominent example is ls) as pointed out by Aaron. With cat, bc, builtin shell commands, etc. (even alpine) this works fine.
–
mpyJun 20 '13 at 15:21

@mpy i succeeded also, with all commands, assuming raj wants to color the command and not the result, i added "\\e[40;34m" to the end of my PROMPT var, and set it back to white with preexec () { echo -n "\\e[40;37m" }...
–
patalucJun 20 '13 at 15:38

@pataluc: Sorry, now I get the point. To color the command line just use zle_highlight=(default:fg=cyan) with a recent zsh version. Zsh also offers dynamic coloring, like fish; see the CHARACTER HIGHLIGHTING section in man zshzle.
–
mpyJun 20 '13 at 15:56

4 Answers
4

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:

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:

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.

Instead of escape codes, I've found that using tput is much more readable for colorizing. For example, my prompt (in bash) is export PS1="[\t \[$(tput setaf 9)\]\u\[$(tput setaf 15)\]@\[$(tput setaf 12)\]\h \[$(tput sgr0)\]\w ] \$ " which outputs the time, then the username in one color, @ in another, and hostname in a third (red, white, and blue at the moment), then prints the PWD and $ . It's very easy to see at a glance where the prompt lines are when scrolling back through the history.
–
MattDMoJun 20 '13 at 16:54

u r awesome...thats all I ever wanted. Thanks could you tell me what values to change if I ever want to change colors?
–
rajJun 20 '13 at 19:53

and is there a way to keep the last one the same as the color had before?
–
rajJun 20 '13 at 19:58

and why isn't the change permenant, every time I log in, I have to color it again
–
rajJun 20 '13 at 20:15

1

The change isn't permanent unless you add it to your shell's initialization file, which for bash is ~/.bashrc; find the line where it sets PS1, which is your prompt string, and modify it as described in the answer. What to change depends on how your prompt string is set up, but most of the salient details are in my answer, and the rest can be found by doing a little Googling. I'm not sure what you mean by "is there a way to keep the last one the same as the color had before?"
–
Aaron MillerJun 20 '13 at 20:53