Mobile version of this pageAdd links to a text (take III)
Next:
More amazing sidewalk art
Related blogs
type - Writing shell scriptsClassic Movie Scripts
Overcomplicated password requirements on Oystercard.com
ztar - my wrapper on tar -z
Catching a carriage return in bash
File check before delete
pwdf - a mix of ls and pwd
Encrypted files in Emacs
Related by category
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:
>>> 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.
Comment
Thanks! I needed a quick way to do this in bash and your code works like a charm.
Very helpfull, needed the bash example
Works gr8! Thnx!
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
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!
THANKS!!!!
Exactly what im looking for.
Greetz from germany,
Martin
This page shows first hit in Google when searching:
reading password python
First page for "python read password" too. Thanks!







Save this page in del.icio.us
Thanks, this is very useful! Needed this exact functionality for a script I was writing.