query
On this page

escapeShellArg

lib.strings.escapeShellArg

Docs pulled from | This Revision | 19 minutes ago


Nixpkgs manual

Quote string to be used safely within the Bourne shell if it has any special characters. Prior to escaping, if string is a path literal, copy it into the store; otherwise, coerce string to a string via toString if it is not one.

Inputs

string
1. Function argument

Type

escapeShellArg :: a -> String

Examples

lib.strings.escapeShellArg usage example

escapeShellArg "esc'ape\nme"
=> "'esc'\\''ape\nme'"

Noogle detected

Aliases

Implementation

The following is the current implementation of this function.

escapeShellArg =
    arg:
    let
      string =
        # Even a store path can need escaping; as an extreme example, though
        # it is deeply unwise, a store path prefix can contain any non-null
        # character.
        # If a path has a store path prefix but is not itself a store path, we
        # still want to copy it into a fresh store object, so as not to take a
        # dependency on the entire original source.
        if isPath arg && !isStorePath arg then "${arg}" else toString arg;
    in
    if match "[[:alnum:],._+:@%/-]+" string == null then
      "'${replaceString "'" "'\\''" string}'"
    else
      string;