Skip to content

Instantly share code, notes, and snippets.

@xupeng
Last active December 23, 2015 18:59
Show Gist options
  • Save xupeng/6679702 to your computer and use it in GitHub Desktop.
Save xupeng/6679702 to your computer and use it in GitHub Desktop.
A relatively reliable way to get login user on Linux
#!/bin/bash
# A relatively reliable way to get login user on Linux
user_entry=`getent passwd $(cat /proc/self/loginuid)`
if [ $? -eq 0 ]; then
login_user=`echo ${user_entry} | cut -d: -f1`
else
login_user=${SUDO_USER:-${LOGNAME}}
if [ "${login_user}" = "" ]; then
login_user=`id -urn`
fi
fi
echo ${login_user}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment