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 ![]()
Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
You may also be interested in other helpful articles:
- XEN Server Status Monitoring Command Cheat Sheet
- Linux Fibre Channel over Ethernet implementation code released
- FreeBSD reset or recover root password
- Discover UNIX and Linux Command line combinations to Expand your *nix vocabulary
- Advanced techniques for using the UNIX/Linux find command
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!



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!!