How do I turn off echo in a terminal?

后端 未结 4 1986
青春惊慌失措
青春惊慌失措 2020-12-09 09:42

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 p

相关标签:
4条回答
  • 2020-12-09 10:10

    read -s password works on my linux box.

    0 讨论(0)
  • 2020-12-09 10:18

    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
    
    0 讨论(0)
  • 2020-12-09 10:23

    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.

    0 讨论(0)
  • 2020-12-09 10:31
    stty_orig=`stty -g`
    stty -echo
    echo 'hidden section'
    stty $stty_orig
    
    0 讨论(0)
提交回复
热议问题