func appendShellSafeQuote(buf []byte, s string) []byte {
	buf = append(buf, '"')
	for width := 0; len(s) > 0; s = s[width:] {
		r := rune(s[0])
		width = 1
		if r >= utf8.RuneSelf {
			r, width = utf8.DecodeRuneInString(s)
		}
		if width == 1 {
			switch r {
			case utf8.RuneError:
				buf = append(buf, '\\')
				buf = append(buf, '0'+s[0]>>6)
				buf = append(buf, '0'+((s[0]>>3)&7))
				buf = append(buf, '0'+(s[0]&7))
			case '\\':
				buf = append(buf, `\\`...)
			case '"':
				buf = append(buf, `\"`...)
			case '$':
				buf = append(buf, `\$`...)
			default:
				buf = append(buf, byte(r))
			}
		} else {
			var runeTmp [utf8.UTFMax]byte
			n := utf8.EncodeRune(runeTmp[:], r)
			buf = append(buf, runeTmp[:n]...)
		}
	}
	return append(buf, '"')
}
Append a quoted form of the string s to the given buffer, and return it.

Handles escaping of \, ", and $, as well as standard escape sequences and
octal characters.
package core
import "unicode/utf8"