I know how send a command as a input in a program like this:

echo toto | ./my_prog

And with process substitution + redirection:

r < <(echo toto)

But how to do this if I want to input a second or a third input?

For example, I have a program that ask my username first and after this it ask me others informations like a number phone or whatever in different input.

up vote 7 down vote accepted

Use { and } to collect the output of multiple programs. For instance, { echo one; echo two; } |program. Leave a space after { and before } and ensure there is a semicolon after the last command within the braces.

I would suggest a here document

./my_proc << END_OF_INPUTS
username
phone number
whatever
END_OF_INPUTS

You can use printf and a pipe too

printf "%s\n" "username" "number phone" "whatever" | ./my_prog
  • I can use python too like this: python -c 'print "toto\x0Atutu\x0A\x0A5\x0A"' | ./my_prog – sushi Mar 1 at 21:29
  • Seems a bit of overkill to spawn python just for this. printf is a shell builtin. – glenn jackman Mar 1 at 22:58

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.