Coder Perfect

Is there a Linux counterpart to the DOS pause command?


I’d like to suspend the execution of a Bash shell script till the user pushes a key. The pause command in DOS is a simple way to do this. Is there a Linux equivalent that I could incorporate into my script?

Asked by jt.

Solution #1

read does this:

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

The -n1 option indicates that it will only wait for one character. The -r switches it to raw mode, which is required since otherwise, pressing backslash doesn’t register until the next key is pressed. The -p option defines the prompt, which must be quoted if there are any spaces in it. The key parameter is only required if you want to know the key they pushed; otherwise, you can use $key to find out.

If you’re using Bash, you may also use the -t option to specify a timeout, which causes read to fail if no key is pushed. As an example:

read -t5 -n1 -r -p 'Press any key in the next five seconds...' key
if [ "$?" -eq "0" ]; then
    echo 'A key was pressed.'
    echo 'No key was pressed.'

Answered by Jim

Solution #2

I frequently use these brief methods, which are similar to @theunamedguy and @Jim’s solutions but include timeout and silent mode.

The last case is one of my favorites, and I use it in a lot of scripts that loop until the user pushes Enter.

Raw mode, which does not accept mixed characters like “” or “”, is specified by the -r option.

Because we don’t need keyboard output, -s specifies silent mode.

The prompt must be between $’ and’to allow spaces and escaped characters. -p $’prompt’ defines the prompt. To benefit escaped characters, you must put between single quotes the dollar symbol; otherwise, you can use simple quotes.

-d $’e’ specifies escappe as the delimiter character, allowing any character to be used as the final character for the current entry. Be sure to use a character that the user can type.

The -n 1 specifies that only one character is required.

Readline mode is specified by the -e option.

In readline mode, -i $’Y’ specifies Y as the first text.

-t 5 specifies a 5-second timeout.

If you need to know the input, or in the case of -n1, the key that was pressed, use key serve.

$? displays the last program’s exit code, which is read, 142 in the event of a timeout, and 0 for correct input. If you need to test $? after some commands, put it in a variable as soon as feasible, because every commands will rewrite $?

Answered by y.petremann

Solution #3

This worked for me on a variety of Linux varieties, but some of the other methods didn’t (including the most popular ones here). It’s also more readable, in my opinion…

echo Press enter to continue; read dummy;

It’s worth noting that a variable must be passed as an argument to read.

Answered by BuvinJ

Solution #4

If you press enter while reading without any parameters, the reading will continue. If you press any key, the DOS pause command will proceed. Use read –n1 if you want this behaviour.

Answered by xsl

Solution #5

read -n1 isn’t a portable command. A portable technique to accomplish the same goal could be:

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

You might do the following instead of using read for a simple press ENTER to continue prompt:

sed -n q </dev/tty

Answered by mikeserv

Post is based on