I have a Bash shell script in which I would like to pause execution until the user presses a key. In DOS, this is easily accomplished with the "pause" command. Is there a Linux equivalent I can use in my script?

share|improve this question
up vote 257 down vote accepted

read does this:

user@host:~$ read -n1 -r -p "Press any key to continue..." key
[...]
user@host:~$ 

The -n1 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. The -p specifies the prompt, which must be quoted if it contains spaces. The key argument is only necessary if you want to know which key they pressed, in which case you can access it through $key.

If you are using Bash, you can also specify a timeout with -t, which causes read to return a failure when a key isn't pressed. So for example:

read -t5 -n1 -r -p 'Press any key in the next five seconds...' key
if [ "$?" -eq "0" ]; then
    echo 'A key was pressed.'
else
    echo 'No key was pressed.'
fi
share|improve this answer
2  
Strictly speaking, that would be "Enter any non-NUL character to continue". Some keys don't send any character (like Ctrl...) and some send more than one (like F1, Home...). bash ignores NUL characters. – Stephane Chazelas Jun 4 '14 at 20:33
2  
How cool. This was posted 8 years and 9 months ago and came right up in my Google search in July 2017. Just what I needed! – SDsolar Jul 8 '17 at 5:27

I use these ways a lot that are very short, and they are like @theunamedguy and @Jim solutions, but with timeout and silent mode in addition.

I especially love the last case and use it in a lot of scripts that run in a loop until the user presses Enter.

Commands

  • Enter solution

    read -rsp $'Press enter to continue...\n'
    
  • Escape solution (with -d $'\e')

    read -rsp $'Press escape to continue...\n' -d $'\e'
    
  • Any key solution (with -n 1)

    read -rsp $'Press any key to continue...\n' -n 1 key
    # echo $key
    
  • Question with preselected choice (with -ei $'Y')

    read -rp $'Are you sure (Y/n) : ' -ei $'Y' key;
    # echo $key
    
  • Timeout solution (with -t 5)

    read -rsp $'Press any key or wait 5 seconds to continue...\n' -n 1 -t 5;
    
  • Sleep enhanced alias

    read -rst 0.5; timeout=$?
    # echo $timeout
    

Explanation

-r specifies raw mode, which don't allow combined characters like "\" or "^".

-s specifies silent mode, and because we don't need keyboard output.

-p $'prompt' specifies the prompt, which need to be between $' and ' to let spaces and escaped characters. Be careful, you must put between single quotes with dollars symbol to benefit escaped characters, otherwise you can use simple quotes.

-d $'\e' specifies escappe as delimiter charater, so as a final character for current entry, this is possible to put any character but be careful to put a character that the user can type.

-n 1 specifies that it only needs a single character.

-e specifies readline mode.

-i $'Y' specifies Y as initial text in readline mode.

-t 5 specifies a timeout of 5 seconds

key serve in case you need to know the input, in -n1 case, the key that has been pressed.

$? serve to know the exit code of the last program, for read, 142 in case of timeout, 0 correct input. Put $? in a variable as soon as possible if you need to test it after somes commands, because all commands would rewrite $?

share|improve this answer
    
+1 for explaining -s; man read and read --help help didn't help on Ubuntu 10.04.1 LTS. Edit: help read did; is the rest deprecated? – Cees Timmerman Oct 22 '14 at 8:35
    
1+ for the great explanation: but i got read: -i: invalid option for the ex. read -rp $'Are you sure (Y/n) : ' -ei $'Y' key;on #osx read -rp $'kill-server: Are you sure (Y/n) : ' -d $'Y' key; works for me instead. ` – Tino Rüb Mar 21 '16 at 18:26
    
I don't know how it works on OSX but I've made some test and -i works perfectly on Ubuntu, also I don't know how if -d works the same way on OSX. – y.petremann Mar 21 '16 at 22:40

read without any parameters will only continue if you press enter. The DOS pause command will continue if you press any key. Use read –n1 if you want this behaviour.

share|improve this answer

read -n1 is not portable. A portable way to do the same might be:

(   trap "stty $(stty -g;stty -icanon)" EXIT
    LC_ALL=C dd bs=1 count=1 >/dev/null 2>&1
)   </dev/tty

Besides using read, for just a press ENTER to continue prompt you could do:

sed -n q </dev/tty
share|improve this answer
1  
status=none is not portable either. Redirect stdout and stderr to /dev/null instead. read -r line < /dev/tty would be enought for press ENTER.... – Stephane Chazelas Jun 4 '14 at 20:28
    
@StephaneChezales thanks - i didnt know that. ill fix it now. Thanks again - fixed. Youre a bottomless well of worthwhile information, by the way. – mikeserv Jun 4 '14 at 20:31
1  
Also note the settings=$(stty -g); stty raw; dd ...; stty "$settings" to save and restore the tty settings. – Stephane Chazelas Jun 4 '14 at 20:36
    
@StephaneChezales - im not at a computer - do you think the tr edit thing could work too? – mikeserv Jun 4 '14 at 20:46
1  
No, because tr would buffer its output as its a pipe, and non-US keyboards have keys that send characters outside the \1-\177 range. dd is the idiomatic way here. – Stephane Chazelas Jun 4 '14 at 20:51

This worked for me on multiple flavors of Linux, where some of these other solutions did not (including the most popular ones here). I think it's more readable too...

echo Press enter to continue; read dummy;

Note that a variable needs to be supplied as an argument to read.

share|improve this answer
4  
Posted 8 years ago. In July 2017 this is what I wanted. Thanks much. – SDsolar Jul 8 '17 at 5:29
    
this answer solve my problem, if I copy and paste this line and for some reason I got more lines appended, the appended lines are not executed , like was with DOS pause – Sérgio Oct 28 '17 at 0:38

If you just need to pause a loop or script, and you're happy to press Enter instead of any key, then read on its own will do the job.

share|improve this answer

Try this:

function pause(){
   read -p "$*"
}
share|improve this answer
    
Can someone please explain why this is downvoted? Is it for lack of content or is this a bad solution? – 3ocene Apr 11 '17 at 22:24
1  
@3ocene This does not show usage of the function that is created; plus this lack any explanation. – parvus Dec 27 '17 at 13:19

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.