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:

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.

Roger Telco - 16 July 2005 [«« Reply to this]
Thanks, this is very useful! Needed this exact functionality for a script I was writing.
Ewynn - 13 February 2006 [«« Reply to this]
Nice, needed the python way of doing this.
Dion - 15 March 2006 [«« Reply to this]
John B. Cole - 12 June 2006 [«« Reply to this]
Thanks! I needed a quick way to do this in bash and your code works like a charm.
uli - 11 July 2006 [«« Reply to this]
helpful indeed
Anonymous - 05 August 2006 [«« Reply to this]
Very helpfull, needed the bash example
Works gr8! Thnx!
Diego - 04 September 2006 [«« Reply to this]
realy usefull. thanks
Bud - 06 October 2006 [«« Reply to this]
Nice, first hit on google for "bash read password" and exactly what i need :D
michael - 26 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 - 12 April 2007 [«« Reply to this]
thx !
robby - 14 April 2007 [«« Reply to this]
thanks for the tips!
nick - 25 October 2007 [«« Reply to this]
Perfect. I struggled for an hour before I found your method.
fritteli - 21 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 - 12 March 2008 [«« Reply to this]
Exactly what im looking for.
Greetz from germany,
AnilG - 11 July 2008 [«« Reply to this]
This page shows first hit in Google when searching:
reading password python
Anonymous - 04 September 2008 [«« Reply to this]
First page for "python read password" too. Thanks!
Anonymous - 24 November 2008 [«« Reply to this]
humpy101 - 30 December 2008 [«« Reply to this]
Brilliant! Thanks very much
Joshua Randall - 28 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 for the other options for the read builtin.

The following example should be functionally equivalent to yours:

read -p "Username: " uname
read -s -p "Password: " passw
Peter Bengtsson - 29 January 2009 [«« Reply to this]
Definitely a cleaner solution. Had I only known!
Richard Bronosky - 25 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 - 27 August 2009 [«« Reply to this]
@Joshua Randall, @Richard Bronosky Thanks a mill. Exactly what I was looking for.
Lanai - 10 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.
Matt - 05 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 - 21 January 2010 [«« Reply to this]
Hi Matt!

In a script, start with


instead of

K. Howe - 10 December 2009 [«« Reply to this]
Nice! Thanks.
Jess - 24 June 2010 [«« Reply to this]
Just what I was looking for.

Thanks so much.
Mike - 10 September 2010 [«« Reply to this]
Just perfect. Thank you so much!
Anonymous - 07 October 2010 [«« Reply to this]
thanks buddy........
Cyrus - 30 May 2011 [«« Reply to this]
You may also use this command :
read -s -p "Password: " mypassword

src :
Boris Kheyfets - 03 November 2012 [«« Reply to this]
Thank You! I was looking for a bash version.
Anonymous - 15 November 2012 [«« Reply to this]
Thanks !
Coder of Salvation - 20 November 2012 [«« Reply to this]
hi..I just wrote this in my .bashrc to improve security.

alias gcal='stty -echo; read -p "password: " password && stty echo && gcalcli --user --pw $password'
RayJ - 06 December 2012 [«« Reply to this]
Simple & works
David Castillo - 25 June 2013 [«« Reply to this]
how about if the password has special characters?

Your email will never ever be published