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
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 30 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
I am working with RHEL server and this is exactly what I needed for my Tomcat app.
What the dificult? All this comands not work.
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.
How to use sleep command??
Please reply.
Doesn’t work on Debian 7, waste of time…
What about SLEEP command?
And a simple way:
Regards,
Sorry, previous one don’t properly works.
This do the job:
Regards,
There is a way to cleanly do that on Linux:
Best regards,
It’s not working for me. I’m not sure why. The loop just keeps going.
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.
The code worked except the comment should read “Press the ENTER key to continue…”.
Thank you!
Thanks worked perfectly. Wonder why they don’t have this as a bash command…
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.
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:
1856387884600b4070477a2_000005
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
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…
1856387884600b4070477a2_000004
Being a windows admin this was of great help.
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
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
}
Cool to know that that works out aswell!!
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.)
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 🙂
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’.
Thank you. I learned that the not so hard way. Trying it. 🙂
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.
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
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.
What about SLEEP command?