Atlantic.Net Blog

How to Set Environment Variables in Linux

Hitesh Jethva
by Atlantic.Net (198posts) under Dedicated Server Hosting
0 Comments

A variable is a location for storing a value which could be a filename, text, number or any other data.

A variable is usually referred to with its symbolic name. The value stored can be displayed, deleted, edited, and re-saved. Variables play an important role in computer programming because they enable programmers to write flexible programs.

An environment variable alters the dynamic values which affect the processes or programs on a computer. They exist in every operating system and their types vary. Environment variables also give information about system behavior and can change the behavior of software and programs.

In this tutorial, we will show you how to set environment variables in Linux.

Prerequisites

  • A fresh Ubuntu 20.04 VPS on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Step 1 – Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server.  Create a new server, choosing Ubuntu 20.04 as the operating system, with at least 1GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your Ubuntu 20.04 server, run the following command to update your base system with the latest available packages.

apt-get update -y

Step 2 – List Environment Variables

You can list all environment variables in your system by running the following command:

printenv

Or:

env

You should see the following screen:

There are a lot of environment variables available in Linux, including HOME, USER, EDITOR, PATH, LANG, TERM, MAIL, SHELL, and many more.

You can display the value of specific variable by passing the name of the variable to the printenv command.

For example, to display the value of the LANG variable, use the following command:

printenv LANG

You should see the following output:

en_US.UTF-8

Step 3 – Set up an Environment Variable

There are two types of variables: Shell variables and the Environment variables.

To create a new shell variable named COMPANY with value Atlantic, use the following command:

COMPANY="Atlantic"

Next, you can verify the variable using the echo command:

echo $COMPANY

You should see the value of your variable in the following output:

Atlantic

You can also verify whether the above variable is an environment variable or not using the printenv command:

printenv COMPANY

You should see the empty output which means the variable is not an environment variable.

To create a new environment variable named ORGANIZATION, use the export command:

export ORGANIZATION=Atlantic

You can verify the environment variable with the following command:

printenv ORGANIZATION

You should get the following output:

Atlantic

The environment variables which you have created in this way are available only in the current session. If you log out of your system, all variables will be lost.

You can also unset an environment variable you created earlier using the unset command followed by the variable name.

For example, to unset an ORGANIZATION variable, use the following command:

unset ORGANIZATION

Step 4 – Set up Persisting Environment Variable

When you set an environment variable using the export command, its existence ends when the user’s session ends, so it is recommended to set environment variables that are persist across sessions.

To set persisting environment variables globally, you will need to define them in /etc/profile file.

For example, you can set the persisting environment variable named COMPANY with value Atlantic by editing /etc/profile file as shown below:

nano /etc/profile

Add the following line at the end of the file:

export COMPANY=Atlantic

Save and close the file when you are finished, then activate the variable with the following command:

source /etc/profile

You can also set a persisting environment for a specific user by editing the user’s .bashrc file.

For example, set a persisting environment named VAR with value “My Linux Box” by editing the user’s .bashrc file:

nano ~/.bashrc

Add the following line at the end of the file:

export VAR="My Linux Box"

Save and close the file when you are finished, then activate the environment variable with the following command:

source ~/.bashrc

Conclusion

In the above guide, you learned how to set and unset an environment variable in Linux. Environment variables are very useful for developers who will need to read or alter the environment of your system. If you’re ready to try setting an environment variable in Linux, get started on dedicated server hosting from Atlantic.Net.

Get A Free To Use Cloud VPS

Free Tier Includes:
G3.2GB Cloud VPS Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources