17

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

5
  • 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, 2013 at 15:01
  • i just tried my suggestion but i don't get the command coloured... :(
    – pataluc
    Jun 20, 2013 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, 2013 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, 2013 at 15:38
  • 1
    @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, 2013 at 15:56

6 Answers 6

6

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.

6
  • 1
    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, 2013 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, 2013 at 19:53
  • and is there a way to keep the last one the same as the color had before?
    – raj
    Jun 20, 2013 at 19:58
  • and why isn't the change permenant, every time I log in, I have to color it again
    – raj
    Jun 20, 2013 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?" Jun 20, 2013 at 20:53
7

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.

4

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
1

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:

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

and here:

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

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

1
  • I use this with all my tail-log scripts. Marvelous. Jun 27, 2013 at 2:27
1

Throw these into your .bashrc file and then source the file after saving.

blk=$'\x1b[90m' # Sets text to black
red=$'\x1b[31m' # Sets text to red
grn=$'\x1b[92m' # Sets text to green
ylw=$'\x1b[93m' # Sets text to yellow
blu=$'\x1b[94m' # Sets text to blue
pur=$'\x1b[95m' # Sets text to purple
cyn=$'\x1b[96m' # Sets text to cyan
wht=$'\x1b[97m' # Sets text to white
rst=$'\x1b[0m'  # resets to default terminal color

Once this is done, you can then use any of these in your terminal permanently or until you either comment the variables out or delete them completely. You can use them in scripts on your machine or with command substitution or whatever. Here is a following example of how I use them personally:

echo -e "${red}This text is red.${rst}"
printf "${grn}This text is green.\n${rst"

I usually use these with individual echo and/or printf commands but you can use them to colorize other commands as well such as the ping command. Add the following to .bashrc to get the effects. Then source your .bashrc file and run the cping command to use ping with purple text. You can set each command function to use any color your terminal supports.

# Add this to your .bashrc file
colored_ping() {

    echo -e "${pur}$(ping $1)${rst}"
}

alias cping='colored_ping'

Then try:

user@linux:~# cping superuser.com

Your output for cping will be purple. If you want to use multiple colors in the same output you can add them in with unique ways. How you do it is completely up to you. For example:

echo -e "${red}This is red and ${blu}this is blue and ${grn}this is green.${rst}
0

You should have a look at the hl command available on git hub :
github : git clone http://github.com/mbornet-hl/hl
and on :
http://www.flashnux.com/notes/page_000022_US.html
hl is a linux command written in C, especially designed to color a text file or the output of a command. You can use up to 42 colors simultaneously, and use a configuration file to simplify command lines. You can colorize the output of every command that can be piped to another one. And if you know what regular expressions are, it will be very easy for you to use. You can use the man page to understand how to use it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .