159

I've created a really simple bash script that runs a few commands. one of these commands needs user input during runtime. i.e it asks the user "do you want to blah blah blah?", I want to simply send an enter keypress to this so that the script will be completely automated.

I won't have to wait for the input or anything during runtime, its enough to just send the keypress and the input buffer will handle the rest.

2
  • 1
    If the tool you are trying to automate is apt-get, the proper solution is to preseed the debconf database with your preferred values; then it won't ask you. Commented Mar 4, 2015 at 5:20
  • 1
    You could use printf instead of echo : printf "hello\n" Commented Feb 23, 2020 at 7:21

7 Answers 7

187
echo -ne '\n' | <yourfinecommandhere>

or taking advantage of the implicit newline that echo generates (thanks Marcin)

echo | <yourfinecommandhere>
Sign up to request clarification or add additional context in comments.

10 Comments

if you feed this output to xxd you get 5c6e hex, which is a literal \n. if you do just echo | xxd you actually end up with a hex of 0a. So I guess use whichever your program needs.
@Marcin, I am surprised, I get 0a0a which is still not as intended, adding an -n suppreses the implicit newline. But your are of course right, a plain echo | ... works fine here. Will update my answer.
Heh, it didn't look quite right to me, so I had to feed it through xxd. It's by far the simplest yet efficient method of seeing exactly what commands output. Gotta love stupid Unix tricks.
@Marcin, still, why do you get 5c6e? Did you omit the -e or does your shell or echo not treat that option?
i get: echo -ne "\n" | xxd 0000000: 0a and echo -n "\n" | xxd 0000000: 5c6e and echo "\n" | xxd 0000000: 5c6e 0a I have not seen any difference between single and double quotes. My shell is just a regular bash, is there settings that would regulate behavior of such basic functionality?
|
58

You might find the yes command useful.

See man yes

5 Comments

yes I do find it useful.
This simulates pressing "y" repeatedly rather than the Enter key. While this is probably what you want to yes/no questions, it won't work for anything that doesn't expect a "y".
@BurhanAli: you can change what yes sends as its output. Read the man page.
can I send yes mutiple times for a script that expects default values [value] {enter}
This only works for things like yum. Where that string is expected.
57

You can just use yes.

# yes "" | someCommand

Comments

25

Here is sample usage using expect:

#!/usr/bin/expect
set timeout 360
spawn my_command # Replace with your command.
expect "Do you want to continue?" { send "\r" }

Check: man expect for further information.

4 Comments

especially helpful if multiple enter keypresses need to be sent
This is the best way, specially for things like phars where different stdout and stdin strings are sent.
Also remember to just enter my_command arg if you have an argument.
It always says: invalid command name "my_command", even when command is simple like "ls".
11

You could make use of expect (man expect comes with examples).

Comments

3

you could use xdotool to simulate a "enter":

xdotool key 'Return' | commandhere

or you can make it sleep then enter a command:

sleep 0.5; xdotool key 'Return' | commandhere

1 Comment

Thank you, this is exactly what I needed. I put it in a for loop because i needed to spam it
3

I know this is old but hopefully, someone will find this helpful.

If you have multiple user inputs that need to be handled you can use process substitution and use echo as a 'file' for cat with whatever is needed to handle the first input like this:

# cat ignores stdin if it has a file to look at
cat <(echo "selection here") | command

and then you can handle subsequent inputs by piping the yes command with the answer:

cat <(echo "selection here") | yes 'y' | command

1 Comment

just what I needed thanks. I'm generating 12 different NestJs modules and each command has two prompts.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.