How to Set Environment Variables in Linux

29/12/2020
Setting environment variables in Linux is a good way to define common and repetitive variables that are used across a number of applications and terminal commands. These environment variables help in cutting down verbosity, bring ease of use, and make development workflow better.

An environment variable in Linux can be used to pass information or influence the behavior of an application or a process. This article will explain various ways to set environment variables and how to use them.

To set an environment variable for the shell currently in use, define a variable in the following format:

$ MYVAR=”xyz”

The definition is self explanatory, “MYVAR” is the variable name while “xyz” is its value. Running the command below will verify if the environment variable has been set correctly:

$ echo $MYVAR

Notice the syntax of environment variables. While they work like any other shell variables, it is generally a good practice to use capital letters and underscores for left hand side (variable name).

To unset a variable, use the command below:

$ unset MYVAR

If you check the variable again using the echo command mentioned above, no output will be displayed. Note that unset will work for current terminal session only. If there are any global, system wide environment variables defined on your system, they will be available again in a new terminal session.

To set an environment variable for the shell currently in use and also for any child processes / shells launched from it, use a variable in the following format:

$ export MYVAR=”xyz”

To permanently set an environment variable for bash shells (most default terminal apps in Linux distributions are configured for bash shell), add the variable (with “export” keyword) at the end of the hidden .bashrc file in your home directory.

export MYVAR=”xyz”

You can edit .bashrc file by running the command below:

$ subl ~/.bashrc

Replace “subl” with the command of your favorite text editor. You will need to reload .bashrc file to enable the changes. Run the command below to do so:

$ source ~/.bashrc

Below is an example of custom environment variables I have set for Ruby Gems.

You can view all environment variables enabled on your system by running the command below:

$ env

To specifically check if the custom environment variable added to .bashrc file has been enabled or not, run the command below:

$ env | grep MYVAR=

To set an environment variable system wide for all apps, shells and processes, add your custom variable in “/etc/environment” file without “export” keyword.

MYVAR=”xyz”

You can edit “/etc/environment” file by running the command below:

$ sudo subl “/etc/environment”

Replace “subl” with your favorite text editor. You may need to reboot the system for the changes to take effect. To verify if your custom variable has been set correctly, run the command below:

$ env | grep MYVAR=

Alternatively, you can use “printenv” command to verify the changes:

$ printenv MYVAR

Note that the “unset” command explained above works for all custom environment variables, whether they are session specific or global variables. However, unset removes a variable for the running shell session only and it won’t remove any system wide or global variable permanently.

Some of the predefined environment variables in Ubuntu include:

  • USER – name of the logged-in user
  • HOME – home directory of logged in user (usually /home/username)
  • DISPLAY – active monitor in use (usually automatically set by login manager)
  • PWD – working directory where the shell is being used or invoked
  • SHELL – shell that is being used system wide (usually /bin/bash)
  • LANG – language used by the system (user defined, can be changed)
  • PATH – scripts / binaries / executables are searched in the directories set in the PATH variable

Some of the environment variables that are commonly used to influence application behavior:

  • LC_ALL – force overrides user defined locale with the value specified in the variable
  • LD_LIBRARY_PATH – used to define additional directories where runtime libraries will be searched
  • PATH – used to define additional directories where scripts / binaries / executables will be searched
  • LD_PRELOAD – used to load custom / downgraded / upgraded libraries in an application

This marks the end of this article. Environment variables in Linux helps in running tweaked commands and applications without actually modifying underlying source and binaries by providing a way to define and use global variables across the system.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Convert Hexadecimal to Decimal in Bash

Four types of number systems are popular in computer systems. These are Decimal, Binary, Octal and Hexadecimal. The binary...
29/12/2020

How Does PATH Work in Bash

When you’re typing a command in the Linux terminal, you’re generally calling a program to do a certain job, for example,...
29/12/2020

How to Debug a Bash Script like a Boss

A lot of things can happen in a bash script that unless you know how to debug like a boss, you might as well be up a creek...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024