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:
Now we will discuss the above methods to set environment variables on Ubuntu systems one by one.
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
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/environmentPATH="/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.
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.shexport 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
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.
The post Setting Up Environment Variables on Ubuntu appeared first on TecAdmin.
The Web Engineering team is thrilled to announce the successful conclusion of our third annual…
This article provides a guide for how to install Microsoft SQL Server on Ubuntu VPS.…
Here’s a step-by-step guide to deploy FlowiseAI (commonly just Flowise) on a Ubuntu VPS using…
Ubuntu images on Microsoft Azure have recently started shipping with the open source package azure-vm-utils…
Organizations everywhere are pushing AI and networks closer to the edge. With that expansion comes…
Every developer who maintains Ruby gems knows that sinking feeling when a user reports an…