windowsbashcommand-linecygwinmsysgit

How to test for administrator privileges in an msysgit/Cygwin script?


How do you test if you have administrator privileges in a script for msysgit/Cygwin? On my machine, opening Git Bash as admin and running whoami still outputs James, not root.

I guess you could probably do something like

if touch /C/file.txt && rm /C/file.txt; then
    echo 'Admin!'
else
    echo 'Not admin!'
fi

but that feels very hackish. Is there any better way to do this?

edit: Also tried id and id -G, but they have the same output for admin and regular terminals.


Solution

  • Found a solution from Batch:

    if net session &> /dev/null; then
        echo 'Admin!'
    else
        echo 'Not admin!'
    fi