Howto add pause prompt in a shell script ( bash pause command )

Most of you may be aware of old good DOS/2000/XP pause command. It is use display the prompt. It is used within a computer batch file and allows the computer to pause the currently running batch file until the user presses any key.

bash pause command - under Linux / UNIX

But there is no pause command under Linux/UNIX bash shell. You can easily use read command with -p option to display pause along with a message:

For example:
$ read -p "Press any key to start backup…"

bash shell pause function

You can create a function as follows:

#!/bin/bash
# init
function pause(){
   read -p “$*”
}
# other stuff
pause 'Press any key to continue…'
# other stuff

Original DOS/XP pause command is an internal command. Use above technique if you are migrating from DOS/Windows batch files :D

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 12 comments… read them below or add one }

1 mne 03.05.07 at 10:41 am

What about SLEEP command?

2 nixcraft 03.05.07 at 2:46 pm

Sleep puts a delay for a specified amount of time w/o a prompt. So you need to use read –p so that user can hit a key.

3 bhaskar 03.05.07 at 4:37 pm

One small tip,
If you do this in a while loop that reads from a file.
e.g.
while read line
do
…..
read -p somevar
…..
done < somefile

It will not work as expected, as the input for the while loop is “somefile”, so the “read -p ” inside the loop will read a line from somefile, instead of standard input.

To solve this you can use file descriptors.

so

exec 5

4 Gilles Allard 03.05.07 at 9:11 pm

If you need an exact replacement for PAUSE you need to use:
read -n 1 -p prompt
without -n, read will require the ENTER key.
I’m not sure but -n may be a bash specific.

5 Smith 09.01.07 at 12:48 pm

read -p “Press any key”. doesn’t provide the ‘pause’ behavior.

It requires ‘ENTER’ key to be pressed, so it becomes, ‘press ENTER key’ instead of ‘press any key’.

6 anders 11.05.07 at 5:21 pm

Using simply read to pause>nul can be quite useful too.

I ran into the need for pause in this simple bash script, where I pass a website as argument and the script tells me the password to the site from my personal password file. Note: one site and one password on each line, file mode should be 600.
The script is useful to have on my webserver which is always on and I can reach from work or anywhere whenever I need it.

#!/bin/bash
cat ~/my_passwords.txt | grep $1; read; clear;

Hope this helps someone :-)

7 Alison 03.21.08 at 5:51 am

Lifesaver. Thankyou. :)

(I needed to put in a “are you sure” message for windows users to be able to run scripts on a Linux box… who knows if it will help, but hey, at least it’s a start.)

8 LeMMiNGS 06.23.08 at 2:35 am

Cool to know that that works out aswell!!

9 Diven 08.22.08 at 12:23 am

I believe the following is at least very close to the behavior of the pause command.


function pause(){
read -s -n 1 -p "Press any key to continue . . ."
echo
}

10 christ 12.09.08 at 1:18 am

read is a good choice for pausing but, sometimes we are looking for - pause then continue command without user interference so i guess sleep is more realiable

11 g00ner 12.30.08 at 11:17 am

Being a windows admin this was of great help.

12 Pai 03.18.09 at 10:25 am

echo “Press any Key to continue”
read -n1 -t5 any_key

-n1 -> number of character it can read
-t5 -> it will wait for 5 seconds the user to enter a char after 5 sec it will resume the flow

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: ,

Previous post: How important is money to you?

Next post: OpenSolaris shipping free DVDs all around the world