Skip to content

Instantly share code, notes, and snippets.

@micahculpepper
Created October 27, 2021 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micahculpepper/3a9533b4967647765842030139209db3 to your computer and use it in GitHub Desktop.
Save micahculpepper/3a9533b4967647765842030139209db3 to your computer and use it in GitHub Desktop.
Find the logged-in user on MacOS
#!/usr/bin/bash
RUID=$(id -ru)
RGID=$(id -rg)
# Find logged in user via the Python -> Objective C bridge
# Docs: https://pythonhosted.org/pyobjc/apinotes/SystemConfiguration.html
output=$(cat << EOF | /usr/bin/python -
import sys
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
res = SCDynamicStoreCopyConsoleUser(None, None, None)
if not res:
sys.exit(1)
username, uid, gid = res
if (not username) or (username == "loginwindow"):
sys.exit(1)
print("RUID={}\nRGID={}".format(uid, gid))
EOF
2>/dev/null)
if [ $? = 0 ]; then
eval "$output"
fi
@micahculpepper
Copy link
Author

even if you run this script via sudo, you'll still get the RUID and RGID of yourself, not just 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment