How do I get the name of the active user via the c

2020-02-07 18:53发布

How do I get the name of the active user via the command line in OS X?

12条回答
太酷不给撩
2楼-- · 2020-02-07 19:07

There are two ways-

whoami

or

echo $USER
查看更多
We Are One
3楼-- · 2020-02-07 19:10

getting username in MAC terminal is easy...

I generally use whoami in terminal...

For example, in this case, I needed that to install Tomcat Server...

enter image description here

查看更多
beautiful°
4楼-- · 2020-02-07 19:15

If you want to know who's currently logged in to the system:

$ w
 15:56:14 up 5 days, 20:58,  6 users,  load average: 0.43, 0.53, 0.50
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
me       pts/2     Fri19    1:03m  0.98s  0.98s -/bin/bash
me       pts/3     09:55    6:00m  0.43s  0.43s /bin/bash
me       pts/5     15:56    0.00s  0.23s  0.00s w

(This is from a Linux system; the formatting on OS X may be slightly different, but the information should be about the same.)

There may be multiple login sessions; UNIX is designed to be a multi-user system, after all.

查看更多
放荡不羁爱自由
5楼-- · 2020-02-07 19:20

If you'd like to display the full name (instead of the username), add the -F flag:

$ id -F
Andrew Havens
查看更多
再贱就再见
6楼-- · 2020-02-07 19:23

You can also retrieve it from the environment variables, but that is probably not secure, so I would go with Andrew's answer.

printenv USER

If you need to retrieve it from an app, like Node, it's easier to get it from the environment variables, such as

process.env.USER.

查看更多
贪生不怕死
7楼-- · 2020-02-07 19:24

I'm pretty sure the terminal in OS X is just like unix, so the command would be:

whoami

I don't have a mac on me at the moment so someone correct me if I'm wrong.

查看更多
登录 后发表回答