ShellQuote is a Go library that quotes strings so they can be used by a POSIX shell.
ShellQuote can be installed with go get
.
go get github.com/depp/shellquote
import "github.com/depp/shellquote"
To quote a string, call String()
. This adds quotes only as necessary. For example,
// Prints x without quotes, because no quotes are needed.
fmt.Println(shellquote.String("x"))
// Prints '$x' with quotes.
fmt.Println(shellquote.String("$x"))
Another useful function is LocalPath()
, which modifies a path so it is not misinterpreted as a flag.
// Prints abc, because abc does not look like a flag.
fmt.Println(shellquote.LocalPath("abc"))
// Prints ./-h, because -t looks like a flag.
fmt.Println(shellquote.LocalPath("-h"))
Non-ASCII characters are not supported.
This is licensed under the MIT license. See LICENSE.txt.