Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 241
James Ko Avatar asked Sep 13 '25 10:09

James Ko


1 Answers

Found a solution from Batch:

if net session &> /dev/null; then
    echo 'Admin!'
else
    echo 'Not admin!'
fi
like image 159
James Ko Avatar answered Sep 16 '25 08:09

James Ko