You can authenticate with a separate invocation of sudo
:
if ! echo "$password1" | sudo -Sv -p ''; then
echo "Authentication failed" >&2
else
sudo foo_bar
fi
Note that this approach will fail if sudo's authentication timeout has been disabled (i.e. if timestamp_timeout
is set to 0 in /etc/sudoers).