6

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?

3

4 Answers 4

19

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)
7

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

Or:

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

Or:

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

EDIT:

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.

0
5

You can also use a here document:

./command.pl <<END
response
y
END
4

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.

2
  • 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, 2014 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, 2014 at 12:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .