HowTo: Add Pause Prompt In a Shell Script ( bash pause command )

by on March 4, 2007 · 25 comments· LAST UPDATED January 9, 2012

in , ,

Most of you may be aware of old good DOS/2000/XP pause command. It is used display the prompt while suspending processing of a batch script. 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

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

 
read -p "Press [Enter] key to start backup..."
 

The above will suspends processing of a shell script and displays a message prompting the user to press [Enter] key to continue.

bash shell pause function

You can create a function as follows:

 
#!/bin/bash
# init
function pause(){
   read -p "$*"
}
 
# ...
# call it
pause 'Press [Enter] key to continue...'
# rest of the script
# ...
 

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

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 25 comments… read them below or add one }

1 mne March 5, 2007 at 10:41 am

What about SLEEP command?

Reply

2 nixCraft March 5, 2007 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.

Reply

3 bhaskar March 5, 2007 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

Reply

4 Gilles Allard March 5, 2007 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.

Reply

5 Smith September 1, 2007 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’.

Reply

6 Scott Dunn January 9, 2012 at 11:21 pm

Thank you. I learned that the not so hard way. Trying it. :)

Reply

7 anders November 5, 2007 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 :-)

Reply

8 Alison March 21, 2008 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.)

Reply

9 LeMMiNGS June 23, 2008 at 2:35 am

Cool to know that that works out aswell!!

Reply

10 Diven August 22, 2008 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
}

Reply

11 christ December 9, 2008 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

Reply

12 g00ner December 30, 2008 at 11:17 am

Being a windows admin this was of great help.

Reply

13 Pai March 18, 2009 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

Reply

14 Sanchit January 4, 2010 at 12:56 pm

Hi,
I have one unix script file that has one command to execute a java program. That Java program is used to download a file from a server. After this command I have different commands (“hdiutil checksum -type CRC32 “) to execute on the downloaded file. My problem is that after executing the java command it is not waiting for the file to be downloaded from the server and executing that command and fails because still that file is not downloaded.

Can someone help me to resolve this issue. How should I wait fro the file to be downloaded then only it should execute the other commands?

Please help me to get out in to this situation as soon as possible…

Reply

15 tom3k February 10, 2010 at 12:06 pm

for anyone writing any cli based php scripts, and who doesnt feel like installing the whole ncurses package JUST to get the ‘press enter to continue…’ functionality…

this method works great via

system(‘read…..

thanks! +5

Reply

16 JW August 7, 2010 at 7:32 pm

Thanks for script, I put it at the end of a function and it is helpful. How would I call a function when Enter is pressed? I tried the following but it didn’t work:

function pause(){
read -p “$*”
Function_1
}
pause “Enter to continue…”

Reply

17 zulkaif dilawar May 27, 2011 at 6:08 am

I want to write a program which pause execution when i enter “return key ” and start execution when i again enter “return key ” from where it is paused.
Please help me.

Reply

18 roger July 16, 2011 at 1:39 am

Thanks worked perfectly. Wonder why they don’t have this as a bash command…

Reply

19 Augustus July 26, 2011 at 5:45 pm

The code worked except the comment should read “Press the ENTER key to continue…”.
Thank you!

Reply

20 Bhanu October 23, 2011 at 7:46 pm

This code worked for me well!

I am looking to pause the script multiple times. So, I used multiple pause statements after every 10 lines. It didn’t work.

Does anyone know why?

Thanks.

Reply

21 Spaldam February 4, 2012 at 6:53 pm

It’s not working for me. I’m not sure why. The loop just keeps going.

Reply

22 epileg March 2, 2012 at 8:50 pm

There is a way to cleanly do that on Linux:

#!/bin/sh

echo -n “Press any key to continue…”
stty -echo
dd count=1 1>/dev/null 2>&1
stty echo
echo

Best regards,

Reply

23 epileg March 2, 2012 at 9:40 pm

Sorry, previous one don’t properly works.
This do the job:

#!/bin/sh

echo -n “Press any key to continue…”
CFG=`stty -g`
stty -echo -icanon
dd count=1 1>/dev/null 2>&1
stty $CFG
echo

Regards,

Reply

24 epileg March 2, 2012 at 10:45 pm

And a simple way:

#!/bin/bash

read -s -n 1 -p “Press any key to continue…”
echo

Regards,

Reply

25 echo November 1, 2012 at 12:27 am

What about SLEEP command?

Reply

Leave a Comment

Tagged as: , , , , , , , , ,

Previous post:

Next post: