Seems to work within bash. It appears the problem is related to zsh. If there is a better site to post this issue on let me know.

I am writing a simple script that creates a series of directories. I want the user to give a confirmation before I do so. I'm using the following as a basis, but cannot seem to get it to run within a bash function. If I put it outside of a function it works fine. Here is an isolated example:


test() {
  read -p "Here be dragons. Continue?" -n 1 -r
  if [[ $REPLY =~ ^[Yy]$ ]]
    echo "You asked for it..."

code from this SO post.

Sourcing the file and/or test results in the following error: read:1: -p: no coprocess. Same output when I place it in my .bashrc



  1. I want the function to be in a config file, so I can call it from whatever directory (ideally my .bashrc or .zshrc)
  2. I've corrected the formatting of the first commented line. Problem still exists in zsh
  3. Bash version is 3.2, but you've helped me figure out that the problem is with zsh and not bash.
  • 1) Is there any place where you actually call the function test() ? 2) #! /bin/bash with a space behind she shebang? I thought those were not allowed? 3) '#!/usr/bin/env bash' is probably better. 4) Which bash version? (I tested with bash 4.1.10 on FreeBSD and got no errors)
    – Hennes
    Feb 22, 2013 at 17:05
  • I improperly tagged the question, I'm using zsh but didn't think that there would be conflict with bash scripts (i have several other similar scripts running all right). I've retagged it, and responded to your questions. Thanks for pointing me in the right direction! Feb 22, 2013 at 17:33
  • How do you start the program? The shebang (#!)interpreter` line is only needed if you want to execute the file with ./read.sh. It is not needed it you run it in any other way or if you put it in an startup script. And if you want to run it in zsh with `zsh ./read.sh' then the lines starting with a # get ignored. They also confuse since their give the impression that you use bash when you use zsh).
    – Hennes
    Feb 22, 2013 at 20:48
  • @NickTomlin Do you know how to make it work without having to put Enter after 'y' (under ZSH)?
    – syntagma
    Apr 16, 2015 at 10:27
  • 1
    A little advice. Naming a function 'test' is actually a bad idea... 'test' is actually a shell built-in in both zsh and bash... So your shell will either use the built-in when you try to call your own function, or any scripts run in the same instance after you declare the function will get in trouble (if they rely on the shell built-in)...
    – svin83
    Oct 20, 2018 at 12:33

3 Answers 3


The –p option doesn’t mean the same thing to bash’s read built-in command and zsh’s read built-in command.  In zsh’s read command, –p means –– guess –– “Input is read from the coprocess.”  I suggest that you display your prompt with echo or printf.

You may also need to replace –n 1 with –k or –k 1.

The zsh equivalent of bash's read -p prompt is

read "?Here be dragons. Continue?"

Anything after a ? in the first argument is used as the prompt string.

And of course you can specify a variable name to read into (and this may be better style):

read "brave?Here be dragons. Continue?"
if [[ "$brave" =~ ^[Yy]$ ]]

(Quoting shell variables is generally a good idea, too.)

  • Thanks for the explanation of why this is not functioning properly. I will check for differences like this in the future. Feb 22, 2013 at 22:04
  • Great explanation, thank's! Had the exact same problem
    – iosifv
    Jan 25, 2019 at 13:25
  • What does =~ mean in your answer?
    – gaoagong
    May 25, 2021 at 21:55
  • @gaoagong: In bash, [[ string =~ regex ]] tests whether the string matches the regular expression. Specifically, [[ "$brave" =~ ^[Yy]$ ]] tests whether $brave (the user input from the read) is either Y or y. May 26, 2021 at 1:12
  • 4
    So frustrating that this information is not given in zsh when you check man read... Thank you very much
    – ossbuntu
    Jan 28, 2022 at 11:37

This code seems to do what you want in zsh.
(Note that the question you refered to explicitly mentions it is for bash).

#!/usr/bin/env zsh

  echo -n "Here be dragons. Continue?"
  read REPLY

  if [[ $REPLY =~ ^[Yy]$ ]]
    echo "You asked for it..."


Three comments:

  • Thank you for your help. I've given the answer to @scott because of the explanation of why this is not functioning, but the snippet you've given me works perfectly. Feb 22, 2013 at 22:05
  • 1
    Just three more things: This was probably best asked on SO (as it is a programming question). 2) Not all shells are compatible (man zsh returns different stuff for read -p then man bash does. 3) Another solution would be to searchj for vared -c -p 'prompt question'
    – Hennes
    Feb 22, 2013 at 22:10
  • +1 Thanks for letting me know. I'll direct future programming-centric questions to SO. Feb 22, 2013 at 22:31
  • On the last line of this function you should add REPLY="" to clear the REPLY variable. Otherwise if you echo $REPLY you will see it is still set until you open or close your terminal or set it again. Sep 26, 2019 at 16:48

This version allows you to have more than one case y or Y, n or N

  1. Optionally: Repeat the question until an approve question is provided

  2. Optionally: Ignore any other answer

  3. Optionally: Exit the terminal if you want

    confirm() {
        echo -n "Continue? y or n? "
        read REPLY
        case $REPLY in
        [Yy]) echo 'yup y' ;; # you can change what you do here for instance
        [Nn]) echo 'nope n' ;;
        # Here are a few optional options to choose between
        # Any other answer:
        # 1. Repeat the question
        *) confirm ;;
        # 2. ignore
        # *) ;;
        # 3. Exit terminal
        # *) exit ;;
        # REPLY=''

Notice this too: On the last line of this function clear the REPLY variable. Otherwise if you echo $REPLY you will see it is still set until you open or close your terminal or set it again.

  • 1
    This work both with bash and zsh !
    – ponsfrilus
    Aug 4, 2020 at 13:46

You must log in to answer this question.

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