Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 24 down vote accepted

You can use read:

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

if [ "$key" = '' ]; then
    # Space pressed, do something
    # echo [$key] is empty when SPACE is pressed # uncomment to trace
    # Anything else pressed, do whatever else.
    # echo [$key] not empty
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
This script doesn't work. Just tested it on Red Hat linux... The else block always runs, even when the spacebar is pressed. – robert Dec 22 '15 at 6:56
@robert It's because you don't use bash. It works if you use read _ instead, if you have some other shell than bash. – Dac Saunders May 2 at 9:48

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 wiki.bash-hackers.org/syntax/quoting#ansi_c_like_strings – rubo77 Jun 5 '14 at 7:57
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

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
if you are new to shell scripting - you need to put the function at the top of your script before using it – Richard Mar 24 at 16:54

lazy one liner:

echo "Press any key to continue or Ctrl+C to cancel"
read && do_something.sh

the disadvantage is that you lose control when the user press ctrl+c. The script will always exit with code 130 in that case.

share|improve this answer

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.