Pause in Bash

Posted: 27th June 2015 by Tim in Bash, Linux
Tags: , , , , , , ,

Sometimes it is useful to pause a script until the user is ready to proceed. In Windows Batch scripting, this can be done with the PAUSE command. In Bash, the same thing can be done using read with the right parameters:

read -rn1 -p "Press any key to continue"

This command is doing the following:

  • -r disables backslash escaping. Without this option, typing only \ will not continue execution.
  • -n1 instructs the command to read only one character.
  • -p "Press any key..." is the prompt, or message printed to the screen.

Note that, like Batch’s PAUSE command, some keys which don’t result in a character being typed, like ALT, CTRL, SHIFT and CAPS LOCK, will not continue execution. Most other keys will work though.