Ubuntu: How to set Environment Variable



Environment variables are of great importance when you are building software applications based on the Ubuntu Operating system. They are used to store details about the environment in which the program or a script runs.


Types of Environment Variables:

    1. System-Wide Environment Variables: These environment variables apply to the entire operating system and affect all users.
    2. User-Specific Environment Variables: These environment variables are specific to individual user accounts and not the whole system.
    3. Temporary Environment Variables: These are only in effect for the duration of the current session, once the session is closed or the Terminal is exited, these settings will be lost.
    4. Permanent Environment Variables: These environment variables persist across sessions and reboots, even when the session is closed or the Terminal is exited, these settings will not be lost..

Examples of Setting Environment Variables

    1. System-Wide Temporary Environment Variables: We make use of the export command to set temporary variables.

      Example:
      export DEV_SERVER_URL="https://192.167.8.2:8000"
    2. System-Wide Permanent Environment Variables: We will to add the environemnt avriable to /etc/environment file to make it parmanent.

      Example:
      nano /etc/environment

      Add the environment variable and save and exit.

      JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
    3. User-Specific Temporary Variable: We make use the export command.

      Example:
      export EDITOR=nano
    4. User-Specific Permanent Variable: We add to the PATH variable under ~/.bashrc or ~/.zshrc

      Example:
      export PATH="$PATH:/home/user/bin"
How to set environment varaibles in Ubuntu Example
-

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


Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

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