
Do you train Kung Fu?
Or know someone who does?
Then check out KungFuPeople.com
Mobile version of this pageAdd links to a text (take III)
Next:
More amazing sidewalk art
Related blogs
File check before deleteCatching a carriage return in bash
pwdf - a mix of ls and pwd
Overcomplicated password requirements on Oystercard.com
Encrypted files in Emacs
Classic Movie Scripts
ztar - my wrapper on tar -z
type - Writing shell scripts
Related by category
Read in passwords with bash
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.
Tweet
Comment
28 comments so farThanks! 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!
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
Definitely a cleaner solution. Had I only known!
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.
@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.
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.
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
#!/bin/bash
instead of
#!/bin/sh


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