Environment variables env in Linux

Introduction

An example - the path to the current directory.

You can get it with the pwd command (present working directory)

pwd

Which is equivalent

echo $PWD

/home/andrei

All environment variables

You can get the list of environment variables with the env

env

LC_PAPER=en_US.UTF-8 LC_ADDRESS=en_US.UTF-8 XDG_SESSION_ID=5422 HOSTNAME=demoad.demo.andrei.com LC_MONETARY=en_US.UTF-8 TERM=xterm-256color SHELL=/bin/bash HISTSIZE=1000 LC_NUMERIC=en_US.UTF-8 QTDIR=/usr/lib64/qt-3.3 QT_GRAPHICSSYSTEM_CHECKED=1 USER=andrei LS_COLORS=rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45: LC_TELEPHONE=en_US.UTF-8 SUDO_USER=andrei SUDO_UID=1000 USERNAME=root MAIL=/var/spool/mail/andrei PATH=/sbin:/bin:/usr/sbin:/usr/bin LC_IDENTIFICATION=en_US.UTF-8 PWD=/usr/share/i18n/locales LANG=en_US.UTF-8 LC_MEASUREMENT=en_US.UTF-8 HOME=/home/andrei SUDO_COMMAND=/bin/su SHLVL=2 LOGNAME=andrei LC_CTYPE=en_US.iso88591 LESSOPEN=||/usr/bin/lesspipe.sh %s XDG_RUNTIME_DIR=/run/user/1000 SUDO_GID=1000 LC_TIME=en_US.UTF-8 LC_NAME=en_US.UTF-8 _=/bin/env OLDPWD=/home/andrei

About the variables that start with LC - read the article locale

About the PATH variable read the article PATH in Linux

If your list is too long - redirect the output to less

env | less

Create your own variable

Quite simple. The main thing - do not put spaces

newvar=9
echo $newvar

9

Note that even when using sudo the value can still be obtained

sudo echo $newvar

9

Variable visibility

In the previous chapter we were able to see the value of the variable by running sudo

But will this variable be visible from another terminal.

Let us start a new terminal with the bash command (you can simply open a new window)

bash
echo $newvar

The variable is not visible.

Return to the old terminal by running exit

exit
echo $newvar

9

Here you can still see the value

export

To make the variable visible in all child terminals you apply the command export

export newvar
bash
echo $newvar

9

Now the variable is also visible in the child terminal

set -a: export every new variable

The command set -a switches on the bash option allexport. While it is on, every variable you create or change is exported at once, as if you had run export for it. set +a switches the option off again. The long forms of the same commands are set -o allexport and set +o allexport.

The option is off by default

set -o | grep allexport

allexport off

Switch it on and create a variable

set -a

othervar=10

set -o | grep allexport

allexport on

Switch it off and start a child terminal

set +a

bash

echo $othervar

10

The child terminal sees the variable although we never ran export for it. Return with exit.

set -a works only on assignments made while the option is on. Variables created before set -a, such as newvar from the chapter above, stay as they were, and variables created after set +a are not exported.

Load variables from a file

The usual reason to run set -a is a file of settings. Such a file holds plain NAME=value lines without export. Create one

vi settings.env

APP_HOST=example.com APP_USER=deploy

The command . (the same as source) runs the file in the current shell. On its own it only creates shell variables

. ./settings.env

echo $APP_HOST

example.com

env | grep APP_

env prints nothing: the variables are not in the environment, so programs you start from this shell do not get them.

Now load the file between set -a and set +a

set -a; . ./settings.env; set +a

env | grep APP_

APP_USER=deploy
APP_HOST=example.com

Both variables are exported now, and every program you start from this shell can read them from its environment.

Keep set +a at the end of the line: without it every variable you create later in this session would be exported too.

The file is run as bash code, so load only files you trust, and put values that contain spaces in quotes, for example APP_NAME="My app"

Article author: Andrei Olegovich

Banner image