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.

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

      nano /etc/environment

      Add the environment variable and save and exit.

    3. User-Specific Temporary Variable: We make use the export command.

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

      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 (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