Active6 months ago
How do I get the name of the active user via the command line in OS X?
Having a disk jammed in your Mac is really frustrating, but you can usually get it out by using a few different tricks. The methods outlined below are for when a disc is truly stuck in the Mac. Eject a stuck CD/DVD from a MacBook Pro. Apr 08, 2010 Having a disk jammed in your Mac is really frustrating, but you can usually get it out by using a few different tricks. The methods outlined below are for when a disc is truly stuck in the Mac. Eject a stuck CD/DVD from a MacBook Pro. (Oddly, I don't see this in the man page for the man command, which you can view by typing man man- once you get back to your shell prompt.) Most pagers can be exited by typing q - which is going to be a useful thing to know for other programs that invoke your pager, or when you use your pager directly to view a file ( less some-file.txt ).
Lawrence JohnstonLawrence Johnston27.3k3737 gold badges110110 silver badges175175 bronze badges
11 Answers
as 'whoami' has been obsoleted, it's probably more forward compatible to use:
kentkent4,10533 gold badges2222 silver badges3131 bronze badges
If you'd like to display the full name (instead of the username), add the
AndrewAndrew-F
flag:105k165165 gold badges467467 silver badges648648 bronze badges
EDIT
The whoami utility has been obsoleted by the id(1) utility, and is equivalent to
dfadfaid -un
. The command id -p
is suggested for normal interactive use.96.4k2828 gold badges177177 silver badges219219 bronze badges
Via here
Checking the owner of /dev/console seems to work well.
stat -f '%Su' /dev/console
27.3k3737 gold badges110110 silver badges175175 bronze badges
Mac Terminal Get Out Of Manual Download
I'm pretty sure the terminal in OS X is just like unix, so the command would be:
I don't have a mac on me at the moment so someone correct me if I'm wrong.
dfaMac Terminal Get Out Of Manual 2016
96.4k2828 gold badges177177 silver badges219219 bronze badges
Eric KoslowEric Koslow1,00422 gold badges1515 silver badges3232 bronze badges
If you want to know who's currently logged in to the system:
Command–Space bar: Show or hide the search field. Option-Command-Esc: an app. Command-T: Open a new tab.
To close all windows of the app, press Option-Command-W. Command-W: Close the front window.
(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.
ephemientephemient160k3131 gold badges236236 silver badges362362 bronze badges
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:
Maurizio LoretiMaurizio Loreti
getting username in MAC terminal is easy..
I generally use
whoami
in terminal..RG6 and RG3 Portable Refrigerant Recovery MachinePerformance and Speed for Today’s Busy TechniciansThe RG6 is engineered to include what technicians need in a recovery machine – a rugged and tough machine with fast recovery rates. Best portable ac.
For example, in this case, I needed that to install Tomcat Server..
AlirezaAlireza60.4k1414 gold badges197197 silver badges131131 bronze badges
You can also use the
logname
command from the BSD General Commands Manual under Linux or MacOS to see the username of the user currently logged in, even if the user is performing a sudo
operation. This is useful, for instance, when modifying a user's crontab while installing a system-wide package with sudo: crontab -u $(logname)
Per
Adam LinkAdam Linkman logname
:2,40544 gold badges2121 silver badges4343 bronze badges
Define 'active user'. Mac pro user manual 2014.
If the question is 'who is the logged in user', then 'who am i' or 'whoami' is fine (though they give different answers - 'whoami' reports just a user name; 'who am i' reports on terminal and login time too).
If the question is 'which user ID is the effective ID for the shell', then it is often better to use 'id'. This reports on the real and effective user ID and group ID, and on the supplementary group IDs too. This might matter if the shell is running SUID or SGID.
Jonathan LefflerJonathan Leffler591k9797 gold badges709709 silver badges10651065 bronze badges
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
.2,55044 gold badges3232 silver badges3737 bronze badges