Skip to content
Advertisement

List all groups on machine via bash script

#!/bin/bash
GROUPS=$(cut -d: -f1 /etc/group)
echo "$GROUPS"

This code gives me an output of 1000 if I’m a regular user or 0 if I’m root, while executing the same command (cut -d: -f1 /etc/group) returns a list of all the available groups on the machine. I also tried with grep "" /etc/group but that produces the same results. What am I not getting here?

Advertisement

Answer

The proper way is getent group|cut -d: -f1, because there are some more authentication method than /etc/group. e.g. sql, ldap, etc… And it is not recommended to use capitalized variable names in shellscript – exactly to prevent this.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement