How to List and Set Environment Variables or Shell Variables in Linux

This post will guide you how to list all environment variables using bash shell on CentOS or RHEL Linux server. How do I list all shell variables through command line on CentOS or RHEL Linux 6/7. How to create shell variable or environmental variable through shell command on Linux system.

list and set shell variable1

What is Shell Variable and Environment Variable


Standard UNIX variables are split into two categories, environment variables and shell variables. A shell variable is a variable that is available only to the current shell. An environment variable is available system wide and can be used by other applications on the system.

List All Environment Variables


You should know that each shell session keeps track of its own shell and environmental variables. And if you want to list all environment variables in your current shell session, you can use one of the following commands:

$ printenv

or

$ env

Outputs:

[devops@devops ~]$ printenv
XDG_SESSION_ID=2
HOSTNAME=devops
SELINUX_ROLE_REQUESTED=
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
SSH_CLIENT=192.168.3.29 1914 22
CONDA_SHLVL=1
SELINUX_USE_CURRENT_RANGE=
CONDA_PROMPT_MODIFIER=(base)
SSH_TTY=/dev/pts/0
USER=devops
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg= 30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4= 01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01 ;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01 ;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz= 01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*. xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01 ;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wm v=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.x wd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36: *.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01; 36:*.xspf=01;36:
CONDA_EXE=/root/anaconda3/bin/conda
PATH=/root/anaconda3/bin:/root/anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
MAIL=/var/spool/mail/root
CONDA_PREFIX=/root/anaconda3
PWD=/home/devops
LANG=en_US.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
HOME=/home/devops
SHLVL=2
LOGNAME=devops
CONDA_PYTHON_EXE=/root/anaconda3/bin/python
SSH_CONNECTION=192.168.3.29 1914 192.168.3.22 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
CONDA_DEFAULT_ENV=base
XDG_RUNTIME_DIR=/run/user/0
OLDPWD=/root
_=/usr/bin/printenv

Note:
The env command allows you to print all exported environment variables or you can also use it to run a program in a modified environment.

The printenv command can be used to print all of environment variables or the specified environment variables.

If you want to display the value of a specified environment variable, you can run the following command:

$ print HOME

Outputs:

[devops@devops ~]$ printenv HOME
/home/devops

List All Shell Variables


If you want to list all shell variables in your current shell session, you can use set command without any additional parameters, then it will list all shell variables, environment variables, local variables, and shell functions, type:

$ set

Outputs:

[devops@devops ~]$ set
BASH=/usr/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="2" [2]="46" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.2.46(2)-release'
COLUMNS=161
CONDA_DEFAULT_ENV=base
CONDA_EXE=/root/anaconda3/bin/conda
CONDA_PREFIX=/root/anaconda3
CONDA_PROMPT_MODIFIER='(base) '
CONDA_PYTHON_EXE=/root/anaconda3/bin/python
CONDA_SHLVL=1
DIRSTACK=()
EUID=1002
GROUPS=()
HISTCONTROL=ignoredups
HISTFILE=/home/devops/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/home/devops
HOSTNAME=devops
HOSTTYPE=x86_64
ID=1002
IFS=$' \t\n'
LANG=en_US.UTF-8
LESSOPEN='||/usr/bin/lesspipe.sh %s'
LINES=54
LOGNAME=devops

Note: The set command can be used to create or unset shell variables in your current shell session. If you do not follow any arguments, it will print a list of all variables.

Common Environmental and Shell Variables


Here are some common environment variables that you may be want to know:
SHELL: his describes the shell that will be interpreting any commands you type in.
TERM: This specifies the type of terminal to emulate when running the shell.
USER: The current logging user
HOSTNAME: The hostname of your system
PWD: the current working directory
PATH: a list of directories that the system will check when searching for commands.
HOME: the current user’s home directory
DISPLAY: set X dispaly name
LANG: the current language and localization settings.
MAIL: the path to the current user’s mailbox.

Creating Shell Variables and Environmental variables


If you want to create you own shell variable in your current shell session, you just need to specify a name and a value.

For example, you wish to define a new shell vairable named “test“, and set its value as “this is a variable“, just type the following line in your shell:

$ test="this is a variable"

Then you should create a shell variable. And this variable is only available in your current session, and it won’t to pass down to child processes or other shell session.

You can use set command with grep command to check if it is created successfully, type:

$ set | grep test

Outputs:

[devops@devops ~]$ set | grep test
test='this is a variable'

You can also use echo command to print the value of any shell or environment variables, type:

$ echo $test

Outputs:

[devops@devops ~]$ echo $test
this is a variable

If you want to trun your shell variable into an environment variable, you just need to use export command to export it, just type:

$ export test

then you can use printenv command in combination with grep command to check if it is in environment list, type:

$ printenv | grep test

Outputs:

[devops@devops ~]$ printenv | grep test
test=this is a variable

Conclusion


You should know that how to list shell variables and environmental variables in your shell session on your CentOS or RHEL Linux 6/7.

You might also like:

Sidebar



back to top