7

I would like to be able to pipe all bash terminal commands through a certain command (for no good reason other than to play a prank on someone). I just want to pipe the stdout of any executed command into a predetermined program without doing anything special.

For example: If that predetermined program was cowsay

echo "Hello World"

should output

 _____________
< Hello World >
 -------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

How can I achieve this? (Some of the fun programs I'd like to use to play pranks on others include rev, cowsay, and lolcat)

1 Answers1

8
exec > >(COMMAND)

Where COMMAND is rev, lolcat or other. This won't work with cowsay.

E.g.

bash-4.3$ exec > >(rev)
bash-4.3$ echo hello
olleh

Explanation:

  • exec normally replaces the current shell with another process, but if you just give it a redirection like in this case, the redirection will take place for the current shell.
  • > redirect stdout
  • >(COMMAND) input into COMMAND

Note that if you have a PROMPT_COMMAND, you should direct it to stderr to avoid the redirected stdout.

wjandrea
  • 14,504