Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Distributions»Ubuntu»Setting Up Environment Variables on Ubuntu

    Setting Up Environment Variables on Ubuntu

    RahulBy RahulMay 28, 20222 Mins ReadUpdated:May 28, 2022

    An environment variable contains a value, that is used to change the behaviors of the processes at run time. Similar to the other operating systems, we can also set the environment variables on a Ubuntu system.

    You can set the environment variables in 3 ways:

    1. Using the export command
    2. Using /etc/environment file
    3. Adding shell script under /etc/profile.d/ directory

    Now we will discuss the above methods to set environment variables on Ubuntu systems one by one.

    1. Using the export command

    You can use the export command on the terminal to set the environment variables temporarily. That variable will be accessible on the same terminal only. Once you close the terminal the variable will be destroyed.

    To set the environment variable, run:

    export MY_ENV=value 
    

    To print the MY_ENV environment variable, type:

    echo $MY_ENV
    

    2. Using /etc/enviroment file

    The /etc/environment is a system-wide configuration file used for setting the environment variables. It is not a shell script, it consists of the assignment expressions, that set the environment variables one per line.

    sudo vim /etc/environment
    
    /etc/environment
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin" JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 MY_HOME="/home/tecadmin"

    You can set multiple environment variables in this file. Each environment variable must be in a separate line.

    During the system reboot, the environment variable written in this file will automatically be assigned and accessible system-wide.

    3. Using /etc/profile.d/*.sh files

    You can also create a shell script under the /etc/profile.d directory. During the user login /etc/profile script is executed. Tha also executed all the shell scripts (files with .sh extension) under /etc/profile.d directory.

    Let’s create a shell script /etc/profile.d/custom-env.sh and set the environment variables using export command.

    sudo vim /etc/profile.d/custom-env.sh 
    

    Set the environment variables like:

    /etc/profile.d/custom-env.sh
    export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 export JRE_HOME=/usr/lib/jvm/java-17-openjdk-amd64 export MY_ENV="value1"

    The next time the user logged in will automatically set the environment variables. You can print the value of the environment variable using the echo command.

    echo $MY_ENV 
    
    value1
    
    Setting up environment variable in Ubuntu
    Print environment variable value

    Conclusion

    This tutorial provides you with the details of setting up the environment variables on the Ubuntu system. These environment variables are very helpful to change the run time behaviors of processes.

    I hope this tutorial helped you with the basic understanding of creating environment variables on Ubuntu and Debian systems. Please provide your valuable suggestions in the comments and do share this article with the social platform.

    environment variables Ubuntu
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleBackup MySQL Databases to Amazon S3 (Shell Script)
    Next Article How To Install XRDP on Ubuntu 22.04

    Related Posts

    How to Install Composer on Ubuntu 22.04

    Updated:June 24, 20223 Mins Read

    Setup Selenium with Python and Chrome on Ubuntu & Debian

    Updated:June 20, 20224 Mins Read

    How to Setup Squid Proxy Server on Ubuntu and Debian

    Updated:June 17, 20225 Mins Read

    How to Install Apache Kafka on Ubuntu 22.04

    Updated:June 5, 20225 Mins Read

    How to Install Apache Maven on Ubuntu 22.04

    Updated:June 2, 20223 Mins Read

    How to Install Apache Solr 9.0 on Ubuntu 22.04

    Updated:May 26, 20223 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    • How to Install Angular CLI on Ubuntu 22.04
    • How to Install Composer on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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