How do I get a user's friendly username on UNIX?

bash, unix

Solution

Parse the GECOS Field for User's Full Name

The format of /etc/passwd and most of the GECOS field is extremely well standardized across Unix-like systems. If you find an exception, by all means let us know. Meanwhile, the easiest way to get what you want on a Unix-like system is to use getent and cut to parse the GECOS field. For example:

getent passwd $LOGNAME | cut -d: -f5 | cut -d, -f1

Problem

I want to get the "friendly" name, not the username, at least if such a string exists for the given user. Things I've tried: ``` whoami jamesarosen id -un jamesarosen id -p uid jamesarosen groups staff com.apple.access_screensharing ... id -P jamesarosen:********:501:20::0:0:James A. Rosen:/Users/jamesarosen:/bin/bash ``` That last one has the information I'm looking for, but I'd prefer not to have to parse it out, particularly since I'm not terribly confident that the format (specifically the number of `:`s) will remain consistent across OSes.

Original source