Credentials for mysql fail for mysqldump in bash script
Apologies, I made a really amateurish error. Apparently, a few lines of code were at some point copied and pasted from a windows machine, so had CR/LF as the end-of-line character. 
Changed this to LF and it worked fine.

I would still be interested in why it decided to use root@localhost (and what password it was using), so feel free to comment here with an explanation.