Skip to content

How to Input Password in Shell Script as Hidden Characters

How to Input Password in Shell Scripts. How to Input passwords as Hidden Characters in shell scripts. Input hidden characters in Linux command or script

Command:

read -s -p “Enter Password: ” pswd

How to Use:-

If you want to take input of password in shell script. You must want to not to show any character on-screen entered by user. Use -s for silent mode. Using -s input characters are not echoed. For example create a shell script named inputpwd.sh and add 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 your password, You just enter your password, characters will not display on-screen. This will work with /bin/bash shell only, older shell (/bin/sh) will throw an error.

# chmod +x inputpwd.sh
# ./inputpwd.sh

Enter Password:
Your password is:  9kjdfk329sksk

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

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


Back to top