Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:

  1. 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).
  2. id, more specifically id -Gn shows different groups if run as administrator. In my Windows 7 machine I have Administrators and root groups added to the list. Again, I am not sure if this is portable.
  3. (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, and this seems utterly inelegant to my taste.) Any better or more elegant suggestions? Perhaps cygwin provides a command utility dedicated for this purpose?
  4. 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).
share|improve this question
    
use the "run as Administrator" unless you do this then the process is not elevated to Administrator permissions even if an Administrator account starts the process. Just create a shortcut so this is always done. –  Ramhound Oct 15 '13 at 13:17
    
@Ramhound Of course. But my problem is not how to run as administrator, my problem is how to detect in scriptable way if I am running as administrator. (I just recently had five terminals open, and needed to find the one that I run as administrator so that I could successfully run ping.) –  FooF Oct 15 '13 at 13:24
1  
I recommend trying ConEmu. It has tabs, can run tab as administrator indicated by different icon in tabbar and most importantly uses console behind the scenes so native windows applications work on it correctly unlike on mintty. –  Jan Hudec Oct 15 '13 at 13:51
1  
@FooF - Just because you run a process with an Administrator level user does not mean the process itself has been escalated. Windows by default will only esclate a process if the user approves of such action hence the "run as Administrator" feature in Windows. You can also provider ALL permissions to ANY group you want even the Foo user group if you wanted. –  Ramhound Oct 15 '13 at 14:12
1  
@FooF - Just think it as sudo which is required even if you are running as a root user. –  Ramhound Oct 15 '13 at 14:55

1 Answer 1

up vote 2 down vote accepted

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

# PROMPT: Recreate \$ functionality

# 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

share|improve this answer
    
Assuming there is no equivalent of UNIX id command in Windows, I accept this as a correct answer. At least running at command without any arguments does not appear to have any side effects. I am going to use this method in my .bashrc file from now on! Thanks for the hack. –  FooF Mar 3 at 11:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.