I can ask the user to press Enter by using read, and have him wait by calling sleep. But I can’t think of a way of doing both at the same time. I would like the user to be given the choice:
Press Ctrl+C to Cancel, Enter to continue or just wait 10 seconds
How can I do that?
In
bash(1), thereadcommand has a-toption where you can specify a timeout. From the man page:Transcript below (without hitting ENTER):
Another, hitting ENTER after a couple of seconds:
And a final one, hitting CTRL-C:
(1) If you’re doing this in a script, make sure that it’s running under
bashrather than some other shell.You can do that in many ways such as by using a
#!shebang line of some type, specifying the shell when you run the command, or making sure your system will runbashby default for scripts.