How to obtain the Full Name of the currently logged in user via Terminal when run as root

bashterminal

This command works great to list the Full Name out:

finger $(whoami) | egrep -o 'Name: [a-zA-Z0-9 ]{1,}' | cut -d ':' -f 2 | xargs echo

But, if you run it after a

sudo su

It returns CVMS Root System Administrator

I'm wondering if there's perhaps another place to read the Full Name variable? Is there a User object somewhere that would list this out? I thought I was on to something with

dscacheutil -q user | grep -A 3 -B 2 -e uid:\ 5'[0-9][0-9]'

But it doesn't look like Full Name is represented in that return data.

Best Answer

You can use dscl to read the RealName:

$ dscl . -read /Users/grgarside RealName
RealName:
 George Garside

The following will give you just what you're after. This uses ‘who am i’ to get the username, then sed to format the output.

$ dscl . -read "/Users/$(who am i | awk '{print $1}')" RealName | sed -n 's/^ //g;2p'
George Garside