Skip to content

How to Assign Password to User in a Shell Script

How to Assign Password to User inside a Shell Script using echo command. Create user in shell script and assign password as standard input.

On this page

Command:

echo <password> | passwd –stdin <username>

Example:

Use the following command to change password of user jack in a shell script. For example we are using string password as password.

echo "password" | passwd --stdin jack

Assigning User Input Password:

Use the following commands to input password from user and assign to user jack.

read -p "Enter Password for User jack: " pwd
echo $pwd | passwd --stdin jack

Also we can prompt password twice from user to make confirmation that user has memorize it correctly. Use the following commands to input password twic from user and assign to user jack.

while :
do
    read -p "Enter Password for User jack: " pwd1
    read -p "Confirm Password for User jack: " pwd2
    if [ "$pwd1" == "$pwd2" ]
    then
		break
    else
	    echo "Password and Confirm password doesn't match...."
    fi
done
echo $pwd1 | passwd --stdin jack

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