Listing all the groups to which a user belongs in macOS I want to know the groups of some user.

$dscl . list /groups filterByUSer theFindUser

Is this possible (even if I have to enumerate all the groups and then filter by user) in a straightforward manner?
Try the command given below.

id -Gn [user]


On my Mac, the output from id -Gn davidanderson is given below.

staff everyone localaccounts _appserverusr admin _appserveradm _lpadmin _appstore _lpoperator _developer _analyticsusers com.apple.access_ftp com.apple.access_screensharing com.apple.access_ssh 2 1


The delimiter could be changed from a space to a comma by entering the following.

groups="$(id -Gn davidanderson)"
groups="${groups// /,}"
echo "$groups"


Here, the output would be as follows.

staff,everyone,localaccounts,_appserverusr,admin,_appserveradm,_lpadmin,_appstore,_lpoperator,_developer,_analyticsusers,com.apple.access_ftp,com.apple.access_screensharing,com.apple.access_ssh,2,1


I am using High Sierra, macOs 10.13.4 and a bash shell.