I wasted a good few hours trying to find a good solution on how to save an echoed string with sudo. I tried “sudo echo ..” and lots of different combinations before I made it work.
I used the command “tee” which basically copies the output and copies into a variable or file. Perfect!
echo "String to save " | sudo tee filename.txt > /dev/null
If you need to improve your skills try my linux training guide, read more