Lxc - how to set root password in a script

Hello everybody,
I’m new to this forum and work on debian stretch.
In order to do some tests I do not succeed to set the root passwd in a script.
This does not work :

lxc-atttach -n name – echo root:pwd | chpasswd

These work but I have to enter the passwd

lxc-attach -n name – passwd

lxc-attach -n name
echo root:pwd | chpasswd
exit

I answer to myself
It works if we put the command “echo root:pwd | chpasswd” in a script.

Probably the echo in the attach doesn’t go at “the right place”, I didn’t dig the question.