如何在Bash中获得当前用户的用户名?我用whoami吗?
当前回答
获取当前任务的user_struct
#define get_current_user() \
({ \
struct user_struct *__u; \
const struct cred *__cred; \
__cred = current_cred(); \
__u = get_uid(__cred->user); \
__u; \
})
其他回答
在命令行中输入
whoami
or
echo "$USER"
在Solaris操作系统中,我使用了这个命令:
$ who am i # Remember to use it with space.
在Linux上——有人已经在评论中回答了这个问题。
$ whoami # Without space
All,
从我在这里看到的所有答案都是错误的,特别是如果你进入了sudo模式,所有返回'root'而不是登录用户。答案是使用'who',找到eh 'tty1'用户并提取它。“w”命令的工作原理相同,var=$SUDO_USER获得真正的登录用户。
干杯!
TBNK
REALUSER="${SUDO_USER:-${USER}}"
…获取常规用户(如果不是sudo)→或←当前sudo调用背后的常规用户。
两个命令:
Id打印用户Id和组。 格式:uid=usernumber(用户名)… Whoami给出当前用户名