How to read 1 character in zsh?

I need to get exactly one character from the console, and not print it. I tried using read -en 1 as I used with bash. But this does not work at all. And vared doesn't seem to have that option.

How to read 1 character in zsh ? (I am using zsh v.4.3.11 and v.5.0.2 )

+4
source share
3 answers
 read -sk 

From the documentation :

-s

Do not play characters if you are reading from a terminal. Currently does not work with the -q option.

-k [num]

Read only one (or num) character. All are assigned to the first name without word separation. This flag is ignored when -q is present. The input is read from the terminal if only one of -u or -p is present. This option can also be used in zle widgets.

Please note that, despite the mnemonic key, this parameter reads full characters, which can consist of several bytes, if the MULTIBYTE parameter is set.

+5
source

If you want your script to be a little more portable, you can do something like this:

 y=$(bash -c "read -n 1 c; echo \$c") 
+1
source

Try something like

 read line c=`echo $line | cut -c1` echo $c 
0
source

All Articles