So I am trying to make a bash script that calls this one command and then feeds it the input. It calls this one command and the command needs 3-4 inputs after. I type the command and it waits for me to enter first name, one I enter first name it waits for me to enter last name, and so on. How can I use bash script to pass these arguments to the command one at a time?
1 Answer
A couple of ways.
Group all the echo commands and pipe them to the command:
{ echo $firstname; echo $lastname ; } | somecommand
or use a heredoc:
somecommand <<EOF
$firstname
$lastname
EOF
10 Comments
Johsh Hanks
If I use the piping method. Would it pipe them one a time tho? As I need it give it one input and then it asks me for another one after. I can't just give them all the inputs in a single input if that makes sense
Barmar
Both of them send each input as a separate line.
echo adds a newline by default.Digital Trauma
@Barmar Why the
() subshell? I think a {} command grouping should suffice here without the need for an extra fork()?Barmar
I'm not sure there's much difference, since it has to fork for the pipe.
Johsh Hanks
Just tried it and it doesn't seem to be working. So I have two variables firstname="John" lastname="Smith" and a variable command. When I do (echo $firstname echo $lastname) | $command it executes the command but then it sits waiting for me to enter first name
|