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

前端 未结 12 1543
耶瑟儿~
耶瑟儿~ 2020-12-13 05:27

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

相关标签:
12条回答
  • 2020-12-13 06:05

    as 'whoami' has been obsoleted, it's probably more forward compatible to use:

    id -un
    
    0 讨论(0)
  • 2020-12-13 06:05

    There are two ways-

    whoami
    

    or

    echo $USER
    
    0 讨论(0)
  • 2020-12-13 06:09

    whoami

    EDIT

    The whoami utility has been obsoleted by the id(1) utility, and is equivalent to id -un. The command id -p is suggested for normal interactive use.

    0 讨论(0)
  • 2020-12-13 06:12

    The question has not been completely answered, IMHO. I will try to explain: I have a crontab entry that schedules a bash shell command procedure, that in turn does some cleanup of my files; and, when done, sends a notification to me using the OS X notification center (with the command osascript -e 'display notification ...). If someone (e.g. my wife or my daughter) switches the current user of the computer to her, leaving me in the background, the cron script fails when sending the notification.

    So, Who is the current user means Has some other people become the effective user leaving me in the background? Do stat -f "%Su" /dev/console returns the current active user name?

    The answer is yes; so, now my crontab shell script has been modified in the following way:

    ...
    if [ "$(/usr/bin/stat -f ""%Su"" /dev/console)" = "loreti" ]
    then /usr/bin/osascript -e \
      'display notification "Cleanup done" sound name "sosumi" with title "myCleanup"'
    fi
    
    0 讨论(0)
  • 2020-12-13 06:14

    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...

    0 讨论(0)
  • 2020-12-13 06:14

    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.

    0 讨论(0)
提交回复
热议问题