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.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix terminal
Category Linux shell scripting
OS compatibility BSD Linux macOS Unix
Est. reading time 3 minutes
Advertisement

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

Try the read command. The syntax is as follows:

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

How do I pause my shell script for a second before continuing?

Let us say we want to wait for 15 seconds before time out and then continuing execution. Then pass the -t seconds as follows:

# get confirmation
read -t 15 -N 1 -p "This will update nginx config FRONTEND on $servers. Continue (y/N)? " answer
echo 
 
# if answer is yes within 15 seconds start updating cluster nodes ...
if [ "${answer,,}" == "y" ]
then
    push_config_to_nodes "$servers"
    reload_nodes "$servers"
fi

If you are calling above script from another script, then you can by default pass the ‘y’ answer using the yes command:
$ /bin/yes y | /path/to/above/script.sh

Getting help about the read command

Type the help command as follows:
$ help read
Here is what I see on GNU bash version 5.0.17 running on the Ubuntu 22.04 LTS:

      -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/Windows 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 or help command. For example:
$ man sleep
$ help sleep

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

34 comments… add one
  • 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,

    • Anonym Jan 24, 2021 @ 20:05

      Perfect, that’s exactly what I needed and the only approach that worked for me. Notice, that also this should read “Press …” instead of “any key”.

      Still kudos 🙂

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

  • Archu Jan 8, 2021 @ 20:45

    I am working with RHEL server and this is exactly what I needed for my Tomcat app.

  • Ano Nym Jan 24, 2021 @ 19:52

    Unfortunately returns this:
    XXX.sh: 4: read: Illegal option -n

  • Katrina Aug 25, 2023 @ 11:28

    Bash is useful for my tasks running on my macOS M1 MacBook Pro. I switched to Bash because all EC2 servers have Bash as their default shell. Ha!

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.