bash 如何将当前用户保存到变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20221707/
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-18 08:46:33  来源:igfitidea点击:

How to save the current user to a variable

bash

提问by Lukasz Medza

I want 'me' to equal $whoami , but when I try to do it I just print "whoami" as a string instead of the actual value. Would it be possible to use a pipe to connect the variable and the command?

我希望 'me' 等于 $whoami ,但是当我尝试这样做时,我只是将“whoami”打印为字符串而不是实际值。是否可以使用管道连接变量和命令?

回答by anubhava

Use it like this:

像这样使用它:

me="$(whoami)"

to store command whoamioutput to shell variable me

将命令whoami输出存储到 shell 变量me

回答by thom

what about:

关于什么:

me="$USER"

but why should you do this ? It is already an environment variable :-)

但你为什么要这样做?它已经是一个环境变量:-)

echo "$USER"

回答by Matt

You have two simple solution, using whoami and USER :

您有两个简单的解决方案,使用 whoami 和 USER :

WHOAMI

我是谁

me="$(whoami)"

result : me=myusername

结果:我=我的用户名

USER

用户

me="${USER}"

result : me=myusername

结果:我=我的用户名

but if you are in domain : me=dnsdomain\myusername

但如果您在域中:me=dnsdomain\myusername

回答by helpermethod

How about:

怎么样:

$ me=$(whoami)
$ echo "$me"

回答by Gabriel

are you looking for this?

你在找这个吗?

whoami=$(whoami)
echo $whoami # prints the value of whoami