I am using rlwrap to colorize prompt in asterisk CLI:

rlwrap -s 99999 -a -pRED /usr/sbin/asterisk -r

I read in man rlwrap that I can also use

rlwrap -z pipeto

to pipe output through a colorizer. I have grc colorizer which works like this:

cat foo | grcat <conf_file>

The above examples colorizes foo using the rules from <conf_file>.

How can I use rlwrap -z pipeto to colorize output from rlwrap through grcat ?



Unfortunately, rlwrap's built-in filter pipeto does not filter output in the way you desire. I find the documentation rather misleading, but what it does is if you run rlwrap -z pipeto some-shell, then, within the interaction:

  • if you type commands without any pipe sign (|), those are passed verbatim to some-shell and then the output of that is simply printed;

  • if you type command | filter, then command is passed to some-shell to interpret, and the output of that is piped through filter before being printed (where filter is any command you could run from the command line in your Unix shell).

So the good news is you could get the behavior you are looking of, kindof, sortof, by running rlwrap -z pipeto asterisk and then remembering to append | grc to each command you want to pass to asterisk. But that would not be very convenient, would it? Hence outfilter.


I suggest creating the following rlwrap filter script:

#! /usr/bin/perl
use lib ($ENV{RLWRAP_FILTERDIR} or ".");
use RlwrapFilter;
use strict;

my $filter = new RlwrapFilter;
my $name = $filter->name;

my $filter_command = join ' ', @ARGV;

$filter->help_text("Usage: rlwrap -z '$name <filter-command>' <command>\n"
                   . "Filter <command> output through <filter-command>");

$filter->output_handler(sub {""});

sub prompt {
    my $prompt = shift;

    my $output = $filter->cumulative_output;
    $output =~ s/\r//g;

    open (PIPE, "| $filter_command")
        or die "Failed to create pipe: $!";
    print PIPE $output;
    close PIPE;

    return $prompt;

Save it as outfilter, make it executable, and then run rlwrap -z './outfilter <coloring-filter>' shell. I tried with:

rlwrap -z './outfilter ccze -A' gosh

which nicely colors Gauche's output. In your case, that would become something like:

rlwrap -z './outfilter grcat grcat-config' asterisk

If you like the filter and want to be able to run it without having to specify its path, you could move it alongside the builtin filters (on my system, that's in directory /usr/share/rlwrap/filters).

Note that the filter as written is probably inefficient (it spawns a new copy of the coloring filter for each interaction with the command shell, because that's the shortest way I could find to have it flush its buffers) and fragile, but if the shell you are interacting with doesn't do any black magic of its own, it should work.

  • you beat me to the answer! I think that the name pipeto is misleading, rather than its documentation, but it is too late to change the name now, I think. Any ideas about how to improve the documentation? I will probably add a slightly more efficient and robust version of your filter to rlwrap. – Hans Lub Mar 19 '15 at 9:45
  • Hehe. I'll contribute a documentation pull request on GitHub when time permits. I learned more on rlwrap's scriptability answering this question than in years of use :-). Let me know if you would like me to submit this filter as well, as a starting point. – dhag Mar 19 '15 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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