nixCraft Poll

Topics

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

Posted by Vivek Gite [Last updated: June 20, 2007]

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

Discussion on This Article:

  1. mne Says:

    What about SLEEP command?

  2. nixcraft Says:

    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 Says:

    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 Says:

    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 Says:

    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 Says:

    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 Says:

    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 Says:

    Cool to know that that works out aswell!!

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!

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

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.