Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to use a script to find the username and home directory of the user that is currently logged on to a Mac. This is what I have, and it works, but is there a better (i.e. simpler) way to do it?

userHome="eval echo ~`echo $user`"

The script will almost definitely not be run by the logged on user.

This is Mac OS X 10.6, joined to an Active Directory, and a network user is logged on. So the username does not appear in /etc/passwd.

share|improve this question
up vote 2 down vote accepted

This script:

w -h | sort -u -t' ' -k1,1 | while read user etc
  homedir=$(dscl . -read /Users/$user NFSHomeDirectory | cut -d' ' -f2)
  echo =$user= =$homedir=

will do the following:

  • find all users logged in (via ssh too!) w -h or can use the who command too
  • sort and find unique users sort -u -t' ' -k1,1
  • for each logged user
    • read username
    • find his home directory from the DirectoryService daemon via the command dscl
    • print out username and his home directory
share|improve this answer
Good old dscl ... don't know why I didn't think of that! – William Jackson Jun 8 '11 at 20:31

The user is stored in a variable USER, and the home is in HOME:

echo user name: $USER, user home: $HOME

This is done automatically. You don't have to write any script to get them.

share|improve this answer
This will only tell me the username and home of the user that runs the script. I want the user that is logged on to the GUI, the script is running as a different user. – William Jackson Jun 8 '11 at 16:06
I see. I misunderstood the question. – Hai Vu Jun 9 '11 at 15:50

From this Advanced Bash-Scripting Guide I learned that

The $(...) form has superseded backticks for command substitution.

The $(...) form of command substitution permits nesting.

Now I am using:

userHome=$(eval echo ~$(echo $user))
share|improve this answer
That is cleaner than userHome=$(dscl . -read /Users/logname NFSHomeDirectory | awk '{print $2}) ; user=$(basename $userHome) - nicely done – bmike Jun 8 '11 at 20:45

Assuming logname works, then you can get the home directory like this:

userHome=$(awk -F: -v u=$user '$1 == u {print $6}' /etc/passwd)
share|improve this answer
The user does not appear in /etc/passwd/. I updated the question. – William Jackson Jun 8 '11 at 16:38

Not sure if all the other complications are needed.

echo $USER
echo ~
share|improve this answer
From the post "The script will almost definitely not be run by the logged on user." -- your answer won't work to discover other users on the system. – Ian C. Jun 4 '15 at 16:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.