Skip to main content
Quote command output correctly.
Source Link
bignose
  • 32.7k
  • 16
  • 80
  • 116

You don't specify a programming language, so I'll assume you want to use the shell; here's an answer for Posix shells.

Two steps to this: get the appropriate record, then get the field you want from that record.

First, getting the account record is done by querying the passwd table:

$ user_name=foo $ user_record="$(getent passwd $user_name)" $ echo "$user_record" foo:x:1023:1025:Fred Nurk,,,:/home/foo:/bin/bash 

For hysterical raisins, the full name of the user is recorded in a field called the “GECOS” field; to complicate matters, this field often has its own structure with the full name as just one of several optional sub-fields. So anything that wants to get the full name from the account record needs to parse both these levels.

$ user_record=$user_record="$(getent passwd $user_name)" $ user_gecos_field=$user_gecos_field="$(echo "$user_record" | cut -d ':' -f 5)" $ user_full_name=$user_full_name="$(echo "$user_gecos_field" | cut -d ',' -f 1)" $ echo $user_full_name"$user_full_name" Fred Nurk 

Your programming language probably has a library function to do this in fewer steps. In C, you'd use the ‘getpwnam’ function and then parse the GECOS field.

You don't specify a programming language, so I'll assume you want to use the shell; here's an answer for Posix shells.

Two steps to this: get the appropriate record, then get the field you want from that record.

First, getting the account record is done by querying the passwd table:

$ user_name=foo $ getent passwd $user_name foo:x:1023:1025:Fred Nurk,,,:/home/foo:/bin/bash 

For hysterical raisins, the full name of the user is recorded in a field called the “GECOS” field; to complicate matters, this field often has its own structure with the full name as just one of several optional sub-fields. So anything that wants to get the full name from the account record needs to parse both these levels.

$ user_record=$(getent passwd $user_name) $ user_gecos_field=$(echo "$user_record" | cut -d ':' -f 5) $ user_full_name=$(echo "$user_gecos_field" | cut -d ',' -f 1) $ echo $user_full_name Fred Nurk 

Your programming language probably has a library function to do this in fewer steps. In C, you'd use the ‘getpwnam’ function and then parse the GECOS field.

You don't specify a programming language, so I'll assume you want to use the shell; here's an answer for Posix shells.

Two steps to this: get the appropriate record, then get the field you want from that record.

First, getting the account record is done by querying the passwd table:

$ user_name=foo $ user_record="$(getent passwd $user_name)" $ echo "$user_record" foo:x:1023:1025:Fred Nurk,,,:/home/foo:/bin/bash 

For hysterical raisins, the full name of the user is recorded in a field called the “GECOS” field; to complicate matters, this field often has its own structure with the full name as just one of several optional sub-fields. So anything that wants to get the full name from the account record needs to parse both these levels.

$ user_record="$(getent passwd $user_name)" $ user_gecos_field="$(echo "$user_record" | cut -d ':' -f 5)" $ user_full_name="$(echo "$user_gecos_field" | cut -d ',' -f 1)" $ echo "$user_full_name" Fred Nurk 

Your programming language probably has a library function to do this in fewer steps. In C, you'd use the ‘getpwnam’ function and then parse the GECOS field.

Bourne shell syntax doesn't support some of these statements; specify Posix-compatible shells.
Source Link
bignose
  • 32.7k
  • 16
  • 80
  • 116

You don't specify a programming language, so I'll assume you want to use the shell; here's an answer for BournePosix shells.

Two steps to this: get the appropriate record, then get the field you want from that record.

First, getting the account record is done by querying the passwd table:

$ user_name=foo $ getent passwd $user_name foo:x:1023:1025:Fred Nurk,,,:/home/foo:/bin/bash 

For hysterical raisins, the full name of the user is recorded in a field called the “GECOS” field; to complicate matters, this field often has its own structure with the full name as just one of several optional sub-fields. So anything that wants to get the full name from the account record needs to parse both these levels.

$ user_record=$(getent passwd $user_name) $ user_gecos_field=$(echo "$user_record" | cut -d ':' -f 5) $ user_full_name=$(echo "$user_gecos_field" | cut -d ',' -f 1) $ echo $user_full_name Fred Nurk 

Your programming language probably has a library function to do this in fewer steps. In C, you'd use the ‘getpwnam’ function and then parse the GECOS field.

You don't specify a programming language, so I'll assume you want to use the shell; here's an answer for Bourne shells.

Two steps to this: get the appropriate record, then get the field you want from that record.

First, getting the account record is done by querying the passwd table:

$ user_name=foo $ getent passwd $user_name foo:x:1023:1025:Fred Nurk,,,:/home/foo:/bin/bash 

For hysterical raisins, the full name of the user is recorded in a field called the “GECOS” field; to complicate matters, this field often has its own structure with the full name as just one of several optional sub-fields. So anything that wants to get the full name from the account record needs to parse both these levels.

