Working with environmental variables in Linux


Shell environment

When working with your server through a shell session, there are many pieces of information that your shell compiles to determine its behavior and access to resources. Some of these settings are contained within configuration settings and others are determined by user input. One way that the shell keeps track of many of of these settings is through an area it maintains called the environment. The environment is an area that the shell builds every time that it starts a session that contains variables that define system properties.

Environmental variables

Environmental variables are variables that are defined for the current shell and are inherited by any child shells or processes. Environmental variables are used to pass information into processes that are spawned from the shell. An environment variable are represent as key-value pairs.

to show environmental variables
  • printenv – Print all or part of environment.
  • env – Print all exported environment or run a program in a modified environment.
# printenv
HOSTNAME=myhostname
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=172.16.51.15 61242 22
SELINUX_USE_CURRENT_RANGE=
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
SSH_TTY=/dev/pts/0
USER=root
...

to set environmental viariables

To set an existing variable only for current shell

VARNAME="my value"

Make sure not to add extra spaces before or after Equals Sign(=)


Using export to set variables for current shell and all processes started from current shell
export VARNAME="my value"   

To set variables permanently for all future interactive bash sessions
  • add export statement to your .bashrc file in your $HOME directory.
for example in /root/.bashrc,

export MYVAR3=value3
export MYVAR4=1004

To set values permanently, and system wide (all users, all processes)
  • add set variable in /etc/environment

The /etc/environment file is only used for setting environment variables.

Alternately, add to directory

  • /etc/profile

/etc/profile initializes variables for login shells only. It is also used to run scripts and can be used by all Bourne shell compatible shells.

Instead of etc/profile, consider creating a new shell script under directory /etc/profile.d/. These files will not be modified on an upgrade.

  • /etc/profile.d/

A list of some common environmental variables
System Variable Meaning To View Variable Value Type
BASH_VERSION Holds the version of this instance of bash. echo $BASH_VERSION
HOSTNAME The name of the your computer. echo $HOSTNAME
CDPATH The search path for the cd command. echo $CDPATH
HISTFILE The name of the file in which command history is saved. echo $HISTFILE
HISTFILESIZE The maximum number of lines contained in the history file. echo $HISTFILESIZE
HISTSIZE The number of commands to remember in the command history. The default value is 500. echo $HISTSIZE
HOME The home directory of the current user. echo $HOME
IFS The Internal Field Separator that is used for word splitting after expansion and to split lines into words with
the read builtin command. The default value is <space><tab><newline>.
echo $IFS
LANG Used to determine the locale category for any category not specifically selected with a variable starting with LC_. echo $LANG
PATH The search path for commands. It is a colon-separated list of directories in which the shell looks for commands. echo $PATH
PS1 Your prompt settings. echo $PS1
TMOUT The default timeout for the read builtin command. Also in an interactive shell, the value is interpreted as
the number of seconds to wait for input after issuing the command. If not input provided it will logout user.
echo $TMOUT
TERM Your login terminal type. echo $TERM
export TERM=vt100
SHELL Set path to login shell. echo $SHELL
DISPLAY Set X display name echo $DISPLAY
export DISPLAY=:0.1
EDITOR Set name of default text editor. export EDITOR=/usr/bin/vim


PS1 - Linux shell prompt

The Linux shell prompt is set up with environmental variable PS1. By default this variable displays hostname and current working directory. You can easily customize your prompt to display information important to you. The default value is \s-\v\$ .

To display current prompt

$ echo $PS1
\u@\h \W]\$

Some prompt switches

    \d : the date in “Weekday Month Date” format (e.g., “Tue May 26”) 
    \H : the hostname
    \t : the current time in 24-hour HH:MM:SS format
    \u : the username of the current user
    \w : the current working directory, with $HOME abbreviated with a tilde

Example:

PS1="\d \H \t \u \w"


http_proxy

One environment variable is called http_proxy. It allows you to connect text based session and/or applications via the proxy server. In this variable we indicate the proxy server IP address (URL) and port values. This variable is almost used by utilities such as elinks, lynx, wget, curl and others commands. You may create a file called /etc/profile.d/proxy.sh to hold these environmental variables.

Examples:

 export http_proxy=http://server-ip:port/
 export http_proxy=http://127.0.0.1:3128/
 export http_proxy=http://proxy-server.mycorp.com:3128/

If the proxy server requires a username and password then add these to the URL. For example, to include the username foo and the password bar:

 export http_proxy=http://foo:bar@server-ip:port/
 export http_proxy=http://foo:bar@127.0.0.1:3128/
 export http_proxy=http://USERNAME:PASSWORD@proxy-server.mycorp.com:3128/

the PATH variable

The PATH is an environmental variable that tells the shell which directories to search for executable files in response to commands issued by a user. A user's PATH consists of a series of colon-separated absolute paths that are stored in plain text files.

adding to the PATH variable:

Add /home/mypath to beginning of PATH

PATH=/home/mypath:$PATH

Add /home/mypath to end of PATH

PATH=$PATH:/home/mypath

Using export to make changes to PATH for current shell and all processes started from current shell

export PATH=$PATH:/home/mypath

To set variables permanently for all your future bash sessions, add export statement to your .bashrc file in your $HOME directory.

vi /home/myuser/.bashrc