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?
Asked
Active
Viewed 5,469 times
3
1 Answers
5
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
- 142,475