How to get the logged in user's real name in Unix?

Viewed 26828

I'm looking to find out the logged in user's real (full name) to avoid having to prompt them for it in an app I'm building. I see the finger command will output a columned list of data that includes this and was wondering if it makes sense to grep through this or is there an easier way? None of the switches for finger that I've found output just the real name. Any thoughts would be much appreciated.

7 Answers

Specific to macOS, there is no getent command; instead you have to use id -F

For macOS and Linux:

if [ "Darwin" = $(uname) ]; then FULLNAME=$(id -P $USER | awk -F '[:]' '{print $8}') else FULLNAME=$(getent passwd $USER | cut -d: -f5 | cut -d, -f1) fi echo $FULLNAME

Related