1

The $USER reports the current username. But what group(s) do they belong to and specifically which is their primary group : ie. if the user creates a new file the group ID that would be applied as part of the file ownership attributes. Is there an environment variable or if not a shell command to obtain that identifier?

WestCoastProjects
  • 343
  • 2
  • 6
  • 17
  • I don't think there is a conventional environment variable for that - however you can use the `id` command ex. `id -gn` – steeldriver Jan 07 '20 at 22:49

1 Answers1

3

As far as I know, there's no conventionally recognised equivalent to $USER for a user's primary group - however you can use the id command. From man id:

   -g, --group
          print only the effective group ID

   -G, --groups
          print all group IDs

   -n, --name
          print a name instead of a number, for -ugG

So to get the primary group name, use id -gn. You can wrap it in a command substitution $(id -gn) if you need something that you can use like a variable.


Note that some shells set a GID variable for the numeric id of the primary group - see for example Why is my GID environment variable empty?

steeldriver
  • 131,985
  • 21
  • 239
  • 326