I want to grep the users (first column) from the /etc/passwd file and assign them a password. I only want to do this for user accounts and not default system accounts. So far I have a simple bash script

touch /usertempfile
grep "/home" /etc/passwd |grep -v "/bin/false"|grep -v "/sbin/nologin"|grep -v $USER |awk -F":" '{print $1}' >> /userstempfile
for i in $(cat /usertempfile); do
    echo -e "password/npassword" |passwd $i
rm -rf /usertempfile

The script is able to extract the users from the /etc/passwd file that are not a default system account but is unable to change the passwords of the users which are in that file. What am I doing incorrectly?

  • 1
    Hm, is that really echo -e "password/npassword" in your script and not \n? Dec 24, 2016 at 7:08

1 Answer 1


I am using chpasswd in my script and it's working fine for me:

echo "$USERNAME:$NEWPASSWORD" | chpasswd

PS: for not root user use sudo before chpasswd

echo "$USERNAME:$NEWPASSWORD" | sudo chpasswd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.