17

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
[...]
1

5 Answers 5

11

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
5
  • 14
    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, 2012 at 17:32
  • @Lekensteyn - That's without a trailing newline though.
    – hookenz
    Mar 15, 2012 at 18:36
  • @MattH Just like echo -n and printf. If you press Enter, you will get a newline.
    – Lekensteyn
    Mar 15, 2012 at 19:54
  • 1
    that does not work for ubuntu 12.04 - read requires argumenst
    – ses
    May 14, 2014 at 20:09
  • 1
    @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, 2014 at 1:33
2

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
1

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"
1
  • How does one send the CONT signal to the script? Dec 31, 2015 at 12:00
1

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! :)

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

1
  • 1
    -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? Dec 31, 2015 at 12:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .