This function ensures that strings you use as arguments to the system() function (or "cmd"|getline commands) are safely quoted. First, all single quotes are converted to '\'', and finally, the argument is wrapped in single quotes.
function shellquote(str) {
gsub(/'/, "'\\''", str)
return sprintf("'%s'", str)
}