Yup, I know the feeling. I've come close to ripping out my hair at times trying to get the right number of backslashes for certain kinds of strings of text. Many here have shared your grief at one time or another.
As a side note, I thought I should also mention you can use backticks instead of the $( ) format. For example:
It's a little less intrusive (visually speaking), but can cause some syntax grief because it's commonly mistaken as a single quote character.
Anyway, glad it's working for you. Just make sure the wife doesn't know you're "cheating"