Take the 2-minute tour ×
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 at 19:23
add comment

4 Answers 4

up vote 5 down vote accepted

You can use read:

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

if [ "$key" = ' ' ]; then
    # Space pressed, do something
else
    # Anything else pressed, do whatever else.
fi
share|improve this answer
1  
There are a couple of problems with this. It matches the tab and newline characters too, why not just compare against ' '? Also, you're doing a numeric comparison, so pressing a letter or symbol will cause an error: -bash: [: x: unary operator expected. TL;DR: [ "$key" = 'a' ] –  nyuszika7h Jun 4 at 13:14
    
Also, // isn't a comment in bash. –  nyuszika7h Jun 4 at 14:09
    
My bad, was a quick answer while was working ok something else. Thanks for the edit. –  AleksanderKseniya Jun 4 at 18:47
    
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 at 9:24
add comment
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)
done
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
add comment

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

Example

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...
$ 

References

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 at 22:29
    
I mean why the $ before the string in here: -rsp $'Press ? –  rubo77 Jun 4 at 22:44
1  
@rubo77 - ah. That's how you can do a literal string with special characters. It's of the form: $' ... ' –  slm Jun 5 at 0:38
1  
@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 at 7:37
1  
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 at 7:57
show 1 more comment

You could create a function for it:

pause(){
 read -n1 -rsp $'Press any key to continue or Ctrl+C to exit...\n'
}

Then you can use this everywhere in your script:

pause
share|improve this answer
add comment

Your Answer

 
discard

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.