Suppressing Sensitive Entered Text/String In Bash

Last Updated on: August 16, 2020

So far in our examples or scripts, we have written in the previous guides, we have no way of controlling what is visible to user, which is fine if we have no sensitive data such as passwords.

As soon as you start prompting for sensitive data, it is recommended you suppress or hide the text, and that can be done using the read command with the -s option. Here is a before and after script example:

Before:

					

#!/bin/bash
read -p "What is Your Name? " name
echo "Your Name is $name"
read -p "Enter Password? " password
read -n1 -p "Press any key to exit "
echo
exit 0

After:

					

#!/bin/bash
read -p "What is Your Name? " name
echo "Your Name is $name"
read -s -p "Enter Password? " password
echo
read -sn1 -p "Press any key to exit "
echo
exit 0

The read -s option will read password from the user, it will nit display as you type and it would then store it to variable password.

On the line that has “Press any key to exit”, we initially didn’t use the s option, which means when user enters any key, it would display it before exiting. We tidy it up by adding an s option, now, when we enter any key to continue, it will not be displayed on the screen.

Here is the output of the newly improved script:

					

user@server:~/bin$ read2.sh
What is Your Name? Mr. Devsrealmer
Your Name is Mr. Devsrealmer
Enter Password?                                    #This won't show anything as you type
Press any key to exit                              #This also won't show anything as you type
pascal@blog:~/bin$

Comment policy: Respectful and beneficial comments are welcome with full open hands. However, all comments are manually moderated and those that doesn't relate with what the passage is saying or offensive comments would be deleted. Thanks for understanding!

Leave a Reply

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