Is there a command to check if Git Bash is opened in Administrator mode?
Asked Answered
L

5

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?

Licorice answered 27/4, 2017 at 6:53 Comment(4)
Why would you open Git Bash as an administrator in the first place?Newspaperman
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.Enochenol
@Enochenol you may be rightLicorice
@Enochenol This appears to be true for my Git Bash install as well.Oster
O
16

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.

Orchestrion answered 13/11, 2019 at 22:32 Comment(2)
What's fi doing?Dongola
fi is the end of the if block. See mywiki.wooledge.org/BashGuide/…Orchestrion
G
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.

Gravois answered 20/2, 2020 at 10:11 Comment(0)
M
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
Megalith answered 8/7, 2022 at 16:10 Comment(0)
G
-2

You can try a linux command, like that:

whoami

This will return your system username

or windows command:

ECHO %USERNAME%
Gerhan answered 10/8, 2017 at 14:41 Comment(1)
But it returns the same result whether you are running git bash as Administrator or not...Overdress
A
-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.

Absentee answered 27/4, 2017 at 8:45 Comment(2)
This only returns the username but not if this user currently has Administrator access.Keegan
The git user name can be anything. Has nothing to do with windows user or access permissions.Falconiform

© 2022 - 2024 — McMap. All rights reserved.