如何在 Bash 中获取当前用户的用户名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19306771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the current user's username in Bash?
提问by George
回答by SethMMorton
On the command line, enter
在命令行中,输入
whoami
or
或者
echo "$USER"
To save these values to a variable, do
要将这些值保存到变量中,请执行
myvariable=$(whoami)
or
或者
myvariable=$USER
Of course, you don't need to make a variable since that is what the $USER
variable is for.
当然,您不需要创建变量,因为这就是$USER
变量的用途。
回答by Brett
An alternative to whoami
is id -u -n
.
的替代方法whoami
是id -u -n
。
id -u
will return the user id (e.g. 0 for root).
id -u
将返回用户 ID(例如 0 表示root)。
回答by Devin R
Use the standard Unix/Linux/BSD/MacOS command logname
to retrieve the logged in user. This ignores the environment as well as sudo, as these are unreliable reporters. It will always print the logged in user's name and then exit. This command has been around since about 1981.
使用标准的 Unix/Linux/BSD/MacOS 命令logname
检索登录用户。这忽略了环境和 sudo,因为它们是不可靠的记者。它将始终打印登录的用户名,然后退出。该命令自 1981 年左右就已存在。
My-Mac:~ devin$ logname
devin
My-Mac:~ devin$ sudo logname
Password:
devin
My-Mac:~ devin$ sudo su -
My-Mac:~ root# logname
devin
My-Mac:~ root# echo $USER
root
回答by Elvenfighter
A hack the I've used on Solaris9 and Linux and which works fine for both of them:
我在Solaris9 和 Linux上使用的 hack并且对它们都适用:
ps -o user= -p $$ | awk '{print }'
This snippet prints the name of the user with the current EUID.
此代码段打印具有当前 EUID 的用户名。
NOTE:you need Bash as the interpreter here.
注意:这里需要 Bash 作为解释器。
On Solaris you have problems with methods, described above:
在 Solaris 上,您会遇到上述方法的问题:
id
does not accept the-u
and-n
parameters (so you will have to parse the output)whoami
does not exist (by default)who am I
prints owner of current terminal (ignores EUID)$USER
variable is set correctly only after reading profile files (for example,/etc/profile
)
id
不接受-u
和-n
参数(因此您必须解析输出)whoami
不存在(默认)who am I
打印当前终端的所有者(忽略 EUID)$USER
只有在读取配置文件(例如,/etc/profile
)后才能正确设置变量
回答by Sireesh Yarlagadda
Two commands:
两条命令:
id
prints the user id along with the groups. Format:uid=usernumber(username) ...
whoami
gives the current user name
id
打印用户 ID 和组。格式:uid=usernumber(username) ...
whoami
给出当前用户名
回答by OldManRiver
When root (sudo) permissions are required, which is usually 90%+ when using scripts, the methods in previous answers always give you root
as the answer.
当需要 root (sudo) 权限时,使用脚本时通常为 90%+,以前答案中的方法总是给你root
答案。
To get the current "logged in" user is just as simple, but it requires accessing different variables: $SUDO_UID
and $SUDO_USER
.
获取当前“登录”用户同样简单,但它需要访问不同的变量:$SUDO_UID
和$SUDO_USER
。
They can be echoed:
它们可以被回显:
echo $SUDO_UID
echo $SUDO_USER
Or assigned, for example:
或分配,例如:
myuid=$SUDO_UID
myuname=$SUDO_USER
回答by Lin-man
回答by Fred Mitchell
For Bash, KornShell(ksh
), sh
, etc. Many of your questions are quickly answered by either:
对于Bash、KornShell( ksh
)sh
等,您的许多问题都可以通过以下任一方式快速得到解答:
man [function]
to get the documentation for the system you are using or usually more conveniently:
获取您正在使用的系统的文档,或者通常更方便:
google "man function"
谷歌“人功能”
This may give different results for some things where Linux and Unix have modest differences.
对于 Linux 和 Unix 具有适度差异的某些事情,这可能会给出不同的结果。
For this question, just enter "whoami" in your shell.
对于这个问题,只需在 shell 中输入“whoami”。
To script it:
编写脚本:
myvar=$(whoami)
回答by Crestwave
The current user's username can be gotten in pure Bash with the ${parameter@operator}
parameter expansion (introduced in Bash 4.4):
当前用户的用户名可以在纯 Bash 中通过${parameter@operator}
参数扩展(在 Bash 4.4 中引入)来获取:
$ : \u
$ printf '%s\n' "${_@P}"
The :
built-in (synonym of true
) is used instead of a temporary variable by setting the last argument, which is stored in $_
. We then expand it (\u
) as if it were a prompt string with the P
operator.
的:
内置(同义词的true
)是通过设置的最后一个参数,其被存储在用于代替临时变量$_
。然后我们展开它 ( \u
) ,就好像它是一个带有P
操作符的提示字符串。
This is better than using $USER
, as $USER
is just a regular environmental variable; it can be modified, unset, etc. Even if it isn't intentionally tampered with, a common case where it's still incorrect is when the user is switched without starting a login shell (su
's default).
这比 using 更好$USER
,因为$USER
它只是一个常规的环境变量;它可以被修改、取消设置等。即使它不是故意篡改的,它仍然不正确的一个常见情况是用户在没有启动登录 shell(su
默认值)的情况下被切换。
回答by John
On mostLinux systems, simply typing whoamion the command line provides the user ID.
在大多数Linux 系统上,只需在命令行上输入whoami即可提供用户 ID。
However, on Solaris, you may have to determine the user ID, by determining the UID of the user logged-in through the command below.
但是,在 Solaris 上,您可能必须通过以下命令确定登录用户的 UID 来确定用户 ID。
echo $UID
Once the UID is known, find the user by matching the UID against the /etc/passwd
file.
知道 UID 后,通过将 UID 与/etc/passwd
文件进行匹配来查找用户。
cat /etc/passwd | cut -d":" -f1,3