15

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?

4
  • 1
    Why would you open Git Bash as an administrator in the first place? Commented Apr 27, 2017 at 9:56
  • 9
    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. Commented Apr 27, 2017 at 14:23
  • 1
    @ElpieKay you may be right Commented Apr 27, 2017 at 14:29
  • @ElpieKay This appears to be true for my Git Bash install as well. Commented Dec 7, 2023 at 12:23

5 Answers 5

17

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What's fi doing?
fi is the end of the if block. See mywiki.wooledge.org/BashGuide/…
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.

Comments

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

Comments

-2

You can try a linux command, like that:

whoami

This will return your system username

or windows command:

ECHO %USERNAME%

1 Comment

But it returns the same result whether you are running git bash as Administrator or not...
-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 Comments

This only returns the username but not if this user currently has Administrator access.
The git user name can be anything. Has nothing to do with windows user or access permissions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.