It seems that Igor has already provided an answer that might work for single quotes.
As for creating the file, remember that sudo affects the program ( sed ) that you run as its option. This does not affect the redirection that your shell handles. Therefore, if you do not have permission to write TestFileNew user, sudo will not help you, as you use it above.
You might be better off creating an exit elsewhere, and then use sudo to move it to a place.
sudo sed "/end/ ..." TestFile > /tmp/TestFileNew sudo mv /tmp/TestFileNew ./TestFileNew
Alternatively, this entire script can be run using sudo ... Ie /path/to/myscript :
#!/bin/bash sed "/end/ ..." TestFile > TestFileNew
then
$ sudo /path/to/myscript
Then sudo starts bash instead of sed, and the privileged instance of bash is responsible for handling the redirection in the script.
source share