Setting up environment variables on Ubuntu


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

You can set environment variables in 3 ways:

  1. Use the export command
  2. Using /etc/environment file
  3. Add shell script in /etc/profile.d/ directory

Now we will discuss the above methods to set each environment variable on Ubuntu system.

1. Use the export command

You can use the export command on the terminal to set temporary environment variables. That variable will only be accessible on the same terminal. When you close the terminal, the variable is destroyed.

To set an environment variable, run:

export MY_ENV=value 

To print the MY_ENV environment variable, type:

echo $MY_ENV

2. Use the file /etc/enviroment

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

sudo vim /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 system reboot, the environment variable written in this file is automatically assigned and accessible system-wide.

3. Use the file /etc/profile.d/*.sh

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

Let’s create a shell script /etc/profile.d/ and set the environment variables using export request.

sudo vim /etc/profile.d/ 

Set environment variables like:


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 logs in, the environment variables will be set automatically. You can print the value of an environment variable with the echo command.

echo $MY_ENV  value1
Print environment variable value


This tutorial gives you details on how to set environment variables on Ubuntu system. These environment variables are useful to change the runtime behavior of processes.

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

Hope this helps!

Source link