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.

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

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 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 VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

Comments on this entry are closed.

  • Ronald Jul 14, 2016 @ 11:30

    What the dificult? All this comands not work.

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

  • everton Sep 16, 2015 @ 11:47

    How to use sleep command??
    Please reply.

  • Unix Macher Mar 25, 2015 @ 15:13

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

  • echo Nov 1, 2012 @ 0:27

    What about SLEEP command?

  • epileg Mar 2, 2012 @ 22:45

    And a simple way:

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

    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 @ 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,

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