Serghey Rodin
Serghey Rodin

Reputation: 49

How to check password in bash

Is it possible to check that user password is correct in bash script? If so please show me how. Thank you

Upvotes: 2

Views: 2601

Answers (1)

bdombro
bdombro

Reputation: 1411

This works for me:

#ensure sudo isn't saving a recent password
sudo -k

#prime it
echo $ROOTPASSWORD | sudo -S echo hello &> /dev/null

#test it
if ! [ "$(sudo -n echo hello 2>&1)" == "hello" ]; then
    echo "Incorrect password was entered"
fi

Upvotes: 2

Related Questions