How to pass keyboard input to linux command?

I run the linux command, which sometimes asks for user input (press 1 or 2).

I always want to answer 1, how can I pass this value automatically?

+7
bash
source share
3 answers

Use the pipe operator | to connect the output of one command to the input of another.

 echo 1 | command 

If you want to repeat some command input, you can use yes . By default, it re-sends the string "y", but also repeats another string of your choice.

 yes | cp * /tmp # Answer "y" to all of cp "Are you sure?" prompts. yes 1 | command # Answer "1" repeatedly until the command exits. 
+11
source share

Just a thought:

 echo "1" | linux_command --with-arguments <&0 

This works for commands that want input from stdin, because 0 is a handle to standard input. This question may be better related to server error ...

+2
source share
 yes 1 | command 
+1
source share

All Articles