tags:

views:

3838

answers:

4

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?

+28  A: 

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
Jim
+1  A: 

Try this:

`function pause(){
   read -p “$*”
}`
Alex Fort
+1  A: 
echo "Presse enter to continue"; read line

This will work in bash as long as stdin is coming from the console. If you have redirected stdin doing something like this e.g

myscript < fooFile

it will not work.

Victor
+3  A: 

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.

xsl