Bash 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 used to display the prompt while suspending the 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. Let us see how to pause our bash based shell script for a given number of times in seconds/minutes/hours before continuing to next operation/command running on a Linux or Unix-like systems.

ADVERTISEMENTS

bash pause command under Linux / UNIX / macOS

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.
Bash add pause prompt using sleep and read command in shell scripts under Linux and Unix

Bash add pause prompt in a shell script with bash pause command

For example:

read -p "Press [Enter] key to start backup..."
read -p "Press any key to resume ..."
## Bash add pause prompt for 5 seconds ##
read -t 5 -p "I am going to wait for 5 seconds only ..."

The above will suspends processing of a shell script and displays a message prompting the user to press [Enter] (or any) key to continue. The last example will wait for 5 seconds before next command execute. We can pass the -t option to the read command to set time out value. By passing the -s we can ask the read command not to echo input coming from a terminal/keyboard as follows:

function pause(){
 read -s -n 1 -p "Press any key to continue . . ."
 echo ""
}
 
## Pause it ##
pasue
 
## rest of script below

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
# ...

Getting help about the read command

Type:
help read

      -a array	assign the words read to sequential indices of the array
    		variable ARRAY, starting at zero
      -d delim	continue until the first character of DELIM is read, rather
    		than newline
      -e	use Readline to obtain the line
      -i text	use TEXT as the initial text for Readline
      -n nchars	return after reading NCHARS characters rather than waiting
    		for a newline, but honor a delimiter if fewer than
    		NCHARS characters are read before the delimiter
      -N nchars	return only after reading exactly NCHARS characters, unless
    		EOF is encountered or read times out, ignoring any
    		delimiter
      -p prompt	output the string PROMPT without a trailing newline before
    		attempting to read
      -r	do not allow backslashes to escape any characters
      -s	do not echo input coming from a terminal
      -t timeout	time out and return failure if a complete line of
    		input is not read within TIMEOUT seconds.  The value of the
    		TMOUT variable is the default timeout.  TIMEOUT may be a
    		fractional number.  If TIMEOUT is 0, read returns
    		immediately, without trying to read any data, returning
    		success only if input is available on the specified
    		file descriptor.  The exit status is greater than 128
    		if the timeout is exceeded
      -u fd	read from file descriptor FD instead of the standard input

Linux sleep command to pause a bash script

We can also use the sleep command to pause the execution of the next command or task for a given number of seconds. The syntax is as follows:
sleep NUM
sleep NUM[suffix]

By default it will pause for NUMBER seconds but we can add [suffix] as follows:

  • s for seconds (the default)
  • m for minutes
  • h for hours
  • d for days

Unlike most implementations of sleep on Unix-like system that require NUMBER be an integer, GNU/pause command NUMBER may be an arbitrary floating point number. Given two or more arguments, pause for the amount of time specified by the sum of their values.

How to use the sleep command

To sleep for 3 seconds, enter:
sleep 3
One can sleep for 0.8 seconds:
sleep 0.8
In this final example, sleep for 1 minute and 42 seconds:
sleep 1m 42s
Bash add pause prompt using the sleep command:

read -t 10 "Sleeping with 10 seconds time out ..."
OR
echo "Sleeping with 10 seconds time out ..." && sleep 10

Please note that portable POSIX shell scripts must give sleep a single non-negative integer argument without a suffix. In other words the following is only valid:
sleep 10

Conclusion

Original DOS/XP pause command is an internal command. Use the above technique if you are migrating from DOS/Windows batch file scripting. Both the read command/sleep command used to pause the execution of the next action in script for a given amount of time. See GNU/sleep command man page here or by typing the following man command:
man sleep

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS

Comments on this entry are closed.

  • mne Mar 5, 2007 @ 10:41

    What about SLEEP command?

  • 🐧 nixCraft Mar 5, 2007 @ 14:46

    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.

  • bhaskar Mar 5, 2007 @ 16:37

    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

  • Gilles Allard Mar 5, 2007 @ 21:11

    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.

  • Smith Sep 1, 2007 @ 12:48

    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’.

    • Scott Dunn Jan 9, 2012 @ 23:21

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

  • anders Nov 5, 2007 @ 17:21

    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 🙂

  • Alison Mar 21, 2008 @ 5:51

    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.)

  • LeMMiNGS Jun 23, 2008 @ 2:35

    Cool to know that that works out aswell!!

  • Diven Aug 22, 2008 @ 0:23

    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
    }

  • christ Dec 9, 2008 @ 1:18

    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

  • g00ner Dec 30, 2008 @ 11:17

    Being a windows admin this was of great help.

  • Pai Mar 18, 2009 @ 10:25
    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
  • Sanchit Jan 4, 2010 @ 12:56

    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…

  • tom3k Feb 10, 2010 @ 12:06

    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

  • JW Aug 7, 2010 @ 19:32

    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..."
  • zulkaif dilawar May 27, 2011 @ 6:08

    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.

  • roger Jul 16, 2011 @ 1:39

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

  • Augustus Jul 26, 2011 @ 17:45

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

  • Bhanu Oct 23, 2011 @ 19:46

    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.

  • Spaldam Feb 4, 2012 @ 18:53

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

  • epileg Mar 2, 2012 @ 20:50

    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,

  • epileg Mar 2, 2012 @ 21:40

    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,

  • epileg Mar 2, 2012 @ 22:45

    And a simple way:

    #!/bin/bash
    read -s -n 1 -p "Press any key to continue..."
    echo
    ...
    

    Regards,

  • echo Nov 1, 2012 @ 0:27

    What about SLEEP command?

  • Unix Macher Mar 25, 2015 @ 15:13

    Doesn’t work on Debian 7, waste of time…

  • everton Sep 16, 2015 @ 11:47

    How to use sleep command??
    Please reply.

  • Elelei May 26, 2016 @ 22:30

    I would like to know how to STOP a program from pausing in the console. It happens all the time. I run a script, minimize the console, then check on it later and found that it has got stuck in a process. I maximize the window and the process resumes. I’ve had this issue for years. It seems to be some bug within BASH.

  • Ronald Jul 14, 2016 @ 11:30

    What the dificult? All this comands not work.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.