Cogitatio materialis est

How to implement "Press any key to continue" in bash?

10th Oct 2013 Tags: #bash #development

If you need a pause in your bash script, like "PAUSE" does in DOS, you may implement it with read command:

  read -n 1 -r -s -p "Press any key to continue..." key
  • The -n 1  : specifies that it only waits for a single character.
  • The -r  : puts it into raw mode, which is necessary because otherwise, if you press something like backslash, it doesn't register until you hit the next key.
  • Tht -s  : Silent mode. If input is coming from a terminal, characters are not echoed.
  • The -p  : specifies the prompt, which must be quoted if it contains spaces. The prompt is displayed only if input is coming from a terminal.
  • The key  : argument is only necessary if you want to know which key they pressed, in which case you can access it through $key.