Where are Environment Variables stored in Ubuntu


The environment variables can be stored in various configuration files depending upon the program that you are referring to as well the scope - system-wide or user-specific.


System-Wide Environment Variables:

    You can find the system-wide environment variables under /etc/environment file.

    # cat /etc/environment
    
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:
         /usr/games:/usr/local/games:/snap/bin"

User-Specific Environment Variables:

    Based on what shell you use, you can find them under the ~/.bashrc or ~/.zshrc files under the home directory.

    Example:
    cat ~/.bashrc
    
    # ~/.bashrc: executed by bash(1) for non-login shells.
    # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
    # for examples
    
    # If not running interactively, don't do anything
    [ -z "$PS1" ] && return
    
    # don't put duplicate lines in the history. See bash(1) for more options
    # ... or force ignoredups and ignorespace
    HISTCONTROL=ignoredups:ignorespace
    
    # append to the history file, don't overwrite it
    shopt -s histappend
    
    # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
    HISTSIZE=1000
    HISTFILESIZE=2000
Location where environment variables are stored Ubuntu

Know the Temporary Environment variables:

    If you are looking for the temporary environment variables set for the current session then you can make use of the printenv command.

    Example:
    # printenv
    
    HOSTNAME=b9fc21f2fc40
    HOME=/root
    TERM=xterm
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/user/bin
    DEV_SERVER_URL=https://192.167.8.2:8000
    PWD=/

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap