Source File
shellescape.go
Belonging Package
github.com/alessio/shellescape
/*
Package shellescape provides the shellescape.Quote to escape arbitrary
strings for a safe use as command line arguments in the most common
POSIX shells.
The original Python package which this work was inspired by can be found
at https://pypi.python.org/pypi/shellescape.
*/
package shellescape // "import gopkg.in/alessio/shellescape.v1"
/*
The functionality provided by shellescape.Quote could be helpful
in those cases where it is known that the output of a Go program will
be appended to/used in the context of shell programs' command line arguments.
*/
import (
)
var pattern *regexp.Regexp
func init() {
pattern = regexp.MustCompile(`[^\w@%+=:,./-]`)
}
// Quote returns a shell-escaped version of the string s. The returned value
// is a string that can safely be used as one token in a shell command line.
func ( string) string {
if len() == 0 {
return "''"
}
if pattern.MatchString() {
return "'" + strings.ReplaceAll(, "'", "'\"'\"'") + "'"
}
return
}
// QuoteCommand returns a shell-escaped version of the slice of strings.
// The returned value is a string that can safely be used as shell command arguments.
func ( []string) string {
:= make([]string, len())
for , := range {
[] = Quote()
}
return strings.Join(, " ")
}
// StripUnsafe remove non-printable runes, e.g. control characters in
// a string that is meant for consumption by terminals that support
// control characters.
func ( string) string {
return strings.Map(func( rune) rune {
if unicode.IsPrint() {
return
}
return -1
}, )
}
![]() |
The pages are generated with Golds v0.6.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |