Bash – How to create merged input (stdin) for a program

bashstdin

Consider such script:

echo a
echo b
echo c

It will produce 3 lines and now I can run it like this:

abc_script | real_program

real_program will get in its input those 3 files. However, there is a problem for a user, instead of just calling abc_script, she/he has to remember to redirect the output to the real_program.

So how to move this redirection to the script? Something like this (the script):

echo a | echo b | echo c | real_program

This does not work, it is only an illustration what I would like to do — hide calling of real_program inside the script.


For the record: in my case the input consists of some file, additional extra lines. So I have:

cat $input_file
echo $extra_line

Not just a,b,c.

Best Answer

Like this:

( echo a
  echo b
  echo c ) | real_program

That runs the three echo commands in a subshell and pipes the subshell's output to the script.

Subshells are syntactical sugar for bash -c 'echo a; echo b; echo c' | real_program.

Related Question