Documentation
¶
Overview ¶
Package shellwords is a pure-Go, CGO-free reimplementation of Ruby's `shellwords` standard library (MRI's lib/shellwords.rb).
It manipulates strings according to the word-parsing rules of the UNIX Bourne shell: splitting a command line into words (Split), escaping a single argument so the shell parses it back verbatim (Escape), and joining an argument list into a single escaped command line (Join).
The behaviour is matched byte-for-byte against MRI Ruby 4.x. The mapping to the Ruby API is:
Split <-> Shellwords.shellsplit / Shellwords.split / String#shellsplit Escape <-> Shellwords.shellescape / Shellwords.escape / String#shellescape Join <-> Shellwords.shelljoin / Shellwords.join / Array#shelljoin
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Escape ¶
Escape escapes s so that it can be safely used as a single argument in a Bourne shell command line. It is the exact analogue of Ruby's Shellwords.shellescape / String#shellescape.
An empty string returns "”" (two single quotes). Otherwise every character outside the safe set [A-Za-z0-9_\-.,:+/@\n] is backslash-escaped, and each newline is then rewritten as '\n' (a backslash cannot escape a newline, since backslash+newline is a shell line continuation).
Multibyte characters are treated as whole characters, not bytes: a non-safe rune is prefixed with a single backslash. It is the caller's responsibility to encode the string in the right encoding for the target shell.
func Join ¶
Join builds a command-line string from words, escaping each element with Escape and joining them with a single space. It is the exact analogue of Ruby's Shellwords.shelljoin / Array#shelljoin. An empty slice yields "".
func Split ¶
Split splits line into an array of tokens the same way the UNIX Bourne shell does. It is the exact analogue of Ruby's Shellwords.shellsplit / String#shellsplit.
Quotes ('single' and "double") and backslash escapes are honoured; concatenation within a single word is supported (a"b"c -> abc). A trailing unmatched quote (or any other character that cannot start a token) yields an *ArgumentError with the message "Unmatched quote at N: ...". A NUL character yields "Nul character at N: ...". line must not contain NUL characters because of the nature of the exec(2) system call.
Note that this is not a full command-line parser: shell metacharacters other than the quotes and backslash (such as | or >) are not treated specially.
Types ¶
type ArgumentError ¶
type ArgumentError struct {
// Message is the full Ruby-compatible message, e.g.
// "Unmatched quote at 2: ...'" or "Nul character at 0: \x00".
Message string
}
ArgumentError mirrors Ruby's ArgumentError as raised by Shellwords.shellsplit (and shellescape). Split returns it for an unmatched quote, a dangling backslash that forms an invalid token, or a NUL character; Escape panics with it via a Go error only when used through the strict helpers — see individual functions. Its Error string reproduces MRI's message exactly, e.g. "Unmatched quote at 0: \"".
func (*ArgumentError) Error ¶
func (e *ArgumentError) Error() string
