Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Reading without echo?



On May 9, 10:23am, Peter Stephenson wrote:
} Subject: Re: Reading without echo?
}
} John S Cooper <John.Cooper@xxxxxxxxxxxxx> wrote:
} > What's the recommended way for a zsh script to read (with prompt) a
} > password without having it echo'd?
} 
} You need to do something like:
} 
}   stty -echo; read pw?'Password> '; stty echo; print

The problem with this (aside from the "no match" error because the `?'
is outside the quotes) is that if you interrupt it, e.g. with ^C, the
terminal may be left in no-echo mode for some period of time.  However,
ZLE does eventually force echo back on (even for external commands and
even when the tty is not frozen with ttyctl, which surprises me a bit).

} There's no internal support for this (actually that's not quite true since
} the zftp module can read passwords, but there's no way to this from the
} main shell).

Actually, there is internal support for this, of a sort.  Look at the STTY
parameter.  Unfortunately, STTY applies only to external commands, not
shell builtins, so you can't just do `STTY=-echo read ...'.

This should probably be considered a bug, because zsh does execute the
`stty -echo' command even though the resulting settings don't apply to
the `read' when it happens.

Anyway, something like

    print -n 'Password> '; STTY=-echo head -1 | read pw

is supposed to safely set and restore no-echo mode.  However, I just tried
it, and it has the same problem as Peter's suggestion -- the tty modes are
not restored if the command is interrupted.  (To see this happen, you must
`unsetopt ZLE'.)

In fact, `ttyctl -f' seems to be nonfunctional when ZLE is disabled, too.

So the only way I could actually "recommend" for doing this is along the
lines of:

    getpass () {
	setopt localoptions localtraps
	trap "stty echo" EXIT HUP INT QUIT
	{ stty -echo
	  read "$@"
	  print
	} </dev/tty >&/dev/tty
    }

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   



Messages sorted by: Reverse Date, Date, Thread, Author