Search code examples
shstdin

Pipe Multiple Strings in Sh Script


Trying to pipe multiple strings into a file similar to the following:

#!/bin/sh

echo "First name: ";
read ANSWER1;
echo "Last name: ";
read ANSWER2;
echo $ANSWER1 ANSWER2;

Wanting to be able to pipe in values like or similar (I don't want to be updating the sh script) and get the following result:

$ echo "Bugs"; echo "Bunny" | scriptName
Bugs Bunny

Solution

  • For multiline input, use a heredoc instead of multiple echos:

    cat << EOF | scriptName
    Bugs
    Bunny
    EOF