Kung Fu Kung Fu

Fujian White Crane Kung Fu

Zope Zope

What I have and am doing with Zope

Photos Photos

Photoalbum, both old and new.

Receptsamlingen Receptsamlingen

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

Contact me Contact me

My contact details and how to contact me.

  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/sh
 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

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!
 
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.