1833752 Members
2722 Online
110063 Solutions
New Discussion

Passwords

 
SOLVED
Go to solution
Neil Edwards
Advisor

Passwords

Hello,

Is there a way to enter a password from within a shell script and not have it echoed to the screen?

Thank you, Neil
It wasn't me.
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Passwords

Hi Neil:

You bet. The trick is to use stty -echo.

#!/usr/bin/sh

trap 'stty echo; exit' 0 1 2 3 15


echo "Enter Password: \c"
stty -echo
read PASSWD
stty echo

Regards, Clay

If it ain't broke, I can fix that.
Craig Rants
Honored Contributor

Re: Passwords

Look at the expect programming language.

http://hpux.ee.ualberta.ca/hppd/hpux/Tcl/expect-5.33/

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
A. Clay Stephenson
Acclaimed Contributor

Re: Passwords

Hi again Neil:

I should add that the trap command is really not optional. You see, the stty command alters the tty device not just your script. Any subsequent processes would also have the echo turned off if you exited in the -echo state. The trap, insure that the terminal is restored upon exit even via a Ctrl-C (or whatever your n'rupt is set to).

I suggest that you comment out the trap statement and do a ctrl-c when entering the password and you will see just what I mean.

Regards, Clay

If it ain't broke, I can fix that.
Peter Kloetgen
Esteemed Contributor

Re: Passwords

Hi Neill,

there is another command which I use very often in shell scripts:

tput --> a command which can be used to take influence on the behaviour of your cursor (and more)

man tput will show you the possibilities of the command. You will find a "invisible"- button which makes the entered textstring invisible at the screen, after reading the input you make the input visible again -- that's it!

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Steven Sim Kok Leong
Honored Contributor

Re: Passwords

Hi,

I think I have found the elegant solution you wanted:

Execute this:

# ps -fp `echo $$`

$ ksh
$ ps -fp `echo $$`
UID PID PPID C STIME TTY TIME COMMAND
user1 16088 16079 0 15:01:49 pts/0 0:00 ksh

$ csh
sys 21: ps -fp `echo $$`
UID PID PPID C STIME TTY TIME COMMAND
user1 16334 16088 0 15:06:28 pts/0 0:00 csh

kite 22: sh
$ ps -fp `echo $$`
UID PID PPID C STIME TTY TIME COMMAND
user1 16345 16334 0 15:06:40 pts/0 0:00 sh

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: Passwords

Hi,

Eh what happened?!

I was responding to another thread and my response ended up in this thread instead. :{

In any case, I agree with Clay that stty -echo and stty echo does the job nicely for "invisible" passwords. I used the following in one of my scripts:

echo "Enter passwd: \c"
trap "" 1 2 3
stty -echo
read passwd
stty echo
trap 1 2 3

Hope this helps. Regards.

Steven Sim Kok Leong