142

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.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
tobbr
  • 2,046
  • 3
  • 14
  • 15

6 Answers6

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

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

echo | <yourfinecommandhere>
Tilman Vogel
  • 9,337
  • 4
  • 33
  • 32
  • 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 07 '11 at 11:46
  • 3
    @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. – Tilman Vogel Jun 07 '11 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 07 '11 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? – Tilman Vogel Jun 07 '11 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 07 '11 at 12:09
  • @Marcin, your last findings are all as I would expect. Also, back-slash is only treated special in double-quotes for dollar, back-tick, double-quote, backslash and newline according to `man bash`, so that's fine too. I was puzzled by your original comment stating that `echo -e '\n' | xxd` would output `0000000: 5c6e`. Can you still reproduce this? – Tilman Vogel Jun 07 '11 at 14:36
  • Is it portable on OSX? – David Froger Dec 09 '16 at 14:31
  • The behaviour of `echo` is highly dependent on your shell and system, it varies quite a lot actually (which explains some of the comments). The only thing that is guaranteed to work is `echo "string without escape characters"`. There is no need to add this part to your answer, as `echo | cmd` is exactly the same. I recommend you edit it out (if you *do* want an explicit newline echo, then `printf '\n'` is the portable way to do that). – Martin Tournoij Sep 18 '17 at 10:39
  • @TilmanVogel How to chain this? I would like to press multiple enters after running `mycommand`. – Sunlight Oct 27 '22 at 06:04
  • @Sunlight What do you mean by "after running"? If you want to feed multiple enters to the same program, you can easily do `echo -ne '\n\n\n' | ` to feed it three enters. If you need an indefinite number of enters, use `yes` as suggested by some of the other answers. – Tilman Vogel Oct 28 '22 at 08:44
53

You can just use yes.

# yes "" | someCommand
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
David Hamner
  • 671
  • 5
  • 6
52

You might find the yes command useful.

See man yes

pavium
  • 14,808
  • 4
  • 33
  • 50
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.

kenorb
  • 155,785
  • 88
  • 678
  • 743
11

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

barti_ddu
  • 10,179
  • 1
  • 45
  • 53
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
Bocar
  • 71
  • 1
  • 3
  • just what I needed thanks. I'm generating 12 different NestJs modules and each command has two prompts. – LeanKhan Mar 11 '22 at 20:37