329

I'm making a script to install my theme, after it finished installing it will appear the changelog and there will be "Press any key to continue" so that after users read the changelog then press any key to continue

  • 16
    use less to display your changelog – cas Jul 05 '16 at 07:21
  • 45
    Ugh - don't do that! Installers should not be interactive; nobody wants to sit babysitting when they could have a bunch of stuff queued up to install. Just drop the changelog in your platform's standard location (usually /usr/share/doc/<package>/Changelog) and let them be done with it. For those who want to read every change, they can just install apt-showchanges or equivalent... – Toby Speight Jul 05 '16 at 15:59
  • Well in my country people do differently, so i just make theme installer as simple as possible: choose option to install>let script do it all>after finished installing show the changelog>press any key to exit, it just a script to install my iOS 10 theme on iPhone 3GS by replacing system. I double check the code so that it wont mess up the system, I tried a few install attempt and it works flawlessly – superquanganh Jul 05 '16 at 16:09
  • 22
    but... where's the "any" key? ;) – Florian Castellane Jul 06 '16 at 12:39
  • 7
    if i could edit my comment this late I'd say 'use less to display your changelog, but "Ugh - don't do that! ..."' – cas Jul 06 '16 at 17:18
  • His other problem: the iPhone does not have any keys. – Twinkles Jul 07 '16 at 09:26
  • The onscreen keyboard of iPhone still work with "press any key to continue" – superquanganh Jul 07 '16 at 13:00
  • Interactive mode would be a useful addition to the command line arguments, just off by default. – John P Jan 23 '17 at 17:47
  • I understand that you asked for a bash command, and I got here similar, but after learning about the (ugh) read -n 1 -s -r -p command I am convinced that we are most likely better off writing the script in python. – Roland Jan 09 '23 at 18:38

3 Answers3

529

You can use the read command. If you are using bash:

read -p "Press enter to continue"

In other shells, you can do:

printf "%s " "Press enter to continue"
read ans

As mentioned in the comments above, this command does actually require the user to press enter; a solution that works with any key in bash would be:

read -n 1 -s -r -p "Press any key to continue"

Explanation by Rayne and wchargin

-n defines the required character count to stop reading

-s hides the user's input

-r causes the string to be interpreted "raw" (without considering backslash escapes)

kos
  • 185
MKT
  • 5,951
  • Then what after the read statement? Seems to be missing the following line. – Jortstek May 02 '20 at 09:05
  • 1
    this helped me when I ran into the "no coprocess" error in zsh https://unix.stackexchange.com/questions/198372/read-command-in-zsh-throws-error – LexJacobs Jul 29 '20 at 21:59
  • If you got here because somebody integrated this into their code (for example abook --add-email-quiet), and you need a workaround, pipe yes | thecommand. – TamaMcGlinn May 16 '21 at 11:42
  • 1
    Yes, but I would add (like suggested by @EKons) to incl. ";echo" at the end, as it is more elegant, it brings the terminal back to a fresh new line. just my 2 cents – avia Jul 11 '21 at 08:31
  • it's showing % when pressing key. – umair mehmood Jan 20 '23 at 12:36
24
read -rsn1 -p"Press any key to continue";echo

Or, if you actually need the REPLY variable:

read -rsn1 -p"Press any key to continue" variable;echo

Replace variable with a variable name you don't need.

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
EKons
  • 944
  • 10
    echo; read -rsn1 -p "Press any key to continue . . ."; echo Oh my gosh... I've done it! I've made Windose! – Andrew Mar 20 '19 at 03:15
  • 3
    This solution (incl. ;echo) is more elegant since it brings the terminal back to a fresh new line. just my 2 cents – avia Jul 11 '21 at 08:31
20

As @cas wrote in a comment, you really should use less for this. If the changelog is more than one page, you really want a pager anyway.

You normally want to consult the PAGER environment variable instead of just calling less:

${PAGER:-less} changelog

will use $PAGER if it is set and less otherwise.