Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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,

@USER/Destop$ ----> blue
/*
--------------
output file in white
-----------
*/

Thanks

share|improve this question
2  
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) –  pataluc Jun 20 '13 at 15:01
    
i just tried my suggestion but i don't get the command coloured... :( –  pataluc Jun 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. –  mpy Jun 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" }... –  pataluc Jun 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. –  mpy Jun 20 '13 at 15:56
add comment

4 Answers

up vote 4 down vote accepted

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.

share|improve this answer
    
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. –  MattDMo Jun 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? –  raj Jun 20 '13 at 19:53
    
and is there a way to keep the last one the same as the color had before? –  raj Jun 20 '13 at 19:58
    
and why isn't the change permenant, every time I log in, I have to color it again –  raj Jun 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 Miller Jun 20 '13 at 20:53
add comment

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.

share|improve this answer
add comment

Another alternative (on a command by command basis) is grc - generic colorizer. It takes a bit of work to setup the regular expressions that it uses to colorize things. I found out about it here:

http://unix.stackexchange.com/questions/8414/how-to-have-tail-f-show-colored-output?newsletter=1&nlcode=17737|321c

and here:

http://unix.stackexchange.com/questions/26313/colored-find-output

I'm not a flash with regexes, so I haven't worked with these commands much.

share|improve this answer
    
I use this with all my tail-log scripts. Marvelous. –  stefgosselin Jun 27 '13 at 2:27
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.