Home >Operation and Maintenance >Linux Operation and Maintenance >How to enter password as hidden characters in shell script

How to enter password as hidden characters in shell script

不言
不言Original
2019-03-20 17:09:155333browse

This article introduces to you how to enter passwords in the form of hidden characters in shell scripts. Let’s take a look at the specific content.

How to enter password as hidden characters in shell script

Let’s first look at the command

read -s -p “Enter Password: ” pswd

How to use:

If you want to enter the password in the shell script. You probably don't want any characters entered by the user to be displayed on the screen. Use -s for silent mode. Characters entered using -s will not be echoed. For example, create a shell script named inputpwd.sh and add the following content.

$ vim inputpwd.sh
#!/bin/bash
### Input password as hidden charactors ### 
read -s -p "Enter Password: "  pswd
### Print the value of pswd variable ### 
echo -e "\nYour password is: " $pswd

Let's execute the script and enter the password, you just enter the password and the characters will not be displayed on the screen. This only works with the /bin/bash shell, older shells (/bin/sh) will throw an error.

Let's execute the script and enter your password, you just enter your password and no characters will appear on the screen. This only works with /bin/bash shell, older shells (/bin/sh) will throw errors.

# chmod +x inputpwd.sh
# ./inputpwd.sh
Enter Password:
Your password is:  9kjdfk329sksk

This article has ended here. For more other exciting content, you can pay attention to the Linux Tutorial Video column of the PHP Chinese website! ! !

The above is the detailed content of How to enter password as hidden characters in shell script. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn