Problem Statement: What is the most elegant and robust way to test if Cygwin mintty
bash
session is "Run as adminstrator"?
Why, specifically? I have typically several mintty
terminals open when using Windows (mintty
does not have tabs). The most awkward is when I need to find a terminal window that I started by right-clicking "Run as administrator" when for example I want to run ping
or other one-time procedure. I would like to indicate the "run as administrator"-ness of the terminal session visually (by changing the bash
shell prompt variable PS1
in my start-up file ~/.bashrc
).
Some quick potential solutions:
- I can compare the value of some environmental variables. By quick look of
env
output there are quite many differences. It is however hard to tell which is most reliable in terms of portability to another Windows machine (perhaps running different version of Windows). id
, more specificallyid -Gn
shows different groups if run as administrator. In my Windows 7 machine I haveAdministrators
androot
groups added to the list. Again, I am not sure if this is portable.- I could try to write a file to a location that would fail as normal user. But I do not want to write any files to strange places – this could in some imagined scenarios be potentially destructive (e.g. failing storage media) and this seems utterly inelegant to my taste.
- Running some Windows program that will indicate by return status or output if the command is run "as administrator". Best would be some with analogous purpose to that of UNIX
id(1)
command (but natively existing in Windows – or Cygwin, but without too far fetched translation of Windows system concepts to POSIX emulated concepts).
Any better or more elegant suggestions? Perhaps cygwin provides a command utility dedicated for this purpose?
Update: 97% duplicate of https://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights/ – the difference is just here using bash
instead of (IMHO weird and archaic) Windows cmd.exe
. Please do check the answers and commentaries there.
Best Answer
I just wrote this function for the same reason. I never know which shell has admin privileges.
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....