10

I want to run a specific program from a script which normally aks the user for some input (several times).

For example, when I start the program in the shell and my input would be:

t [ENTER]
3 [ENTER]
12 [ENTER]
e [ENTER]

where one has to wait after every line that the program wants the next input.

I guess there is a solution like

echo t | prog
echo 3 | prog
echo 12 | prog
echo e | prog

but after the first line the program runs with no input because of an empty buffer. How can I fix that?

3 Answers 3

18

Prime use case for a here-document:

prog <<EOF
t
3
12
e
EOF
Sign up to request clarification or add additional context in comments.

Comments

4

Guess it depends on what kind of shell you are using. With bash you can echo multiple lines like,

$ echo "t
> 3
> 12
> e" | prog

Comments

-2

The read command reads a single line, terminated by a newline. You can include newlines in your echo:

echo "t\n3\n12\ne" | prog

2 Comments

echo does not handle escape sequences like \n unless -e option is specified on command line. The alternative solution is to use printf.
The bash built-in echo does not handle escape sequences without the -e option, which is a departure from the POSIX standard. Shells like dash, I assume, do not require -e. However, this just supports the suggestion to use printf instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.