Is there a way to pipe back to back values into a command? e.g. If I can command.pl and want to pipe in response, then y to confirm (because it doesn't have a way to auto confirm)?

I've tried echo "y" | echo "response" | ./command.pl but it doesn't work because the ordering of the pipe is from left to right. In essence, I need to run the one pipe first, then pipe the next command after that. Is there a trick with parentheses or quotes or something else?


The simplest solution is to combine the two echo commands.

echo $'y\nresponse' | ./command.pl

Writing a string with single quotes and a $ in front tells bash to interpret escape sequences like \n.

If the commands you were piping were more complicated, you could group them with curly braces.

{ echo y; echo response; } | ./command.pl

Parentheses would also work, though they'd create an unnecessary extra sub-shell (a minor inefficiency).

(echo y; echo response) | ./command.pl

Another option is process substitution.

./command.pl < <(echo y; echo response)

Have you tried grouping your two answers in a single echo separated by a newline?

echo -e "response\ny" | ./command.pl

Note the -e flag is necessary with bash to enable interpretation of backslash escapes (unless bash is in Unix conformance mode).

Or more portably:

echo 'response
y' | ./command.pl


printf 'response\ny\n' | ./command.pl


printf '%s\n' response y | ./command.pl


I forgot to mention, but the problem with your initial command was that echo doesn't take any input via its STDIN. The output of the command echo "y" never reached ./command.pl.


You can also use a here document:

./command.pl <<END

I put the commands into a file then catted the file into the command and that worked.

cat blah.txt | ./command

When I came back, I noticed user43791's response, so will accept that as the answer which looks to be the same thing I did, but more succinct.

  • 1
    Glad my answer fits your need! :) Btw, your command could be rewritten like so : ./command < blah.txt (redirect stdin from the file). This would be much more efficient since it saves you a process (cat) and a pipe.
    – user43791
    Dec 2 '14 at 22:48
  • 2
    @user43791 is right, and you can even do the following (makes it more like the cat you usually use) : <blah.txt ./command Dec 3 '14 at 12:15

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.