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/bash
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.

Comments

Post your own comment
Roger Telco

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

Ewynn

Nice, needed the python way of doing this.

Dion

thanks

John B. Cole

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

uli

helpful indeed

Anonymous

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

Diego

realy usefull. thanks

Bud

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

michael

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

thx !

robby

thanks for the tips!

nick

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

fritteli

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

THANKS!!!!
Exactly what im looking for.
Greetz from germany,
Martin

AnilG

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

Anonymous

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

Anonymous

Thanks!

humpy101

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

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.

Lanai

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

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

Hi Matt!

In a script, start with

#!/bin/bash

instead of

#!/bin/sh

K. Howe

Nice! Thanks.

Jess

Just what I was looking for.

Thanks so much.

Mike

Just perfect. Thank you so much!

Anonymous

thanks buddy........

Cyrus

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

src : http://bash.cyberciti.biz/misc-shell/shel-to-accept-password/

Boris Kheyfets

Thank You! I was looking for a bash version.

Anonymous

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 foo@gmail.com --pw $password'

RayJ

Beautiful
Simple & works

David Castillo

how about if the password has special characters?

Anonymous

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