150

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.
    – tripleee
    Mar 4, 2015 at 5:20
  • 1
    You could use printf instead of echo : printf "hello\n" Feb 23, 2020 at 7:21

7 Answers 7

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

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

echo | <yourfinecommandhere>
10
  • 1
    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
    Jun 7, 2011 at 11:46
  • 4
    @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. Jun 7, 2011 at 11:49
  • 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
    Jun 7, 2011 at 11:55
  • @Marcin, still, why do you get 5c6e? Did you omit the -e or does your shell or echo not treat that option? Jun 7, 2011 at 12:00
  • 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?
    – Marcin
    Jun 7, 2011 at 12:09
55

You might find the yes command useful.

See man yes

5
  • 84
    yes I do find it useful. Sep 5, 2012 at 23:03
  • 4
    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".
    – Burhan Ali
    Jul 21, 2015 at 14:51
  • 6
    @BurhanAli: you can change what yes sends as its output. Read the man page.
    – siride
    Apr 21, 2017 at 15:21
  • 1
    can I send yes mutiple times for a script that expects default values [value] {enter}
    – AK_
    Jun 11, 2018 at 21:26
  • This only works for things like yum. Where that string is expected.
    – eco
    Apr 8, 2019 at 19:09
54

You can just use yes.

# yes "" | someCommand
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
  • especially helpful if multiple enter keypresses need to be sent
    – Andy
    Jul 28, 2018 at 20:11
  • This is the best way, specially for things like phars where different stdout and stdin strings are sent.
    – eco
    Apr 8, 2019 at 19:09
  • Also remember to just enter my_command arg if you have an argument.
    – eco
    Apr 8, 2019 at 21:18
  • It always says: invalid command name "my_command", even when command is simple like "ls".
    – taranaki
    Jun 5, 2022 at 4:49
11

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

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
  • just what I needed thanks. I'm generating 12 different NestJs modules and each command has two prompts.
    – LeanKhan
    Mar 11, 2022 at 20:37
2

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
  • 1
    Thank you, this is exactly what I needed. I put it in a for loop because i needed to spam it Dec 6, 2023 at 16:08

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