Why is my GID environment variable empty?

8,902

I assume you're using bash as your shell. Bash doesn't set a GID variable. The list of Bash variables mentions EUID and UID, but not GID.

Zsh, on the other hand, does set GID:

$ bash -c 'echo $GID'

$ zsh -c 'echo $GID'
1000
Share:
8,902

Related videos on Youtube

Corey
Author by

Corey

Working & Traveling in Canada. I was a cloud engineer, developed backend REST API, optimized the stability of Windows services running on the cloud platform. Automized the generation of Windows images, and maintain automated CI/CD pipelines for code deployment. Skilled in Python, Java, and Scripting, with experience in Openstack and DevOps.

Updated on September 18, 2022

Comments

  • Corey
    Corey over 1 year

    I try echo $GID but get nothing. However, I can get 1000 by using id -g, what's the differences between them ?

    id -u     => 1000
    id -g     => 1000
    echo $UID => 1000
    echo $GID => 
    

    The output of id:

    uid=1000(user) gid=1000(user) groups=1000(user),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare),999(docker)
    

    The output of groups:

    user adm cdrom sudo dip plugdev lpadmin sambashare docker
    

    Ubuntu version:

    Distributor ID: Ubuntu
    Description:    Ubuntu 16.04.6 LTS
    Release:    16.04
    Codename:   xenial
    
  • Corey
    Corey about 5 years
    What's the differences between using bash -c 'echo $GID' and echo $GID in terminal ?
  • Olorin
    Olorin about 5 years
    @Corey echo $GID in my terminal would run it in zsh, since I use zsh as my shell. In your terminal, it might be run in bash. I use bash -c ... to run the command specifically in bash.
  • Corey
    Corey about 5 years
    Got it, thanks. Is there any way in bash to get GID except id -g ?
  • Prvt_Yadav
    Prvt_Yadav about 5 years
    @Corey use command cat /etc/group | grep ^your_group_name | cut -d: -f3
  • Corey
    Corey about 5 years
    Is it a good idea to use $GROUPS as a replacement ?
  • Olorin
    Olorin about 5 years
    @Corey if you're using bash, possibly. But I don't know if it's guaranteed to hold your GID as the first element of the array.