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.


Post your own comment
Roger Telco

Thanks, this is very useful! Needed this exact functionality for a script I was writing.


Nice, needed the python way of doing this.



John B. Cole

Thanks! I needed a quick way to do this in bash and your code works like a charm.


helpful indeed


Very helpfull, needed the bash example
Works gr8! Thnx!


realy usefull. thanks


Nice, first hit on google for "bash read password" and exactly what i need :D


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


thx !


thanks for the tips!


Perfect. I struggled for an hour before I found your method.


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!


Exactly what im looking for.
Greetz from germany,


This page shows first hit in Google when searching:
reading password python


First page for "python read password" too. Thanks!




Brilliant! Thanks very much

Joshua Randall

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

Definitely a cleaner solution. Had I only known!

Richard Bronosky

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

@Joshua Randall, @Richard Bronosky Thanks a mill. Exactly what I was looking for.


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.


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?


Hi Matt!

In a script, start with


instead of


K. Howe

Nice! Thanks.


Just what I was looking for.

Thanks so much.


Just perfect. Thank you so much!


thanks buddy........


You may also use this command :
read -s -p "Password: " mypassword

src :

Boris Kheyfets

Thank You! I was looking for a bash version.


Thanks !

Coder of Salvation

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'


Simple & works

David Castillo

how about if the password has special characters?


Please change " it's content is outdated" to "its content is outdated"

Peter Bengtsson

Thank you!

Your email will never ever be published.

Related posts

Go to top of the page