Last Updated: February 25, 2016
·
1.755K
· croaky

Get your name from the Unix passwd(5) database

I learned this tip from Mike Burns.

Drop into an interactive Ruby shell:

$ irb

Require the Etc module:

> require 'etc'

From the docs:

The Etc module provides a more reliable way to access information about the logged in user than environment variables such as +$USER+.

Get your name from the GECOS field in the passwd(5) database:

> Etc.getpwuid.gecos.split(',').first
=> "Dan Croak"