Page 1 of 1

Bash Script – Press Any Key To Continue

Bash Script – Press Any Key To Continue
   4

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.

Parameters
Parameter Purpose
-n 1 Return after typing one character (and that’s why Alt, Shift and such do not work – as they are not characters).
-r Do not allow backslashes to escape characters (this way a backslash will work as well)
-s 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.

Some Variations

Press ENTER to continue


1
read -r -s -p $'Press enter to continue...'

Press Escape to continue


read -r -s -p $'Press escape to continue...\n' -d $'\e'

Here -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.

Support Us ...


Your support is very much appreciated, and can be as easy as sharing a link to my website with others, or on social media.

Support can also be done by sponsoring me, and even that can be free (e.g. shop at Amazon).
Any funds received from your support will be used for web-hosting expenses, project hardware and software, coffee, etc.

Thank you very much for those that have shown support already!
It's truly amazing to see that folks like my articles and small applications.

Please note that clicking affiliate links, like the ones from Amazon, may result in a small commission for us - which we highly appreciate as well.

Comments


There are 4 comments. You can read them below.
You can post your own comments by using the form below, or reply to existing comments by using the "Reply" button.

  • Apr 25, 2020 - 5:49 PM - peniel Comment Link

    Thank you for this very useful article.

    Reply

    peniel

    • Apr 26, 2020 - 5:26 AM - Hans - Author: Comment Link

      Thanks Peniel! 
      It’s much appreciated that you took the time to place a thank-you post! Glad it was useful for you! 

      Reply

      Hans

  • Jan 26, 2021 - 3:02 PM - Old Docsis Guy Comment Link

      Thanks for this article, I REALLY like the explanation of the switches! Worked like a champ on my cable modem upgrade firmware script!

       Old Docsis Guy

    Reply

    Old Docsis Guy

    • Jan 27, 2021 - 5:03 AM - Hans - Author: Comment Link

      Hi Old Docsis Guy!

      Great to hear this was useful for you! Thank you for the compliment – it is much appreciated! 

      Reply

      Hans



Your Comment …

Do not post large files here (like source codes, log files or config files). Please use the Forum for that purpose.

Please share:
*
*
Notify me about new comments (email).
       You can also use your RSS reader to track comments.


Tweaking4All uses the free Gravatar service for Avatar display.