def escape(x):
    """
    Shell escape the given string

    Implementation borrowed from now-deprecated commands.mkarg() in the stdlib
    """
    if '\'' not in x:
        return '\'' + x + '\''
    s = '"'
    for c in x:
        if c in '\\$"`':
            s = s + '\\'
        s = s + c
    s = s + '"'
    return s

    Shell escape the given string

    Implementation borrowed from now-deprecated commands.mkarg() in the stdlib
    
stackoverflow/venv/lib/python3.6/site-packages/twisted/python/_shellcomp.py