I generally run sudo mysql database < database.dump and type the password on prompt.
In order to automate this, I tried to execute echo 'password' | sudo -S mysql database < database.dump but it is not recognizing my password.
I have also tried without success:
mysql_cmd="mysql database < database.dump"
echo 'password' | sudo -S $mysql_cmd
I can sudo -S other commands normally. What might be the problem?