Crosstips.org

My fun Crossword solver project. Crosstips.org & Krysstips.se

Kung Fu

Fujian White Crane Kung Fu

Fry-IT

Fry-IT is the company I work for

Photos

Photoalbum, both old and new.

Zope

What I have and am doing with Zope

Receptsamlingen

In Swedish only. About my "Collection of Recipes" website.

Contact me

My contact details and how to contact me.

 

KungFuPeople.com
Do you train Kung Fu?
Or know someone who does?
Then check out KungFuPeople.com


Mobile version of this page Mobile version of this page


 

Read in passwords with bash


passwords, username, password, getpass, passw, scripts, echo, bash, sh, stty

24th of March 2005

This has taken me some time to figure out because I couldn't find anything on Google. I think the problem was that I didn't know what to look for.

If you have a bash script that asks the user to enter their username and password you use the read function in sh. But when you read in the password you don't want it to show on the screen what you're writing. Someone could be leaning over your shoulder. Python has a similar standard library module called getpass which works like this:

 >>> from getpass import getpass
 >>> p = getpass("Password please: ")
 Password please: 
 >>> print "Your password is", len(p), "characters long"
 Your password is 5 characters long

That's fine if you do this via Python; but I needed to do it in one of my bash scripts. Here's how to do it:

 #!/bin/bash
 read -p "Username: " uname
 stty -echo
 read -p "Password: " passw; echo
 stty echo

Now, hopefully this will help other people who get stuck with the same problem.



Comment

27 comments so far
Roger Telco - 16th July 2005  [«« Reply to this]
Thanks, this is very useful! Needed this exact functionality for a script I was writing.
Ewynn - 13th February 2006  [«« Reply to this]
Nice, needed the python way of doing this.
Dion - 14th March 2006  [«« Reply to this]
thanks
John B. Cole - 12th June 2006  [«« Reply to this]
Thanks! I needed a quick way to do this in bash and your code works like a charm.
uli - 11th July 2006  [«« Reply to this]
helpful indeed
Anonymous - 4th August 2006  [«« Reply to this]
Very helpfull, needed the bash example
Works gr8! Thnx!
Diego - 4th September 2006  [«« Reply to this]
realy usefull. thanks
Bud - 6th October 2006  [«« Reply to this]
Nice, first hit on google for "bash read password" and exactly what i need :D
michael - 26th October 2006  [«« Reply to this]
Easier solution:
read -s -p "Password: " passwd
see also: the description for builtin commands in the bash man page (man bash or info bash)

also interesting is read -e ... to gain readline support for editing the input line
pierz - 11th April 2007  [«« Reply to this]
thx !
robby - 14th April 2007  [«« Reply to this]
thanks for the tips!
nick - 24th October 2007  [«« Reply to this]
Perfect. I struggled for an hour before I found your method.
fritteli - 20th January 2008  [«« Reply to this]
awesome, just what i was looking for! and reading michael's comment from oct 26th showed me an even more elegant solution. thanks a lot!
martin - 11th March 2008  [«« Reply to this]
THANKS!!!!
Exactly what im looking for.
Greetz from germany,
Martin
AnilG - 11th July 2008  [«« Reply to this]
This page shows first hit in Google when searching:
reading password python
Anonymous - 4th September 2008  [«« Reply to this]
First page for "python read password" too. Thanks!
Anonymous - 24th November 2008  [«« Reply to this]
Thanks!
humpy101 - 30th December 2008  [«« Reply to this]
Brilliant! Thanks very much
Joshua Randall - 28th January 2009  [«« Reply to this]
Note that you actually don't need to handle the terminal options with stty yourself -- the bash read builtin has an option that will do it for you [-s]. See http://www.ss64.com/bash/read.html for the other options for the read builtin.

The following example should be functionally equivalent to yours:

#!/bin/bash
read -p "Username: " uname
read -s -p "Password: " passw
Peter Bengtsson - 29th January 2009   [«« Reply to this]
Definitely a cleaner solution. Had I only known!
Richard Bronosky - 25th March 2009  [«« Reply to this]
Before doing a "stty -echo" or a "read -s", you should set trapping like so:
trap "stty echo; exit" INT TERM EXIT

Otherwise the script can exit in a state where the user can't see there keystrokes and then they get confused.
Token Paki - 27th August 2009  [«« Reply to this]
@Joshua Randall, @Richard Bronosky Thanks a mill. Exactly what I was looking for.
Lanai - 9th September 2009  [«« Reply to this]
Excuse me. Winning is important to me, but what brings me real joy is the experience of being fully engaged in whatever I'm doing.
I am from Arabia and also now am reading in English, give please true I wrote the following sentence: "This court was based generally that breeches could smuggle again of the speaker, whom they said based as an character or song of the settlement."

Thank you very much :-(. Lanai.
Ulf - 12th September 2009  [«« Reply to this]
Hey. All men are frauds. The only difference between them is that some admit it. I myself deny it. Help me! There is an urgent need for sites: Big documents to the full power gave usually.. I found only this - [URL=http://www.khuyennongvn.gov.vn/Members/Lacewigs/burmesevirgin-lace-wig]burmesevirgin lace wig[/URL]. Bargain lace wigs, they interviewed the hair also a unhappy lines before the pin filmed. Anne resembles porter that she is having his night and they sing to lead back convulsively. :cool: Thanks in advance. Ulf from Leone.
Matt - 4th December 2009  [«« Reply to this]
I constantly am getting this error:

read -s -p "Password: " passw
read: 4: illegal option -s

does anyone know why I can't get -s to work?
Anonymous - 21st January 2010   [«« Reply to this]
Hi Matt!

In a script, start with

#!/bin/bash

instead of

#!/bin/sh
K. Howe - 10th December 2009  [«« Reply to this]
Nice! Thanks.
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.