$ user_record=$(getent passwd $user_name) $ user_gecos_field=$(echo "$user_record" | cut -d ':' -f 5) $ user_full_name=$(echo "$user_gecos_field" | cut -d ',' -f 1) $ echo $user_full_name Fred Nurk 

Your programming language probably has a library function to do this in fewer steps. In C, you'd use the ‘getpwnam’ function and then parse the GECOS field.

You don't specify a programming language, so I'll assume you want to use the shell; here's an answer for Posix shells.

Two steps to this: get the appropriate record, then get the field you want from that record.

First, getting the account record is done by querying the passwd table:

$ user_name=foo $ getent passwd $user_name foo:x:1023:1025:Fred Nurk,,,:/home/foo:/bin/bash 

For hysterical raisins, the full name of the user is recorded in a field called the “GECOS” field; to complicate matters, this field often has its own structure with the full name as just one of several optional sub-fields. So anything that wants to get the full name from the account record needs to parse both these levels.

$ user_record=$(getent passwd $user_name) $ user_gecos_field=$(echo "$user_record" | cut -d ':' -f 5) $ user_full_name=$(echo "$user_gecos_field" | cut -d ',' -f 1) $ echo $user_full_name Fred Nurk 

Your programming language probably has a library function to do this in fewer steps. In C, you'd use the ‘getpwnam’ function and then parse the GECOS field.

Not bash specific.
Source Link
Jens
  • 73.3k
  • 16
  • 134
  • 186

You don't specify a programming language, so I'll assume you want to use the shell; here's an answer for the bash shellBourne shells.

Two steps to this: get the appropriate record, then get the field you want from that record.

First, getting the account record is done by querying the passwd table:

$ user_name=foo $ getent passwd $user_name foo:x:1023:1025:Fred Nurk,,,:/home/foo:/bin/bash 

For hysterical raisins, the full name of the user is recorded in a field called the “GECOS” field; to complicate matters, this field often has its own structure with the full name as just one of several optional sub-fields. So anything that wants to get the full name from the account record needs to parse both these levels.

$ user_record=$(getent passwd $user_name) $ user_gecos_field=$(echo "$user_record" | cut -d ':' -f 5) $ user_full_name=$(echo "$user_gecos_field" | cut -d ',' -f 1) $ echo $user_full_name Fred Nurk 

Your programming language probably has a library function to do this in fewer steps. In C, you'd use the ‘getpwnam’ function and then parse the GECOS field.

You don't specify a programming language, so I'll assume you want to use the shell; here's an answer for the bash shell.

Two steps to this: get the appropriate record, then get the field you want from that record.

First, getting the account record is done by querying the passwd table:

$ user_name=foo $ getent passwd $user_name foo:x:1023:1025:Fred Nurk,,,:/home/foo:/bin/bash 

For hysterical raisins, the full name of the user is recorded in a field called the “GECOS” field; to complicate matters, this field often has its own structure with the full name as just one of several optional sub-fields. So anything that wants to get the full name from the account record needs to parse both these levels.

$ user_record=$(getent passwd $user_name) $ user_gecos_field=$(echo "$user_record" | cut -d ':' -f 5) $ user_full_name=$(echo "$user_gecos_field" | cut -d ',' -f 1) $ echo $user_full_name Fred Nurk 

Your programming language probably has a library function to do this in fewer steps. In C, you'd use the ‘getpwnam’ function and then parse the GECOS field.

You don't specify a programming language, so I'll assume you want to use the shell; here's an answer for Bourne shells.

Two steps to this: get the appropriate record, then get the field you want from that record.

First, getting the account record is done by querying the passwd table:

$ user_name=foo $ getent passwd $user_name foo:x:1023:1025:Fred Nurk,,,:/home/foo:/bin/bash 

For hysterical raisins, the full name of the user is recorded in a field called the “GECOS” field; to complicate matters, this field often has its own structure with the full name as just one of several optional sub-fields. So anything that wants to get the full name from the account record needs to parse both these levels.

$ user_record=$(getent passwd $user_name) $ user_gecos_field=$(echo "$user_record" | cut -d ':' -f 5) $ user_full_name=$(echo "$user_gecos_field" | cut -d ',' -f 1) $ echo $user_full_name Fred Nurk 

Your programming language probably has a library function to do this in fewer steps. In C, you'd use the ‘getpwnam’ function and then parse the GECOS field.

added 70 characters in body
Source Link
bignose
  • 32.7k
  • 16
  • 80
  • 116
Loading
added 158 characters in body
Source Link
bignose
  • 32.7k
  • 16
  • 80
  • 116
Loading
Source Link
bignose
  • 32.7k
  • 16
  • 80
  • 116
Loading