How to check if cygwin mintty/bash is run as administrator?

The definitive answer to this question comes from the Cygwin mailing list. A process is running with Administrator rights if the user who started it is part of group 544 (Administrators). Also from the comment below by Cromax, it seems that group 114 (Local account and member of Administrator group) is also sometimes present. The test for these two groups is

id -G | grep -qE '\<(114|544)\>'

For example,

id -G | grep -qE '\<(114|544)\>' && echo admin || echo user

In the past you also needed to check for group 0, the root group in /etc/group. But /etc/group is no longer installed in Cygwin, and should usually be removed if it's present, so it's no longer recommended to check for group 0 too. Just group 544.


I just wrote this function for the same reason. I never know which shell has admin privileges.

function isadmin()
{
    net session > /dev/null 2>&1
    if [ $? -eq 0 ]; then echo "admin"
    else echo "user"; fi
}

It adapted from this answer https://stackoverflow.com/a/11995662/307968 for windows cmd shell. Net Session returns 0 status if you are admin.

Now I just need to change my prompt, or maybe the titlebar color....


I use the return value of the Windows program at. I also recreated the functionality of the PROMPTING special character \$.

# Set a white $ initially
eStyle='\[\e[0m\]$'

# If 'at' succeeds, use a red # instead
at &> /dev/null && eStyle='\[\e[0;31m\]#\[\e[0m\]'  # Use # in red

PS1='\n\[\e[0;32m\]\u@\h \[\e[0;33m\]\w\[\e[0m\]\n'"$eStyle "

Examples