Linux

 

 

 

 

Environment Variable

 

Simply put, Environment Variable is a kind of Global Variable stored and maintained by the Operating System itself. These variables are used by various Shell command and other applications. Windows also have this kind of Environment variable as you can see in this example. If you have understandings on Windows Enviroment, you would easily understand Linux Environment Variable as well.

 

 

 

How to fine the list of Environment Variable in my system ?

 

The simplest way to find out what kind of variables are registered as Environment Variables (System Variables) in your system is to use the command env or printenv as shown below.

 

# env

  or

# printenv

 

XDG_VTNR=7

XDG_SESSION_ID=c1

CLUTTER_IM_MODULE=xim

XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/sharetechnote

SESSION=ubuntu

GPG_AGENT_INFO=/home/jaekuryu/.gnupg/S.gpg-agent:0:1

SHELL=/bin/bash

TERM=xterm-256color

VTE_VERSION=4205

QT_LINUX_ACCESSIBILITY_ALWAYS_ON=1

WINDOWID=56623114

UPSTART_SESSION=unix:abstract=/com/ubuntu/upstart-session/1000/1030

GNOME_KEYRING_CONTROL=

GTK_MODULES=gail:atk-bridge:unity-gtk-module

USER=sharetechnote

LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:...;36:

QT_ACCESSIBILITY=1

XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0

XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0

SSH_AUTH_SOCK=/run/user/1000/keyring/ssh

DEFAULTS_PATH=/usr/share/gconf/ubuntu.default.path

XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg

PATH=/home/sharetechnoe/bin:...:/usr/local/games:/snap/bin

DESKTOP_SESSION=ubuntu

QT_IM_MODULE=ibus

QT_QPA_PLATFORMTHEME=appmenu-qt5

XDG_SESSION_TYPE=x11

PWD=/home/sharetechnote

JOB=unity-settings-daemon

XMODIFIERS=@im=ibus

GNOME_KEYRING_PID=

LANG=en_CA.UTF-8

GDM_LANG=en_CA

MANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.path

IM_CONFIG_PHASE=1

COMPIZ_CONFIG_PROFILE=ubuntu-lowgfx

GDMSESSION=ubuntu

SESSIONTYPE=gnome-session

GTK2_MODULES=overlay-scrollbar

HOME=/home/sharetechnote

XDG_SEAT=seat0

SHLVL=1

LANGUAGE=en_CA:en

LIBGL_ALWAYS_SOFTWARE=1

GNOME_DESKTOP_SESSION_ID=this-is-deprecated

UPSTART_INSTANCE=

UPSTART_EVENTS=xsession started

XDG_SESSION_DESKTOP=ubuntu

LOGNAME=jaekuryu

COMPIZ_BIN_PATH=/usr/bin/

QT4_IM_MODULE=xim

XDG_DATA_DIRS=/usr/share/ubuntu:...:/var/lib/snapd/desktop

DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-e6LwXeFYYa

LESSOPEN=| /usr/bin/lesspipe %s

INSTANCE=

UPSTART_JOB=unity7

XDG_RUNTIME_DIR=/run/user/1000

DISPLAY=:0

XDG_CURRENT_DESKTOP=Unity

GTK_IM_MODULE=ibus

LESSCLOSE=/usr/bin/lesspipe %s %s

XAUTHORITY=/home/sharetechnote/.Xauthority

OLDPWD=/home

_=/usr/bin/env

 

 

If you want to list only a specific system variable and its value, you can print it out as shown below. This example print out the value assigned to the system variable named SHELL

 

# printenv SHELL

 

/bin/bash

 

 

 

How to create (add) your own variable to Environment Variable ?

 

You can define your own variable and add the variable to Environment Variable list.

 

First, you can create your own variable and assign the value as below.

 

# TESTVAR='HELLO WORLD'

 

 

Then, you can add the variable to Environment Variable list using 'export' command as shown below.

 

# export TESTVAR

 

 

Then you can confirm whether the variable is successfully registered to Environment variable as shown below.

 

# printenv | grep "TESTVAR"

 

TESTVAR=HELLO WORLD

 

 

Or, you can check the variable as shown below.

 

# printenv TESTVAR

 

HELLO WORLD

 

 

Or, you can check the variable as shown below.

 

# echo $TESTVAR

 

HELLO WORLD

 

 

 

How to change the value of an Environment Variable ?

 

Chaging the value of an Environment variable is simple. Just assign a new value to a variable as shown below.

 

# TESTVAR='Hi'

 

Then you can confirm whether the variable is successfully changed (updated) as shown below.

 

# printenv | grep "TESTVAR"

 

TESTVAR=Hi

 

 

Or, you can check the variable as shown below.

 

# printenv TESTVAR

 

Hi

 

 

Or, you can check the variable as shown below.

 

# echo $TESTVAR

 

Hi

 

 

 

How to remove an Environment Variable ?

 

You can remove an Environment Variable using export command (the same command that is used to add a variable) with '-n' option as shown below.

 

# export -n TESTVAR