Using zsh, I'm trying to put a step in my ~/.zprofile
where I interactively ask a yes/no style question. At first I tried this bash-style approach, but I saw errors of this form:
read: -p: no coprocess
(I'm aware that typically the zsh syntax is different from bash's - I tried preceding it with a sh emulation command - emulate -LR sh
- but it made no difference).
This page implied the syntax might be different, so guided by this page and the zsh man page, I tried this instead:
read -q REPLY?"This is the question I want to ask?"
This instead fails with an error of the form:
/home/user/.zprofile:5: no matches found: REPLY?"This is the question I want to ask?"
How can I ask a simple yes/no question with zsh? Ideally the command would just swallow one character, with no need to press Enter/Return, and be 'safe' - i.e. the subsequent test defaults to no/false unless 'Y' or 'y' are entered.
From zsh - read
You must quote the entire argument
this will prompt you with
This is the question I want to ask?
and return the character pressed inREPLY
.If you don't quote the question mark,
zsh
tries to match the argument as a filename. And if it doesn't find any matching filename, it complains withno matches found
.I'm adding this answer because every time you want to ask the user for confirmation, you also want to act on it. here's a function that prompts with
read -q
(thanks, other answers!) and branches on the result to do what you want (in this case, git stuff):See ZSH Manual for documentation of ZSH's read. Try: