26

I'm writing a Bourne shell script and have a password input like this:

echo -n 'Password: '
read password

Obviously, I don't want the password being echoed to the terminal, so I want to turn off echo for the duration of the read. I know there's way to do this with stty, but I'll ask the question for the benefit of the community whilst I go read the manpage. ;)

Josh Glover
  • 25,142
  • 27
  • 92
  • 129
  • 2
    possible duplicate of [How to get a password from a shell script without echoing](http://stackoverflow.com/questions/3980668/how-to-get-a-password-from-a-shell-script-without-echoing) – javaPlease42 Jun 25 '15 at 23:02

4 Answers4

55
stty_orig=`stty -g`
stty -echo
echo 'hidden section'
stty $stty_orig
Sandro Munda
  • 39,921
  • 24
  • 98
  • 123
  • 2
    This is certainly the way I've done it before, if memory serves. Just out of curiosity, is this any different from `stty -echo; read foo; stty echo`? – Josh Glover Apr 12 '11 at 12:14
  • 4
    Yes, it leaves echo switched off afterwards in case it had been switched off already. – ak2 Apr 12 '11 at 15:18
  • i am new to unix. Could u explain about what exactly stty and what this code is doing – Abhishek Gupta Jul 18 '12 at 14:41
  • More general, better than Wikipedia when you're new to unix @AbhishekGupta: 1. learn how to scroll, search, and quit `less` (or `more`). 2. `man whateveryouwonderabout` or, for bash builtin commands such as "set", `help set`. Good luck on your journey ;-) – conny Jun 13 '19 at 14:23
17

read -s password works on my linux box.

Rumple Stiltskin
  • 9,597
  • 1
  • 20
  • 25
6

You can use '-s' option of read command to hide user input.

echo -n "Password:"
read -s password
if [ $password != "..." ]
then
        exit 1; # exit as password mismatched #
fi

Also you can use 'stty -echo' if you want to hide from terminal to print. And restore the terminal settings using "stty echo"

But I think for getting password input from user 'read -s password' is more than enough.

PravinY
  • 500
  • 1
  • 5
  • 9
0

Bourne Shell Script :

#!/bin/sh

# Prompt user for Password
echo -n 'Password: '

# Do not show what is being typed in console by user
stty -echo

# Get input from user and assign input to variable password
read password

# Show what is being typed in console
stty echo

stty manual command for more information:

@:/dir #man stty

stty manual snippets:

 STTY(1)              stty 5.2.1 (March 2004)              STTY(1)

     NAME
          stty - change and print terminal line settings

     SYNOPSIS
          stty [-F DEVICE] [--file=DEVICE] [SETTING]...
          stty [-F DEVICE] [--file=DEVICE] [-a|--all]
          stty [-F DEVICE] [--file=DEVICE] [-g|--save]

     DESCRIPTION
          Print or change terminal characteristics.

          -a, --all
               print all current settings in human-readable form

          -g, --save
               print all current settings in a stty-readable form

          -F, --file=DEVICE
               open and use the specified DEVICE instead of stdin

          --help
               display this help and exit

          --version
               output version information and exit

          Optional - before SETTING indicates negation.  An * marks
          non-POSIX settings.  The underlying system defines which
          settings are available.



   Local settings:

          [-]echo
               echo input characters
javaPlease42
  • 4,699
  • 7
  • 36
  • 65