Regarding the application of this, I can see that you're trying to remotely execute a script file using an eval command. Having done this myself, I would recommend writing each line to a file instead of appending it to a string, as strings can only have a length of 4095 characters or less, which can be a problem in some cases. It is also worth noting that there is a considerably shorter (127 or 255 maybe?) character limit on server command arguments, but I have yet to experience a problem with this as long as my lines are of reasonable length.
As far as the " issue goes, it is essentially replacing "\\" with "\\\\" and so for quotes and single-quotes.
Edit: I was wrong about the string length limit, as my conjecture was based on results from echoing strings.