How to pause in zsh?

I want to write this bash loop for zsh

while true; do echo "print something"; read -p "pause"; done 

This echo loop then waits for the user to press the enter key. If I enter it as is, the read instruction does not pause, causing zsh to echo "print something" endlessly, without waiting for the user to press the enter button.

+6
bash zsh
source share
4 answers

It looks like -p doing something else in zsh . You probably need something like read some_variable\?pause .

+10
source share

Since this concerns the only search result that I could find, and I found it useful, but still a bit confusing, here is another way to put it: if all you want to do is echo a line of text and the user enters to wait .. . read \?"I am waiting for you to press [Enter] before I continue."

+9
source share

In zsh :

 read -s -k '?Press any key to continue.' 

From man zshbuiltins :

  • -s Do not select characters when reading from the terminal.
  • -k Read only one character.
  • name?prompt The name is omitted, so user input is stored in the REPLY variable (and we ignore it). The first argument contains ? , so the rest of this word is used as a hint about the standard error when the shell is interactive.

To include a new line after the prompt:

 read -s -k $'?Press any key to continue.\n' 

$'' is explained in QUOTING in man zshmisc .

Finally, the pause function, which accepts an arbitrary prompt in a script that does what the OP asks:

 #!/usr/bin/env zsh pause() read -s -k "?$*"$'\n' while true; do echo "print something" pause "pause" done 
+1
source share
 #!/bin/zsh pause() { echo "$*"; read -k1 -s } 

Now we can call the function with any invitation text:

 pause "paused! press any key to continue" pause "you can write anything here :)" 
0
source share

All Articles