Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Zsh's read for some reason is echo'ing ^M's instead of accepting them as <Enter> keystrokes. (If -d is set, then they are recognized as <Enter>'s, but still echoed.) It also doesn't support basics like the backspace key.

I can get around this by hacking / running bash,

> a=$(bash -c 'read -e -p "What would you like to do?: " tmp; echo $tmp')
What would you like to do?: eat cake
> echo $a                                                                
eat cake

but I'm wondering if there's a cleaner way.

share|improve this question
    
Cannot confirm on a fresh zsh 4.3.10 install; % read a works fine here. –  grawity May 22 '12 at 22:04
    
This is using iterm on OS X; read does work on Linux machines. –  Nicholas Tung May 22 '12 at 22:37
add comment

1 Answer

To input a line of text comfortably under zsh, use vared. Using vared instead of read invokes zle, which is the equivalent of passing -e in bash to invoke readline.

tmp=
vared -p 'What would you like to do?: ' tmp

The behavior you describe with plain read looks like a misconfigured terminal rather than a shell issue. Run stty -a to show your terminal settings, and make sure that eol is set to ^M and erase is set to what your Backspace key sends. Depending on the operating system and how it's set up and on the terminal, Backspace sends either ^H or ^?. The backspace setting usually goes wrong because of some configuration file that tries to set it manually, so the first thing you should do is track and remove any such misconfiguration. If you find none, review your terminal emulator's settings to check that it isn't set into some historical compatibility mode. If all else fails, add something like this to your ~/.zshrc:

if [[ $(ps -o comm= $PPID) = iterm ]]; then
  stty erase '^?'
fi
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.