Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pause in zsh?

Tags:

bash

zsh

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.

like image 658
k107 Avatar asked Mar 07 '11 02:03

k107


People also ask

How do you pause in Shell?

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.

How do you pause a terminal script?

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.

What is the pause command in Linux?

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.

Can I use bash commands in zsh?

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.


1 Answers

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
like image 200
Rodolfo Carvalho Avatar answered Nov 03 '22 08:11

Rodolfo Carvalho