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!
- Email this to a friend
- Printable version
- Rss Feed
- Last Updated: Jun/20/2007

{ 12 comments… read them below or add one }
What about SLEEP command?
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.
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
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.
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’.
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 :-)
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.)
Cool to know that that works out aswell!!
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
}
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 realiableBeing a windows admin this was of great help.
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