shlex

Types

An error that occurs when lexing a shell input

pub type LexError {
  UnclosedQuotation
  NoEscapedCharacter
}

Constructors

  • UnclosedQuotation

    A quote was opened without a matching closing quote

  • NoEscapedCharacter

    An escape was encountered without a following character to escape

Values

pub fn join(input: List(String)) -> String

Concatenate a list of string inputs into an escaped shell command.

This is a convenience function that applies shlex.quote to each element and joins the result. It is the inverse of shlex.split.

Warning: See shlex.quote for potential vulnerabilities in non-POSIX and interactive shells.

Examples

let unsafe_input = "foo; cat ~/.ssh/id_rsa"
assert shlex.join(["git", "commit", "-m", unsafe_input])
  == "git commit -m 'foo; cat ~/.ssh/id_rsa'"
pub fn quote(input: String) -> String

Escape a string input into a shell word.

Warning: The output is safe for POSIX shell parsing, but may be vulnerable to shell injection in non-POSIX-compliant shells or interactive shells with history expansion, quick substitution, or other parsing extensions.

Examples

let unsafe_input = "foo; cat ~/.ssh/id_rsa"
assert shlex.quote(unsafe_input) == "'foo; cat ~/.ssh/id_rsa'"
pub fn split(input: String) -> Result(List(String), LexError)

Split a shell input into a list of string tokens.

This aims to follow the POSIX standard defined by IEEE Std 1003.1-2024.

Examples

let assert Ok(tokens) = shlex.split("git commit -m 'hello world!'")
assert tokens == ["git", "commit", "-m", "hello world!"]
Search Document