Why passwd isnt working in shell scripts?

I had to write a script to change my login password, and the script wasnt working fine. When I searched through the previous postings in this forum, I got the solution (using 'expect' tool).

But I would like to know why passwd command isnt working in scripts?

passwd opens /dev/tty for reading. And it will read /dev/tty to obtain a password. Before it reads the terminal, it reconfigures it to turn off echo, so that the password cannot be seen as it is typed. If that reconfiguration fails, passwd will give up. And after it has the password, it turns echo back on. Operations like these simply require a terminal.

If that behavior is acceptable, then the passwd command does indeed work in scripts just fine. The "problem" is that people want the script to be able to supply the password rather than the user of the script. In that case, you need expect.