# No echo

**URL:** <https://rubytalk.org/t/no-echo/5099>\
**Category:** ruby-talk\
**Created:** [11 March 2003 09:30 UTC](https://rubytalk.org/t/no-echo/5099 "2003-03-11T09:30:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Szymon\_Drejewicz1](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@Szymon\_Drejewicz1](https://rubytalk.org/u/Szymon_Drejewicz1)\
**Post date:** [11 March 2003 09:30 UTC](https://rubytalk.org/t/no-echo/5099/1 "2003-03-11T09:30:12Z")

</div>

How to get string from console without echo? I need it for password reading.

Szymon Drejewicz

---

<div class="post-metadata">

**Author:** ![Josef\_Jupp\_SCHUGT](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@Josef\_Jupp\_SCHUGT](https://rubytalk.org/u/Josef_Jupp_SCHUGT)\
**Post date:** [11 March 2003 15:08 UTC](https://rubytalk.org/t/no-echo/5099/2 "2003-03-11T15:08:57Z")

</div>

EHLO

- On 2003-03-11 13:57

> How to get string from console without echo? I need it for password  
> reading.

Use termios available at

[http://raa.ruby-lang.org/list.rhtml?name=ruby-termios](http://raa.ruby-lang.org/list.rhtml?name=ruby-termios)

Sample usage (note that the input routine has the potential of  
showing a strange behavior: When a star is shown for every password  
character, the stars do not fit into one line and you use backspace  
the stars in the first line will not vanish even if the corresponding  
characters are deleted):

require ‘termios’ # Use termios for non-echoing input of password

BS = 8  
LF = 10  
DEL = 127

> **···**
>
> - Szymon Drejewicz [drejewic@wsisiz.edu.pl](mailto:drejewic@wsisiz.edu.pl) wrote:
> 
> #==============================================================================
> 
> # Prompt for and reads in a password. When `stars’ is true display a star for
> 
> # each character when the password is entered, otherwise do not provide any
> 
> # visual feedback. BS (Backspace) and DEL (Delete or rubout) can be used to
> 
> # edit input.
> 
> #==============================================================================  
> def read\_pass(stars)  
> pass = ‘’
> 
> $stdin.extend Termios  
> oldt = $stdin.tcgetattr  
> newt = oldt.dup  
> newt.lflag &= ~(Termios::ECHO | Termios::ICANON)  
> $stdin.tcsetattr(Termios::TCSANOW, newt)
> 
> loop {  
> c = $stdin.getc  
> break if c == LF  
> if (c == BS or c == DEL) and (not pass.empty?)  
> pass.chop!  
> print “#{BS.chr} #{BS.chr}” if stars  
> else  
> pass \<\<= c.chr  
> print ‘\*’ if stars  
> end  
> }
> 
> $stdin.tcsetattr(Termios::TCSANOW, oldt)  
> print “\n”
> 
> return pass  
> end
> 
> HTH
> 
> ## Josef ‘Jupp’ Schugt
> 
> .---------------------. .------..—..-----..—..-----.
> 
> > [http://jupp.tux.nu/](http://jupp.tux.nu/) | | jupp || @ || gmx || . || net |  
> > `---------------------' `------‘`---'`-----’`---'`-----’

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [12 March 2003 01:35 UTC](https://rubytalk.org/t/no-echo/5099/3 "2003-03-12T01:35:56Z")

</div>

Thanks for the demonstration, Josef. I often wondered myself how to  
do it. Your program is archived at

[http://www.rubygarden.org/ruby?PasswordWithoutEcho](http://www.rubygarden.org/ruby?PasswordWithoutEcho)

Gavin

> **···**
>
> On Wednesday, March 12, 2003, 2:08:57 AM, Josef wrote:
> 
> > EHLO
> 
> > - On 2003-03-11 13:57
> > - Szymon Drejewicz [drejewic@wsisiz.edu.pl](mailto:drejewic@wsisiz.edu.pl) wrote:
> > 
> > > How to get string from console without echo? I need it for password  
> > > reading.
> 
> > Use termios available at …
