Documentation
¶
Overview ¶
Package shellescape provides functions to escape strings for use in posix shell commands.
It is a drop-in replacement for gopkg.in/alessio/shellescape.v1.
Additionally an Unquote function is provided.
Index ¶
- Variables
- func Expand(input string, opts ...ExpandOption) (string, error)
- func Join(args ...string) string
- func Quote(str string) string
- func QuoteCommand(args []string) string
- func Split(input string) ([]string, error)
- func StripUnsafe(s string) string
- func Unquote(input string) (string, error)
- type ExpandOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMismatchedQuotes is returned when the input string has mismatched quotes when unquoting. ErrMismatchedQuotes = errors.New("mismatched quotes") // ErrTrailingBackslash is returned when the input string ends with a trailing backslash. ErrTrailingBackslash = errors.New("trailing backslash") )
Functions ¶
func Expand ¶
func Expand(input string, opts ...ExpandOption) (string, error)
Expand expands the input string according to the rules of a posix shell. It supports parameter expansion, command substitution and simple $envvar expansion. It does not support arithmetic expansion, tilde expansion, or any of the other expansions and it doesn't support backticks.
func Quote ¶
Quote safely encloses a string in single quotes for shell usage.
Example ¶
This example demonstrates how to use shellescape.Quote to escape a string for use as an argument to a shell command.
package main import ( "fmt" "github.com/k0sproject/rig/v2/sh/shellescape" ) func main() { quoted := shellescape.Quote("value with spaces") fmt.Println(quoted) }
Output: 'value with spaces'
func QuoteCommand ¶
QuoteCommand safely quotes and joins a list of strings for use as a shell command.
Example ¶
This example demonstrates how to use shellescape.QuoteCommand to escape a command and its arguments for use in a shell command.
package main import ( "fmt" "github.com/k0sproject/rig/v2/sh/shellescape" ) func main() { quoted := shellescape.QuoteCommand([]string{"ls", "-l", "file with space"}) fmt.Println(quoted) }
Output: ls -l 'file with space'
func StripUnsafe ¶
StripUnsafe removes non-printable runes from a string.
Types ¶
type ExpandOption ¶
type ExpandOption func(*expandOptions)
ExpandOption is a functional option for Expand.
func ExpandErrorIfUnset ¶
func ExpandErrorIfUnset() ExpandOption
ExpandErrorIfUnset causes Expand to return an error if a variable is not set. By default, unset variables are replaced with an empty string. This only applies when ExpandParam is not set.
func ExpandExec ¶
func ExpandExec() ExpandOption
ExpandExec enables command substitution, as in $(command).
func ExpandNoDollarVars ¶
func ExpandNoDollarVars() ExpandOption
ExpandNoDollarVars disables $var expansion.
func ExpandParam ¶
func ExpandParam() ExpandOption
ExpandParam enables parameter expansion, as in ${parameter:...} and some other patterns. If this is not set, only simple ${VAR} expansion is performed.