I want to write this bash loop for zsh
while true; do echo "print something"; read -p "pause"; done
This loop echos, then waits for the user to press enter. If I enter it as is, the read statement doesn't pause, causing zsh to infinitely echo "print something" without waiting for the user to press enter.
There is no pause command under Linux/UNIX bash shell. You can easily use the read command with the -p option to display pause along with a message.
You may ask how to use this in a script, but just like you would any other command, insert the cmd /c 'pause' command within your script to utilize the native functionality.
pause() causes the calling process (or thread) to sleep until a signal is delivered that either terminates the process or causes the invocation of a signal-catching function.
The ZSH shell is an extended version of the Bourne Again Shell; thus, most commands and scripts written for bash will work on ZSH. The ZSH shell provides full programming language features such as variables, control flow, loops, functions, and more.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With