Fixes trailing newlines caused by bash HEREDOC usage

I didn't touch `urlmode()` since I didn't quite understand it.

I used `printf %s` instead of `echo -n` since the `printf` version is verbatim output whereas the `echo` version may do some post-processing of trailing newlines.