Back to godoc.org
github.com/alessio/shellescape

package shellescape

v1.2.2
Latest Go to latest
Published: Mar 2, 2020 | License: MIT | Module: github.com/alessio/shellescape

Overview

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.

Index

Examples

func Quote

func Quote(s string) string

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.

Example

Code:

filename := "myfile; rm -rf /"
prog := "/bin/ls"
unescapedCommand := strings.Join([]string{prog, filename}, " ")
escapedCommand := strings.Join([]string{prog, shellescape.Quote(filename)}, " ")

fmt.Println(unescapedCommand)
fmt.Println(escapedCommand)

Output:

/bin/ls myfile; rm -rf /
/bin/ls 'myfile; rm -rf /'
Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
f or F : Jump to identifier