Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

How do I stop a bash script until a user has pressed Space?

I would like to have the question in my script

Press space to continue or CTRL+C to exit

and then the script should stop and wait until Space is pressed.

share|improve this question
All this and more is addressed in this SO Q&A BTW: What is the linux equivalent to DOS pause? – slm Jun 4 '14 at 19:23

4 Answers 4

up vote 16 down vote accepted

You can use read:

read -n1 -r -p "Press space to continue..." key

if [ "$key" = ' ' ]; then
    # Space pressed, do something
    # Anything else pressed, do whatever else.
share|improve this answer
You should add -s to not to print the pressed character out on the terminal. And add a linebreak at the end, or the output will continue directly in the same line like the question. Best would be: read -n1 -rsp $'Press any key to continue or Ctrl+C to exit...\n' – rubo77 Jun 22 '14 at 9:24

You could create a function for it:

 read -n1 -rsp $'Press any key to continue or Ctrl+C to exit...\n'

Then you can use this everywhere in your script:

share|improve this answer
hold=' '
printf "Press 'SPACE' to continue or 'CTRL+C' to exit : "
tty_state=$(stty -g)
stty -icanon
until [ -z "${hold#$in}" ] ; do
    in=$(dd bs=1 count=1 </dev/tty 2>/dev/null)
stty "$tty_state"

This now prints a prompt without a trailing newline, handles CTRL+C reliably, invokes stty only as often as necessary, and restores the controlling tty to exactly the state in which stty found it. Look into man stty for information on how to explicitly control echoes, control characters and all.

You might also do this:

printf "Press any key to continue or 'CTRL+C' to exit : "
(tty_state=$(stty -g)
stty -icanon
LC_ALL=C dd bs=1 count=1 >/dev/null 2>&1
stty "$tty_state"
) </dev/tty

You could do it with ENTER, no [ tests ] and no stty like:

sed -n q </dev/tty
share|improve this answer

The method discussed in this SO Q&A is likely the best candidate for an alternative to the pause behavior that you're accustom to on Windows when doing BAT files.

$ read -rsp $'Press any key to continue...\n' -n1 key


Here I am running the above and then simply pressing any key, in this case the D key.

$ read -rsp $'Press any key to continue...\n' -n1 key
Press any key to continue...


share|improve this answer
That's a prompt. That's how I write code that you'll be running from the command line. – slm Jun 4 '14 at 22:29
I mean why the $ before the string in here: -rsp $'Press ? – rubo77 Jun 4 '14 at 22:44
@rubo77 - ah. That's how you can do a literal string with special characters. It's of the form: $' ... ' – slm Jun 5 '14 at 0:38
@rubo77 - that's different. That's a dollar sign w/ double quotes, I used a dollar w/ single quotes. Please delete that comment, it's wrong. – slm Jun 5 '14 at 7:37
Ah, I understand. In case you put escaped sequences inside the prompt-string. see – rubo77 Jun 5 '14 at 7:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.