You have to surround the variable with parentheses when declaring an array. Also, since you know the first element before the first IFS (space) is always going to be "GroupMembership:", you could just include every element beyond the first with awk This should work:
# Build an array of all group members of the admin group. Use awk to truncate the first field.
USERS=($(/usr/bin/dscl . read /Groups/admin GroupMembership | /usr/bin/awk '{print substr($0,index($0,$2))}'))
# Verify the array is built properly
for i in "${USERS[@]}"; do
echo "User: $i"
done
You have to surround the variable with parentheses when declaring an array. Also, since you know the first element before the first IFS (space) is always going to be "GroupMembership:", you could just include every element beyond the first with awk This should work:
# Build an array of all group members of the admin group. Use awk to truncate the first field.
USERS=($(/usr/bin/dscl . read /Groups/admin GroupMembership | /usr/bin/awk '{print substr($0,index($0,$2))}'))
# Verify the array is built properly
for i in "${USERS[@]}"; do
echo "User: $i"
done