alessio/shellescape

Escape arbitrary strings for use as command line arguments

Go

200

103 commits

updated Sep 29, 2026

See the code

README

shellescape

Build GoDoc sourcegraph codecov

Escape arbitrary strings for safe use as command line arguments.

Contents of the package

This package provides the shellescape.Quote() function that returns a shell-escaped copy of a string. This functionality 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.

This work was inspired by the Python original package shellescape.

Usage

The following snippet shows a typical unsafe idiom:

package main

import (
	"fmt"
	"os"
)

func main() {
	fmt.Printf("ls -l %s\n", os.Args[1])
}

See in Go Playground

Especially when creating pipeline of commands which might end up being executed by a shell interpreter, it is particularly unsafe to not escape arguments.

shellescape.Quote() comes in handy and to safely escape strings:

package main

import (
        "fmt"
        "os"

        "al.essio.dev/pkg/shellescape"
)

func main() {
        fmt.Printf("ls -l %s\n", shellescape.Quote(os.Args[1]))
}

See in Go Playground

The escargs utility

escargs reads lines from the standard input and prints shell-escaped versions. Unlike xargs, blank lines on the standard input are not discarded.

command-line
golang
shell
shell-escape
shellescape
shell-script
shell-scripting
string-escape

Significant stargazers

Vladimir Krivosheev

405 followers · starred Jun 2019

Doron Behar

79 followers · starred Apr 2020

Lonny Wong

61 followers · starred Apr 2026

Artur Troian

127 followers · starred Nov 2022

alessio/shellescape

Escape arbitrary strings for use as command line arguments

Go

200

103 commits

updated Sep 29, 2026

See the code

README

shellescape

Build GoDoc sourcegraph codecov

Escape arbitrary strings for safe use as command line arguments.

Contents of the package

This package provides the shellescape.Quote() function that returns a shell-escaped copy of a string. This functionality 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.

This work was inspired by the Python original package shellescape.

Usage

The following snippet shows a typical unsafe idiom:

package main

import (
	"fmt"
	"os"
)

func main() {
	fmt.Printf("ls -l %s\n", os.Args[1])
}

See in Go Playground

Especially when creating pipeline of commands which might end up being executed by a shell interpreter, it is particularly unsafe to not escape arguments.

shellescape.Quote() comes in handy and to safely escape strings:

package main

import (
        "fmt"
        "os"

        "al.essio.dev/pkg/shellescape"
)

func main() {
        fmt.Printf("ls -l %s\n", shellescape.Quote(os.Args[1]))
}

See in Go Playground

The escargs utility

escargs reads lines from the standard input and prints shell-escaped versions. Unlike xargs, blank lines on the standard input are not discarded.

command-line
golang
shell
shell-escape
shellescape
shell-script
shell-scripting
string-escape

Significant stargazers

Vladimir Krivosheev

405 followers · starred Jun 2019

Doron Behar

79 followers · starred Apr 2020

Lonny Wong

61 followers · starred Apr 2026

Artur Troian

127 followers · starred Nov 2022