3

When using "groups" or "id -Gn", I end up with the typical space-delimited list of all groups for the current user. These commands run on the assumption that group names cannot contain a space character, and indeed, as long as we stay within Unix, it's going to be the case.

However, my company is now part of a bigger one, that has Microsoft domains setups, and unfortunately, their Active Directory domain group names contain a space character, like "FOOBAR\Domain Users".

One of our scripts typically uses "groups" output and makes a list out of it, based on that space-character delimiter, which means that it now fails miserably:

$ groups
FOOBAR\Domain Users FOOBAR\Other Domain everyone admin 

... which obviously ends up producing such list:

FOOBAR\Domain
Users
FOOBAR\Other
Domain
everyone
admin

As you can imagine, the first 4 groups don't exist and the rest of the script fails to achieve anything of value.

Does anyone know where to obtain such group names in a better way?

PS: I know of /etc/group but such AD groups aren't mentioned there. Would there be another file like this, but for AD groups, that I could parse?

2
  • 1
    Not sure if it is good "answering practice" to reference an answer on a sister site, but I found this answer on Stack Overflow which deals with spaces in the group names: stackoverflow.com/a/50381984/737427
    – Integrator
    Sep 8, 2018 at 15:51
  • 1
    That is indeed exactly what I needed.
    – chris
    Sep 9, 2018 at 19:55

1 Answer 1

5

old post that I randomly ran across, but my current method is

id | egrep -o 'groups=.*' | sed 's/,/\n/g' | cut -d'(' -f2 | sed 's/)//'
1
  • For non english linux shell, use LC_ALL=C id | egrep -o 'groups=.*' | sed 's/,/\n/g' | cut -d'(' -f2 | sed 's/)//' Nov 27, 2020 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.