如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:18:27  来源:igfitidea点击:

How can I get the current user's username in Bash?

bashusername

提问by George

I am writing a program in Bash that needs to get the user's username.

我正在用 Bash 编写一个需要获取用户用户名的程序。

I have heard of a thing called whoami, but I have no idea what it does or how to use it.

我听说过一种叫做 的东西whoami,但我不知道它有什么作用或如何使用它。

What command do I use to get the current username?

我使用什么命令来获取当前用户名?

回答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 $USERvariable is for.

当然,您不需要创建变量,因为这就是$USER变量的用途。

回答by Brett

An alternative to whoamiis id -u -n.

的替代方法whoamiid -u -n

id -uwill 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 lognameto 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 上,您会遇到上述方法的问题:

  • iddoes not accept the -uand -nparameters (so you will have to parse the output)
  • whoamidoes not exist (by default)
  • who am Iprints owner of current terminal (ignores EUID)
  • $USERvariable 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:

两条命令:

  1. idprints the user id along with the groups. Format: uid=usernumber(username) ...

  2. whoamigives the current user name

  1. id打印用户 ID 和组。格式:uid=usernumber(username) ...

  2. whoami给出当前用户名

回答by OldManRiver

When root (sudo) permissions are required, which is usually 90%+ when using scripts, the methods in previous answers always give you rootas the answer.

当需要 root (sudo) 权限时,使用脚本时通常为 90%+,以前答案中的方法总是给你root答案。

To get the current "logged in" user is just as simple, but it requires accessing different variables: $SUDO_UIDand $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

In SolarisOS I used this command:

SolarisOS 中,我使用了以下命令:

$ who am i     # Remember to use it with space.

On Linux- Someone already answered this in comments.

在 Linux 上 - 有人已经在评论中回答了这个问题。

$ whoami       # Without space

回答by Fred Mitchell

For Bash, KornShell(ksh), sh, etc. Many of your questions are quickly answered by either:

对于BashKornShell( 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 Poperator.

:内置(同义词的true)是通过设置的最后一个参数,其被存储在用于代替临时变量$_。然后我们展开它 ( \u) ,就好像它是一个带有P操作符的提示字符串。

This is better than using $USER, as $USERis 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/passwdfile.

知道 UID 后,通过将 UID 与/etc/passwd文件进行匹配来查找用户。

cat /etc/passwd | cut -d":" -f1,3