DISQUS

Tech-Recipes: Hide password entry in Bourne/bash shell script | Bourne shell scripting | Tech-Recipes

  • Anonymous · 5 years ago
    instead you can use -s option with read to hide the value like password

    read -s secret
  • qmchenry · 5 years ago
    Neat trick! Beware that this is OS dependent. It works on all the Linux flavors I've tried but not a Solaris 8 system. If you are writing a script to run on many platforms, the old school method above may be more generic. But if writing a script for a specific system, this is a much slicker method. Thanks for sharing!
  • sttyecho · 3 years ago
    I also add the protection for the interruptions:

    trap "stty echo ; exit" 1 2 15
    stty -echo
    read password
    stty echo
    trap "" 1 2 15

    If the user press Ctrl+C in the password prompt, the normal stty mode will be restored
  • qmchenry · 3 years ago
    Awesome! I've never used trap before, but I'll be using it from now on. That's a great solution to a very annoying problem. Thanks for sharing! Use of the trap command would make a great recipe..

    Q
  • Anonymous · 3 years ago
    One more thing (OS and shell-dependent):
    use closing redirection in the stty command:
    stty -echo >&- 2>&-

    This helps to avoid obsolete "No terminal" message in the scripts.