get the OS X username using bash

1

How can I get the OS X username using bash. The one that is displayed in the Finder UI for example.

Finder UI

For me it's Clément but when I run whoami or echo $USER I got clement which is the username that is used in a lower level, by the file system for example (/Users/clement).

Cl00e9ment

Posted 2019-11-16T20:41:35.480

Reputation: 13

Answers

1

I would try id -F which gives the "Full name" of the current user

See the man pages for the command line: man id (or take a look at https://ss64.com/osx/id.html)

pd95

Posted 2019-11-16T20:41:35.480

Reputation: 26

Thanks. I accepted your answer over Gordon Davisson's one because it is the simplest. – Cl00e9ment – 2019-11-18T20:57:36.760

Neat; it looks like this option was added in Yosemite (version 10.10) (after I'd last looked at that man page). I'll leave my answer, since it still works & is needed on older versions. – Gordon Davisson – 2019-11-18T22:06:45.567

1

You can get your "Real Name" using the dscl command:

$ dscl /Search -read "/Users/$USER" RealName
RealName:
 Gordon Davisson

If you want just the name (e.g. for use in a script), you can clean up the output with sed:

$ dscl /Search -read "/Users/$USER" RealName | sed '1d; s/^ //'
Gordon Davisson

BTW, "username" is generally used to mean the one that $USER gets (also called the "short name" or "account name"); the one you want is the "Real Name" or "Full Name" (or sometimes "gecos", for historical reasons).

Gordon Davisson

Posted 2019-11-16T20:41:35.480

Reputation: 28 538

Thank, it works. Thanks also for clarification between username and real name. – Cl00e9ment – 2019-11-18T20:59:16.163