That's really the wrong approach; you're piling a bunch of hacks together that all "leak" through to different layers. (For example, consider recolor 'x//g;#' greenh
, which is intended to take occurrences of x//g;#
and color them green, but which actually takes occurrences of x
and deletes them; or recolor foo blueh
, which is intended to take occurrences of foo
and color them blue, but which actually doesn't work because your function secretly depends on a global variable being set and the user didn't define $blueh
.)
I think you're better off just defining individual functions:
greenh() { pat="$1" perl -pe 's/$ENV{pat}/\e[2;30;42m$&\e[0m/g' ; }
yellowh() { pat="$1" perl -pe 's/$ENV{pat}/\e[2;30;43m$&\e[0m/g' ; }
aquah() { pat="$1" perl -pe 's/$ENV{pat}/\e[2;30;46m$&\e[0m/g' ; }
If you do want a single recolor
function, then you're better off defining the colors inside it:
recolor() {
perl -e '
my $prefix =
{
"greenh" => "\e[2;30;42m",
"yellowh" => "\e[2;30;43m",
"aquah" => "\e[2;30;46m",
}->{$color};
die "Unrecognized color $color" unless $prefix;
while (<>) {
s/$pat/$prefix$&\e[0m/g;
print;
}
' -s -- -pat="$1" -color="$2"
}
(For completeness' sake, though, I should mention that Bash does support variable indirection; if $2
is greenh
, then ${!2}
is whatever $greenh
is. But that feature is usually best avoided, and your example is exactly why.)