msys2 sudo code example

Example: sudo msys2

escape()
{
    RESULT="$1"
    RESULT="${RESULT/\'/\\\'\'}" # replace ' with \''
    RESULT="${RESULT/\"/\\\\\\\"}" # replace " with \\\"
    echo "''$RESULT''" # PowerShell uses '' to escape '
}

sudo()
{
    ESCAPED=()
    for ARG in "$@"
    do
        ESCAPED+=($(escape "$ARG"))
    done

    SHELL_PATH=$(cygpath -w $SHELL)
    PS_COMMAND="[Console]::In.ReadToEnd() | Start-Process '$SHELL_PATH' '-c -- \"${ESCAPED[*]}\"' -Verb RunAs"
    cat /dev/stdin | powershell -NoProfile -ExecutionPolicy Bypass "$PS_COMMAND"
}