Search code examples
bashescapingtemplating

Bash equivalent of Python's shlex.quote


Python's standard library has a shlex.quote function that takes a string and returns one that is guaranteed to be interpreted as that same string by Unix shells. This is achieved by putting the string in single quotes and escaping any occurrences of the single quote character that appear in it.

This function is useful e.g. when you're templating out shell scripts and can't guarantee that the substituted values only contain "shell-safe" strings.

My question: Is there an equivalent of this written in pure bash with at most coreutils as a dependency? Or perhaps even a bash builtin mechanism I'm not aware of?


Minimal example to show how you would use such a utility (called shlex_quote here as a placeholder):

generate_greeting_script.sh:

#!/bin/bash
cat > greet.sh <<HEREDOC
#!/bin/bash
greeting=$(shlex_quote "$1")
echo "\$greeting \$(whoami)!"
HEREDOC
chmod +x greet.sh
$ ./generate_greeting_script.sh "'ello"
$ ./greet.sh
'ello govnah!

Which works just fine with a shlex_quote utility using Python's shlex.quote, but having Python as a dependency just for that is overkill.


Solution

  • Or using @Q variable expansion

    cat generate_greeting_script:

    #!/usr/bin/env bash
    
    cat >greet <<HEREDOC
    #!/usr/bin/env bash
    
    printf ${1@Q}' %s!\n' "\$USER"
    HEREDOC
    chmod +x greet
    
    ./generate_greeting_script 'I say hello to'
    

    A .sh extension on executable scripts with a shebang is not required.

    Generated greet:

    #!/usr/bin/env bash
    
    printf 'I say hello to'' %s!\n' "$USER"
    

    Output:

    ./greet
    I say hello to lea!
    

    Reference man bash:

    ${parameter@operator}

    Parameter transformation.

    The expansion is either a transformation of the value of parameter or information about parameter itself, depending on the value of operator. Each operator is a single letter:

    Q

    The expansion is a string that is the value of parameter quoted in a format that can be reused as input.