Close Menu
    Facebook X (Twitter) Instagram
    • About
    • Privacy Policy
    • Write For Us
    • Newsletter
    • Contact
    Instagram
    About ChromebooksAbout Chromebooks
    • News
      • Stats
    • AI
    • How to
      • DevOps
      • IP Address
    • Apps
    • Business
    • Q&A
      • Opinion
    • Gaming
      • Google Games
    • Blog
    • Podcast
    • Contact
    About ChromebooksAbout Chromebooks
    Linux

    How to Set an Environment Variable in Linux

    Dominic ReignsBy Dominic ReignsJanuary 8, 2026Updated:January 8, 2026No Comments4 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest

    Environment variables store temporary data like paths, configuration settings, and tokens. Programs and scripts access these values without hardcoding them into files. This guide shows how to set an environment variable in Linux for current sessions and across system restarts.

    What Are Environment Variables in Linux

    Environment variables store system and user configuration data. Each user has a unique set of variables that define their working environment. An admin account differs from a standard user account in terms of available variables and permissions.

    Variables can be user-specific or system-wide. Examples include personal API keys visible only to one user or global paths accessible to all users.

    Common environment variables:

    Variable Description
    USER Current logged-in username
    HOME User home directory path
    SHELL Active shell path like bash or zsh
    LANG System language and locale settings
    MAIL User mailbox location
    PATH Directories searched for executables

    These variables exist only for the current session unless configured for persistence.

    How to List Environment Variables in Linux

    Display all environment variables for your session:

    $ env

    Output shows all defined variables:

    SHELL=/bin/bash
    PWD=/home/user
    USER=user
    HOME=/home/user
    LANG=C.UTF-8
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    MAIL=/var/mail/user

    How to Print Environment Variables in Linux

    Two methods display specific variable values.

    Using printenv Command

    Print a single variable:

    $ printenv SHELL

    Returns:

    /bin/bash

    Using echo Command

    Print using the dollar sign prefix:

    $ echo $SHELL

    Returns:

    /bin/bash

    Both methods produce identical output. The echo command requires the dollar sign before the variable name.

    How to Set Environment Variables in Linux

    Use the export command to define variables:

    $ export JAVA_HOME=/usr/bin/java

    Verify the variable exists:

    $ echo $JAVA_HOME

    Returns:

    /usr/bin/java

    Variables set this way exist only in the current session. Close the terminal and the variable disappears.

    Warning: Variables defined with export are temporary. They do not persist after closing the terminal or logging out.

    How to Make Environment Variables Persistent in Linux

    Store variables in configuration files to preserve them across sessions.

    Edit .bashrc for User Variables

    Open the .bashrc file in your home directory:

    $ vi ~/.bashrc

    Add the export statement at the end:

    export JAVA_HOME=/usr/bin/java

    Apply changes without logging out:

    $ source ~/.bashrc

    Test by opening a new terminal:

    $ echo $JAVA_HOME

    The variable persists across all future sessions for your user account.

    Note: The .bashrc file runs automatically when you open a new terminal session. Changes take effect immediately after running the source command.

    How to Create a Global Environment Variable in Linux

    Global variables are accessible by all users on the system. Define them in system-wide configuration files.

    Using /etc/environment

    Edit the file with root privileges:

    # vi /etc/environment

    Add the variable without using export:

    GLOBAL_VARIABLE="This is a global variable"

    Apply changes:

    # source /etc/environment

    Using /etc/profile

    Edit the profile file:

    # vi /etc/profile

    Add the variable with export:

    export GLOBAL_VARIABLE="This is a global variable"

    Test from any user account:

    $ echo $GLOBAL_VARIABLE

    Returns:

    This is a global variable

    All users can now access this variable regardless of their session.

    Tip: Use /etc/environment for simple variable assignments. Use /etc/profile when you need to set variables that depend on shell features or commands.

    How to Verify Environment Variables Work

    Check if a variable persists across sessions:

    Open a new terminal window.

    Print the variable:

    $ echo $JAVA_HOME

    If the output shows the value, the variable is persistent. Empty output means the variable was not saved correctly or the configuration file was not sourced.

    Verify global variables by switching users:

    $ su - username
    $ echo $GLOBAL_VARIABLE

    The variable should display for all user accounts.

    FAQS

    Add the export command to your .bashrc file in your home directory, then run source ~/.bashrc to apply changes immediately.

    .bashrc runs for interactive non-login shells, while .bash_profile runs only for login shells. Use .bashrc for most environment variables.

    Run the env command in your terminal to display all environment variables and their current values for your session.

    Yes, edit /etc/environment or /etc/profile with root privileges to create system-wide variables accessible to all users on the system.

    Use the unset command followed by the variable name: unset VARIABLE_NAME. This removes it from the current session only.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr
    Dominic Reigns
    • Website
    • Instagram

    As a senior analyst, I benchmark and review gadgets and PC components, including desktop processors, GPUs, monitors, and storage solutions on Aboutchromebooks.com. Outside of work, I enjoy skating and putting my culinary training to use by cooking for friends.

    Related Posts

    How To Install NPM Linux

    January 26, 2026

    How To Check Linux List Processes

    January 24, 2026

    Wget Mac Installation Guide

    January 23, 2026

    Comments are closed.

    Best of AI

    Stable Video Diffusion User Trends And Statistics 2026

    January 29, 2026

    VALL-E Statistics 2026

    January 28, 2026

    StarCoder Statistics And User Trends 2026

    January 27, 2026

    BLIP-2 Statistics 2026

    January 23, 2026

    AI mode Usage Statistics 2026

    January 22, 2026
    Trending Stats

    Search engine operators Statistics 2026

    January 29, 2026

    Most searched keywords on Google

    January 27, 2026

    Ahrefs Search Engine Statistics 2026

    January 19, 2026

    Pay Per Click Advertising Statistics 2026

    January 16, 2026

    Google Ads Revenue 2025

    November 29, 2025
    • About
    • Write For Us
    • Contact
    • Privacy Policy
    • Sitemap
    © 2026 About Chrome Books. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.