In zsh
:
read -s -k '?Press any key to continue.'
From man zshbuiltins
:
-s
Don't echo back characters if reading from the terminal.
-k
Read only one character.
name?prompt
Name is omitted, thus user input is stored in the REPLY
variable (and we ignore it). The first argument contains a ?
, thus the remainder of this word is used as a prompt on standard error when the shell is interactive.
To include a newline after the prompt:
read -s -k $'?Press any key to continue.\n'
$''
is explained under QUOTING
in man zshmisc
.
Finally, a pause
function that takes an arbitrary prompt message 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
_
doesn't work: zsh throws an errorzsh: read-only variable: _
. Anything else works though. Thanks for the tip. – Cuttlefish