In this article I’ll describe what I have used to create a “Press Any Key to Continue” in a Bash script (Linux/MacOS X).
Working with scripts in Bash (Terminal or Shell) isn’t what I do daily. The day I had to create a script to help me do an SVN update followed by a massive recompile. I wanted to see the SVN result first before proceeding with recompile, so I had to add a “Press Any Key To Continue” option in the script.
Since it took me a little effort, I figured; let’s describe this in an article. After all, it may be good reference for myself and for others.
These methods work for Linux and MacOS.
Bash Press Key to continue
In certain situations, one may want a “Press any key to continue” option in a script, to give the user some time to review certain output of the script. For example, I made a script which does an SVN update of Lazarus Pascal, and then does a full clean “make”. Before the “make” starts, I’d like to review what was updated from SVN, just to give me an idea what to expect once the compiler and linker are done creating a new version of Lazarus Pascal. Obviously, you can apply this to your own script which may or may not be related to anything Lazarus Pascal.
For this we use the “read” command (man page), which is available under Linux and MacOS X. The man page under MacOS X is somewhat limited, not sure why, so I recommend looking at the link.
The simplest way, that I found, to accomplish this is:
read -n 1 -r -s -p $'Press enter to continue...\n'
Note that this catches all keys except keys like Shift, Alt, Ctrl, Windows, Alt-menu, and Command ( ). You could consider this a problem … or not.
||Return after typing one character (and that’s why Alt, Shift and such do not work – as they are not characters).
||Do not allow backslashes to escape characters (this way a backslash will work as well)
||Do not echo (display) what is being typed.
|-p $”Press any key to continue\n”
||Display a prompt saying “Press any key to continue” (\n = new line).
Dollar symbol ($) must be before the quoted string to allow escape characters like new line.
Adding a Time Out
We can expand this line with a time out. So if the user does not press any key within an x amount of seconds (5 seconds in the example below), the script will continue as if the user had pressed a key. This is done as such:
read -t 5 -n 1 -s -r -p "Press any key to continue"
The parameter “
-t 5 ” sets the timeout to 5 seconds – which you can alter whatever time you’d prefer.
Press ENTER to continue
read -r -s -p $'Press enter to continue...'
Press Escape to continue
read -r -s -p $'Press escape to continue...\n' -d $'\e'
-d $'\e' determines that the Escape key (\e) is the “final” character (kind-a like using the ENTER key). So be aware that the user can type a bunch of other characters (not displayed) before they press Escape.
Note : the “Press any key to continue” would have worked with the Escape key as well … and may even be the better solution unless pressing Escape is required.
Donations are very much appreciated, but not required. Donations will be used for web-hosting expenses, project hardware or a motivational boost (a drink or snack). Thank you very much for those have donated already! It's truly AwEsOmE to see that folks like our articles and small applications.