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$