6

This question already has an answer here:

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)

marked as duplicate by dessert command-line Jun 1 '18 at 19:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6
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.

  • Thanks for the explanation, two questions: 1. It works great for tools like lolcat or rev which operate on a line by line basis. But for cowsay, it waits until I press ctrl-d, then outputs everything. How can we make it add an EOF to each command, and spawn a new cowsay each time? – vikarjramun Jun 1 '18 at 17:12
  • @vikarjramun Yeah, I'm having the same problem. Must be a buffering thing. I'm researching it now – wjandrea Jun 1 '18 at 17:13
  • 2
    @wjandrea Perhaps the unbuffer command from expect could help. – PerlDuck Jun 1 '18 at 17:16
  • @wjandrea my first thought was xargs cowsay but that didn't work. Maybe xargs bash -c "echo {} | cowsay"? I'll try that and see. – vikarjramun Jun 1 '18 at 17:30
  • @PerlDuck I couldn't get it to work. I tried it with and without the -p flag. – wjandrea Jun 1 '18 at 18:57

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