I modified it like this:
echo "alg $alg"
echo "key $key"
echo "comment $comment"
printf "$alg $key $comment\n" >> "$keyfile" ; \
so I could see where printf was breaking. It's breaking on the first one
-- $alg
Unmatched '.
key: Undefined variable.
comment: Undefined variable.