How to read passwords in bash scripts in a way like tools which are not showing it on terminal ?
(Changing font to black on black might be tricky by easy to copy&paste - so it's not solution)
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
I always used
stty -echo to turn echoing off, then read and afterwards do
stty echo (read more by viewing man of
stty - i.e.
man stty). This is more useful from a programmers perspective as you can turn echoing off and then read a password from a programming language such as Java, C(++), Python, etc. with their standard stdin "readers."
In bash, the usage could look like this:
echo -n "USERNAME: "; read uname echo -n "PASSWORD: "; stty -echo; read passwd; stty echo; echo program $uname $passwd passwd= # get rid of passwd
Python, for example, would look like:
from sys import stdout from os import system as term uname = raw_input("USERNAME: ") # read input from stdin until [Enter] in 2 stdout.write("PASSWORD: ") term("stty -echo") # turn echo off try: passwd = raw_input() except KeyboardInterrupt: # ctrl+c pressed raise SystemExit("Password attempt interrupted") except EOFError: # ctrl+d pressed raise SystemExit("Password attempt interrupted") finally: term("stty echo") # turn echo on again print "username:", uname print "password:", "*" * len(passwd)
I had to do this a lot of times in Python, so I know it pretty well from that perspective. This isn't very hard to translate to other languages, though.