Bash add pause in shell script with bash pause command

Bash add pause in shell script with bash pause command

Most of you may be familiar with the good old DOS/2000/XP break team. Used to display a hint while batch script processing is paused. It is used in a batch file on a computer and allows the computer to pause the running batch file until the user presses any key. Let’s see how we can set a certain number of pauses in seconds/minutes/hour in our bash-based shell script before moving on to the next operation/command on Linux or Unix-like systems.


Bass pause command under Linux / UNIX / macOS

There is no pause command in the Linux/UNIX bash shell. You can simply use the playback control with the -p option to display the pause with the message.
Bash adds a pause instruction with the sleep and read command in shell scripts on Linux and Unix

Bash adds a pause prompt to the shell script with thebash-pause command.

For example:

read -p Press the [Enter] key to start saving.
read -p Press any key to continue the backup.
## Bash adds a 5-second pause ##
read -t 5 -p I only wait 5 seconds…

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

This interrupts shell script processing and displays a message asking the user to press [Enter] (or other key) to continue. The last example waits 5 seconds before the next command is executed. We can give the read command the -t option to set the value of the timeout. During transfer -s, we can instruct the controller not to read the terminal/keyboard echo input, as follows:

Function pause(){
read -s -s -n 1 -p Press any key to continue … .


## Pause

## the rest of the text below

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

Shell Pause function

You can create a function like this:

#! /bin/bash
# init
read -p $*

# …
# Call pause
Press the [Enter] key to continue …
# Rest of Scenario
# …

# init
function pause(){
read -p “$*”
# …
# call it
pause ‘Press [Enter] key to continue…’
# rest of the script
# …

Getting help with sleeping.

helping you sleep

-ARRAY cards read the words on successive indexes of array
of ARRAY variables, beginning with the zero value
-d, and continue until the first DELIM character is read instead of
, not a new line
– use Readline, To get line
-i text, use TEXT as start text to read line
-n nchars come back after reading the NCHARS characters, without waiting for line
for a new line, but taking into account the separator if you have read less than
NCHARS characters before, such as
-N nchars do not return until after reading the NCHARS characters, unless
EDF is found or a timeout is read, where all

-p separators in the query produce a PROMPT string without a new string before trying
ignore -r does not allow backslashes to map all
-s characters without echo input from the
terminal -t timeout and returns an error if the entire
input string is not read within seconds of TIMEOUT. The value of variable
TMOUT is the default timeout. TIME can be a fraction of
. If TIMEOUT is 0,
is read immediately without attempting to read the data, with
being returned successfully only if input is available from the specified file descriptor
. Output state greater than 128
when the timeout is greater than
-u fd is read from the FD file descriptor instead of the standard input.

Linux Sleep command to stopBash script

We can also use the sleep command to suspend the next command or task for a few seconds. The syntax is as follows:
Sleep NUMBER [suffix] By default it stops for NUMBER seconds, but we can add [suffix] as follows

  • seconds (default)
  • m in a few minutes
  • hourly
  • in a few days

Unlike most Unix-like standby implementations that require NUMBER to be an integer, the GNU/NUMBER command/pause can be a random floating point number. If there are two or more arguments, we pause for a time determined by the sum of their values.

How is the command Sleepused?

To sleep for 3 seconds, go inside:Sleep 3 Ability to sleep 0.8 seconds: sleep 0.8 In this last example, sleep 1 minute and 42 seconds:sleep 1m 42cBash adds an invitation to pause with the Sleep command.

Read -t 10 sleeps with 10 seconds time-out …
Echo Sleepy with a 10-second time-out… & sleep 10

read -t 10 “Sleeping with 10 seconds time out …”
echo “Sleeping with 10 seconds time out …” && sleep 10

Please note that portable POSIX shell scripts must return a single non-negative integer without a suffix if they are inactive. In other words, only the following is valid:
Afternoon sleep 10


The original DOS/XP pause command is an internal command. Use the technique described above when migrating from a DOS/Windows batch scenario. The two play/sleep commands are used to pause the next action in the script for a certain time. Look, look, look, look, look, look. GNU/sleep commands manual page here or by entering the following commands:
Person sleeps
Reading aid


Published: Vivek gears

The author is the creator of nixCraft and an experienced system administrator, DevOps engineer and Linux/Unix shell script trainer. Receive the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly by email.bash pause until keypress,wait command in shell script,shell script wait for command to finish,sleep command in unix shell script,bash sleep milliseconds,shell script pause wait,how to stop sleep command in linux,time sleep bash script

You May Also Like