Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need a pause on a shell script to show a warning before continuing. For instance, on DOS it goes like this:

doit.bat:

[...]
echo 'Are you sure? Press Ctrl-C to abort, enter to continue.'
pause
[...]

How can I do this on bash? For the moment a sleep command seems to do the trick and is simple enough but is not exactly the idea:

doit.sh

[...]
echo 'Are you sure? Press Ctrl-C to abort.'
sleep 3
[...]
share|improve this question
up vote 8 down vote accepted

something along the lines of

echo -n "prompt"  #'-n' means do not add \n to end of string
read              # No arg means dump next line of input
share|improve this answer
11  
In Bash, this can be shortened to read -p "prompt". More details on shell built-ins can be retrieved with the help command, e.g. help read. – Lekensteyn Feb 27 '12 at 17:32
    
@Lekensteyn - That's without a trailing newline though. – Matt H Mar 15 '12 at 18:36
    
@MattH Just like echo -n and printf. If you press Enter, you will get a newline. – Lekensteyn Mar 15 '12 at 19:54
    
that does not work for ubuntu 12.04 - read requires argumenst – ses May 14 '14 at 20:09
    
@ses On 12.04.4 using bash there is no requirement that read have an argument. Using sh (which often invokes bash in sh compatible mode) read requires an argument. If you start your script with #!/bin/sh you will see the latter behavior – Huckle May 15 '14 at 1:33

read -p "Press any key to continue or CTRL-C to abort" works fine under 14.04 in my scripts. As @Lekensteyn stated in the comment above it seems this has been the case since 12.04.4. The 'help read' page states:

-p prompt   output the string PROMPT without a trailing newline before
            attempting to read
share|improve this answer

My favourite use of this is to set a default timeout, so that the script can also run unattended:

echo "Press any key to continue..."
read -n1 -t5 any_key

where:

  • -n1 tells the read to accept any single character
  • -t5 tells it to wait a maximum of 5 seconds for input

I work almost exclusively with Bash under CentOS, so I can't vouch for this working on other Linux variants, but since it's Bash, I would expect it to work. I'd love to know for certain if it does! :)

share|improve this answer

The simples wait to stop the script is to call kill -STOP $$ from the script: After paused the script will continue its work after receiving -CONT signal.

#!/bin/bash
echo "$0 stopping now"
kill -STOP $$
echo "$0 continues its work"
share|improve this answer
    
How does one send the CONT signal to the script? – David Foerster Dec 31 '15 at 12:00
echo "press enter to continue"
read unusedVariable
echo "running these 3 lines in .sh file, this echo will never be seen, unless you have a really slow computer"

place these 3 lines in a .sh file and run it

share|improve this answer
    
-1 What is this supposed to do and why? Why would the output of the last echo command not show up and what does it have to do with the speed of the computer? – David Foerster Dec 31 '15 at 12:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.