Using The Bash Shell Environment

The Bash shell (bourne-again shell) is the most common default shell on most Linux distributions. Having a working knowledge of the environment is useful for any novice Linux user. This article will outline how to view, set, change, and retain environment variables in your shell.

Out-of-the-box Linux installs have a somewhat long list of environment variables that are set for various programs to use, so do not be alarmed by the amount of output.

1. How To View Bash Shell Environment Variables Using env

To view all the variables currently set in your Bash environment you can enter the command env. This will output all variables currently set in your environment. For example:

erik@debian:~$ env
TERM=xterm
SHELL=/bin/bash
SSH_CLIENT=X.X.X.X 51688 22
SSH_TTY=/dev/pts/2
USER=erik
LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=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:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.flac=01;35:*.mp3=01;35:*.mpc=01;35:*.ogg=01;35:*.wav=01;35:
SSH_AUTH_SOCK=/tmp/ssh-Hdelx27792/agent.27792
MAIL=/var/mail/erik
PATH=/usr/local/bin:/usr/bin:/bin:/usr/games
PWD=/home/erik
LANG=en_CA.UTF-8
HISTCONTROL=ignoredups
SHLVL=1
HOME=/home/erik
LOGNAME=erik
SSH_CONNECTION=X.X.X.X 51688 X.X.X.X 22
LESSOPEN=| /usr/bin/lesspipe %s
DISPLAY=localhost:11.0
LESSCLOSE=/usr/bin/lesspipe %s %s
_=/usr/bin/env

As you can see by the output, there are a large set of values. Some of which make logical sense. For instance, HOME=/home/erik is my home directory upon login. My current working directory of PWD=/home/erik. An interesting variable is the _=/usr/bin/env displays the last command executed by me.

2. Display Specific Environment Variable In Bash Using echo $VAR

Lets say there is a specific variable that you want to view instead of displaying the whole set and grepping for that variable. You can just enter the command echo $VAR where $VAR is the environment variable you want to view.

erik@debian:~$ echo $SHELL
/bin/bash

3. Create Environment Variable Temporarily In Bash

To temporarily create a variable in Bash you can simply enter erik=”hi” . Then doing a echo $erik the prompt will display “hi”. However, in doing so, the variable will not show up in a env output. To modify the variable you can:

erik@debian:~$ echo $erik
hi
erik@debian:~$ erik="no"
erik@debian:~$ echo $erik
no

These methods of creating variables is local only to this shell and will not be seen to child shells.

4. Creating Environment Variable In Bash Using export

For a Bash variable to persist to a child shell, and also show up during an env output, the user must use the export feature.

erik@debian:~$ data=hi
erik@debian:~$ echo $data
hi
erik@debian:~$ env | grep data
erik@debian:~$ export data
erik@debian:~$ env | grep data
data=hi

In this example I have created an environment variable called data in which a child shell could use. Keep in mind that this variable is only present in this session. If you were to logout, then log back in, the data variable would disappear.

5. Removing Environment Variable In Bash Using unset

At this point we now have the data variable in our environment. But perhaps we want to remove it because it smells bad. Its actually quite easy to do so.

erik@debian:~$ echo $data
hi
erik@debian:~$ env | grep data
data=hi
erik@debian:~$ unset data
erik@debian:~$ echo $data
 
erik@debian:~$ env | grep data
erik@debian:~$

So in this example I showed the data variable is set, and can be found in our environment. I then ran the unset command to remove the variable. I then tried to display the contents of our variable, and as you can see it no longer has a value and is no longer part of our environment.