Have you ever wondered how login pages display dots or asterisks instead of showing your password in plain text? This is done to protect your system or account. In this article, we will explore how to read passwords without echoing them back in Bash scripting.
Reading Passwords in Silent Mode
To read a password without echoing back, follow these steps:
Bạn đang xem: Bash Scripting – Bash Read Password without Echoing back
- Create a file named “User.sh” using the command:
$ touch User.sh
- Open “User.sh” in a text editor:
$ nano User.sh
- Write the following bash script into “User.sh”:
#!/bin/bash
echo "Enter Username: " # read username and echo username in terminal
read username
echo "Enter Password: " # password is read in silent mode i.e. it will show nothing instead of password.
read -s password
echo
echo "Your password is read in silent mode."
Here, the -s
option is used to read passwords in silent mode.
- Save the script and make it executable by running:
$ chmod +x ./User.sh
- Finally, run the script:
$ ./User.sh
Xem thêm : Remove Google Account without Password [100% Work]
The script will read both the username and password, but it will not echo the password.
Reading Passwords with Asterisk (*)
If you want to read the password with an asterisk (*), follow these steps:
- Create a file named “Password.sh” using the command:
$ touch Password.sh
- Open “Password.sh” in a text editor:
$ nano Password.sh
- Write the following bash script into “Password.sh”:
#!/bin/bash
password=""
echo "Enter Username: " # it will read username
read username
pass_var="Enter Password:"
# this will take password letter by letter
while IFS= read -p "$pass_var" -r -s -n 1 letter
do
# if you press enter, the condition is true and it exits the loop
if [[ $letter == $'' ]]; then
break
fi
# the letter will be stored in the password variable
password="$password$letter"
# in place of password, an asterisk (*) will be printed
pass_var="*"
done
echo
echo "Your password is read with an asterisk (*)."
In this script, the -r
option is used to read the password, while -p
echoes the input character by character. -s
reads the password in silent mode, and -n
does not print the trailing newline.
- Save the script and make it executable by running:
$ chmod +x ./Password.sh
- Finally, run the script:
$ ./Password.sh
Xem thêm : How to Hide Your Story from Someone on Snapchat
The script will read both the username and password, displaying the password with an asterisk (*).
Frequently Asked Questions
Q: Why is it important to read passwords without echoing back?
A: Reading passwords without echoing back adds an extra layer of security by preventing others from seeing the entered password.
Q: Can I modify the bash scripts to suit my specific needs?
A: Yes! Feel free to modify the scripts according to your requirements. Bash scripting offers great flexibility.
Q: Are there any risks associated with reading passwords in silent mode?
A: Reading passwords in silent mode reduces the risk of exposing sensitive information, but it’s important to ensure the script is secure and trustworthy.
Conclusion
Reading passwords without echoing back is a useful skill in Bash scripting. By following the steps outlined in this article, you can enhance the security of your scripts and protect sensitive information. Remember to handle passwords securely and responsibly in your applications.
For more information and resources on technology and information security, visit Eireview.
Nguồn: https://eireview.org
Danh mục: Technology