14

Let's say you opened a Git Bash console and used it for a time but you forgot if you opened it as Administrator or not.

Is there a way to check this in the current console, without closing and opening it again?

Samurai Jack
  • 2,985
  • 8
  • 35
  • 58
  • Why would you open Git Bash as an administrator in the first place? – 1615903 Apr 27 '17 at 09:56
  • 8
    Run `env | grep SESSIONNAME`. If it's run as Administrator, the command returns nothing. If not, it returns `SESSIONNAME=Console`. But this is just what I have observed from tests. I am not sure if this is the difference caused by running as or not as administrator. I have no idea what SESSIONNAME means. – ElpieKay Apr 27 '17 at 14:23
  • @ElpieKay you may be right – Samurai Jack Apr 27 '17 at 14:29

5 Answers5

14

Adapted from the thread on how to check for admin rights, run the following:

if [[ $(sfc 2>&1 | tr -d '\0') =~ SCANNOW ]]; then echo Administrator; else echo $USERNAME; fi

If you are running git-bash as Administrator, it will print Administrator, otherwise it will print your username.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
daveloyall
  • 2,140
  • 21
  • 23
1

My PS1 environmental variable looks like this:

\[\033]0;$TITLEPREFIX:$PWD\007\]\n\[\033[32m\]\u@\h \[\033[35m\]$MSYSTEM \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$

So off the back of the answer from @daveloyall. I've put this into my .bash_profile

if [[ $(sfc 2>&1 | tr -d '\0') =~ SCANNOW ]]; then
    export TITLEPREFIX='ADMIN' 
fi

This then prefixes the window title with ADMIN if you've opened git-bash as administrator.

0

A reliable way to check if git-bash on Windows is running as admin privilege:

#!/bin/bash

net session 1>/dev/null 2>&1 || printf >&2 "error: please run this script as administrator.\n"

If you want to take into consideration that there might be a Windows which has no net command (I doubt that), you can check for it:

#!/bin/bash

if type -fq net >/dev/null
then
    net session 1>/dev/null 2>&1 || printf >&2 "error: please run this script as administrator.\n"
fi
lzhh
  • 852
  • 7
  • 16
-2

You can try a linux command, like that:

whoami

This will return your system username

or windows command:

ECHO %USERNAME%
Zepp
  • 48
  • 4
  • 5
    But it returns the same result whether you are running git bash as Administrator or not... – pgras Jan 30 '18 at 12:05
-4

Yes, You can check the current username by using this command:

git config user.name

This will return the username and you can know if it is the Administrator.

  • 2
    This only returns the username but not if this user currently has Administrator access. – Leon S. Jun 14 '17 at 12:47
  • 1
    The git user name can be anything. Has nothing to do with windows user or access permissions. – Brad Oct 25 '20 at 02:05