Sometimes, you need to put a non-interactive passwords using scripts, as you can see, you can’t use passwd tool, because it only works using interactive way.
On Debian, and of course lot of distros, you can use chpasswd instead (create, update & modify)
From chpasswd manual:
chpasswd reads a list of user name and password pairs from standard input and uses this information to update a group of existing users, so you can update passwords in batch mode.
echo “user:pass” | chpasswd