Piping a command through a color filter
You'd use tput
for that:
tput setaf 1
echo This is red
tput sgr0
echo This is back to normal
This can be used to build a pipe:
red() { tput setaf 1; cat; tput sgr0; }
echo This is red | red
The basic colours are respectively black (0), red (1), green, yellow, blue, magenta, cyan and white (7). You'll find all the details in the terminfo(5)
manpage.
Here's a little script that does just that. Save this as color
in a directory in your $PATH
(for example, ~/bin
if that's in your $PATH
):
#!/usr/bin/env perl
use strict;
use warnings;
use Term::ANSIColor;
my $color=shift;
while (<>) {
print color("$color").$_.color("reset");
}
Then, pass your text through the script, giving .
as the pattern to match and specifying a color:
The supported colors depend on the abilities of your terminal. For more details, see the documentation of the Term::ANSIColor
package.
With zsh
:
autoload colors; colors
for color (${(k)fg})
eval "$color() {print -n \$fg[$color]; cat; print -n \$reset_color}"
And then:
$ echo "while" | blue
while