5

Wondering if you know if there is a slick way to get full username from shell?

example: if my unix username is froyo then I want to get my full name as registered in the system in this case [froyo  === Abhishek Pratap]

finger command does it but for all the logged in users at the same time ..so that needs some parsing to get the right value. Anything slicker ?

Anything from inside python would be great too.

Thanks! -Abhi

Abhi
  • 6,075
  • 10
  • 41
  • 55

1 Answers1

14

One way to achieve this is using pwd.getpwuid and os.getuid:

>>> import os
>>> import pwd
>>> pwd.getpwuid(os.getuid())[4]

This is the traditional Unix way of doing it (based on standard C library calls).

isedev
  • 18,848
  • 3
  • 60
  • 59
  • awesome this works ...I knew python would have something straight forward. – Abhi Feb 22 '13 at 18:57
  • 1
    This is not Python as such. It is the standard Unix C library which provides the `getpwuid` and `getuid` functions. These are simply exposed in the Python `os` and `pwd` modules. – isedev Feb 22 '13 at 19:00
  • ur right ..for me I wanted to get the answer in python env for downstream usage so this works perfect even though we may say it has no python flavor to it :) – Abhi Feb 22 '13 at 19:09
  • The GECOS field can contain comma-separated list of values on some systems, so you may want to add an extra `.split()` for that. – Michał Górny Aug 06 '18 at 09:30
  • 1
    Ubuntu and Debian will return the Full USERNAME, with 4 commas, sometimes intertwined with data. To subvert this, use: pwd.getpwuid(os.getuid())[4].split(',')[0] – PyTis Jan 28 '19 at 16:39