Bash scripting password hiding

This is a quick howto to hide user input on bash (e.g. when you want to ask for a password).

When you need to create a bash script where password input is needed, hiding the typed characters would be preferable this quick how to shows you how to do this in a few lines of code.

Create your bash script as required, when you come to the part where you need to hide your user input:

# << your scripts goes here >>
 
# Create a variable which executes the terminal environment with standard values
terminal_original=`stty -g`
 
# Stop the terminal showing user input
stty -echo
 
# Read your password 
read yourpassword
 
# Enable terminal output again
stty $terminal_original
 
# << your scripts goes here >>

Leave a Reply

Your email address will not be published. Required fields are marked *