How to fix /bin/bash^M: bad interpreter: No such file or directory error

Ever get the error /bin/bash^M: bad interpreter: No such file or directory ?
This frequently occurs when moving files between Windows and OSX/Linux environments and the files haven’t been saved with the correct encoding.

There are a few ways to fix it, including using dos2unix or saving the offending file with the correct encoding, but here’s my favourite:

https://gist.github.com/mikeybeck/4c2bb518b088ca319a246b80beed3703

Simply run the above bash script on the broken file and the error should be fixed, just like that!