YouTube Icon

Code Playground.

How to Set and List Environment Variables in Linux

CFG

How to Set and List Environment Variables in Linux

In Linux and Unix based frameworks climate factors are a bunch of dynamic named values, put away inside the framework that are utilized by applications dispatched in shells or subshells. In basic words, a climate variable is a variable with a name and a related worth. 

Climate factors permit you to modify how the framework functions and the conduct of the applications on the framework. For instance, the climate variable can store data about the default content manager or program, the way to executable documents, or the framework region and console design settings. 

In this guide, we will disclose to peruse and set climate and shell factors. 

Environment Variables and Shell Variables

Factors have the accompanying configuration: 

KEY=value
KEY="Some other value"
KEY=value1:value2

The names of the factors are case-delicate. By show, climate factors ought to have UPPER CASE names. 

When doling out different qualities to the variable they should be isolated by the colon : character. 

There is no space around the equivalents = image. 

Factors can be ordered into two fundamental classifications, climate factors, and shell factors. 

Climate factors are factors that are accessible framework wide and are acquired by totally brought forth kid cycles and shells. 

Shell factors are factors that apply just to the current shell occasion. Each shell, for example, zsh and slam, has its own arrangement of interior shell factors. 

There are a few orders accessible that permit you to rundown and set climate factors in Linux: 

  • env – The order permits you to run another program in a custom climate without adjusting the current one. At the point when utilized without a contention it will print a rundown of the current climate factors. 
  • printenv – The order prints all or the predefined climate factors. 
  • set – The order sets or unsets shell factors. At the point when utilized without a contention it will print a rundown of all factors including climate and shell factors, and shell capacities. 
  • unset – The order erases shell and climate factors. 
  • trade – The order sets climate factors. 

List Environment Variables

The most utilized order to shows the climate factors is printenv. On the off chance that the name of the variable is passed as a contention to the order, just the estimation of that variable is shown. On the off chance that no contention is indicated, printenv prints a rundown of all climate factors, one variable for each line. 

For instance, to show the estimation of the HOME climate variable you would run: 

printenv HOME

The yield will print the way of the as of now signed in client: 

/home/linuxize

You can likewise pass more than one contentions to the printenv order: 

printenv LANG PWD
en_US
/home/linuxize

In the event that you run the printenv or env order with no contentions it will show a rundown of all climate factors: 

printenv

The yield will look something like this: 

LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35;...
LESSCLOSE=/usr/bin/lesspipe %s %s
LANG=en_US
S_COLORS=auto
XDG_SESSION_ID=5
USER=linuxize
PWD=/home/linuxize
HOME=/home/linuxize
SSH_CLIENT=192.168.121.1 34422 22
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
SSH_TTY=/dev/pts/0
MAIL=/var/mail/linuxize
TERM=xterm-256color
SHELL=/bin/bash
SHLVL=1
LANGUAGE=en_US:
LOGNAME=linuxize
XDG_RUNTIME_DIR=/run/user/1000
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
LESSOPEN=| /usr/bin/lesspipe %s
_=/usr/bin/printenv

The following are probably the most well-known climate factors: 

  • Client - The current signed in client. 
  • HOME - The home registry of the current client. 
  • Supervisor - The default document manager to be utilized. This is the editorial manager that will be utilized when you type alter in your terminal. 
  • SHELL - The way of the current client's shell, for example, slam or zsh. 
  • LOGNAME - The name of the current client. 
  • Way - A rundown of registries to be looked through when executing orders. At the point when you run an order the framework will look through those registries in a specific order and utilize the primary discovered executable. 
  • LANG - The current regions settings. 
  • TERM - The current terminal imitating. 
  • MAIL - Location of where the current client's mail is put away. 

The printenv and env orders print just the climate factors. On the off chance that you need to get a rundown, all things considered, including climate, shell and factors, and shell capacities you can utilize the set order: 

set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()

The order will show a huge rundown, all things considered, so you presumably need to pipe the yield to the less order. 

set | less

You can likewise utilize the reverberation order to print a shell variable. For instance, to print the estimation of the BASH_VERSION variable you would run: 

echo $BASH_VERSION
4.4.19(1)-release

Setting Environment Variables

To all the more likely delineate the contrast between the Shell and Environment factors we'll begin with setting Shell Variables and afterward proceed onward to the Environment factors. 

To make another shell variable with the name MY_VAR and worth Linuxize basically type: 

MY_VAR='Linuxize'

You can check that the variable is set by utilizing either reverberation $MY_VAR of separating the yield of the set order with grep set | grep MY_VAR: 

echo $MY_VAR
Linuxize

Utilize the printenv order to check if this variable is a climate variable: 

printenv MY_VAR

The yield will be unfilled which reveal to us that the variable is definitely not a climate variable. 

You can likewise attempt to print the variable in a sub-shell and you will get a vacant yield. 

bash -c 'echo $MY_VAR'

The fare order is utilized to set Environment factors. 

To establish a climate variable essentially trade the shell variable as a climate variable: 

export MY_VAR

You can check this by running: 

printenv MY_VAR
Linuxize

In the event that you attempt to print the variable in a sub-shell this time you will get the variable name imprinted on your terminal: 

bash -c 'echo $MY_VAR'
Linuxize

You can likewise set climate factors in a solitary line: 

export MY_NEW_VAR="My New Var"

Climate Variables made in this manner are accessible just in the current meeting. In the event that you open another shell or on the off chance that you log out all factors will be lost. 

Persistent Environment Variables

To make Environment factors tireless you have to characterize those factors in the slam design records. In most Linux circulations when you start another meeting, climate factors are perused from the accompanying documents: 

/and so forth/climate - Use this document to set up framework wide climate factors. Factors in this record are set in the accompanying organization: 

FOO=bar
VAR_TEST="Test Var"

/and so forth/profile - Variables set in this document are stacked at whatever point a slam login shell is entered. While proclaiming climate factors in this record you have to utilize the fare order: 

export JAVA_HOME="/path/to/java/home"
export PATH=$PATH:$JAVA_HOME/bin

Per-client shell explicit design records. For instance, in the event that you are utilizing Bash, you can proclaim the factors in the ~/.bashrc: 

export PATH="$HOME/bin:$PATH"

To stack the new climate factors into the current shell meeting utilize the source order: 

source ~/.bashrc

Conclusion

In this guide, we have told you the best way to set and rundown climate and shell factors. 

Don't hesitate to leave a remark on the off chance that you have any inquiries.




CFG