30
8
Edit:
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:
read.sh
#!/bin/bash
test() {
read -p "Here be dragons. Continue?" -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "You asked for it..."
fi
}
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
Edit::
@hennes
- I want the function to be in a config file, so I can call it from whatever directory (ideally my .bashrc or .zshrc)
- I've corrected the formatting of the first commented line. Problem still exists in
zsh
- Bash version is 3.2, but you've helped me figure out that the problem is with zsh and not bash.
@NickTomlin Do you know how to make it work without having to put Enter after 'y' (under ZSH)? – syntagma – 2015-04-16T10:27:49.877
@REACHUS alas, I do not. Good luck! – Nick Tomlin – 2015-04-16T13:01:25.823
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 – 2018-10-20T12:33:02.600
>
#! /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)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! – Nick Tomlin – 2013-02-22T17:33:06.847How 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 – 2013-02-22T20:48:30.637