Linux Environment

Controlling the bash Environment

The default command line interpreter on Kamiak is bash (the Bourne Again SHell). Changes to your user experience can be made by altering the “bash environment”.

Environment Variables

There are certain variables whose values determine the behavior of the bash environment. These are called environment variables and are traditionally denoted by ALL CAPS. To see the values of all our environment variables we can invoke the env command

-bash-4.2$ env
MANPATH=/opt/apps/lmod/lmod/share/man::
XDG_SESSION_ID=192
HOSTNAME=login-p1n02.kamiak.wsu.edu
TERM=xterm-256color
SHELL=/bin/bash
HISTSIZE=1000
MODULEPATH_ROOT=/opt/apps/modulefiles
SSH_CLIENT=67.185.209.100 52762 22
PERL5LIB=/opt/moab/lib/perl5
LMOD_PKG=/opt/apps/lmod/lmod
LMOD_VERSION=6.0.1
SSH_TTY=/dev/pts/4
QT_GRAPHICSSYSTEM_CHECKED=1
USER=wsuNID
LMOD_sys=Linux
LMOD_PREPEND_BLOCK=normal
MAIL=/var/spool/mail/wsuNID
PATH=/usr/local/bin:/usr/local/sbin:/opt/moab/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/ibutils/bin:/opt/dell/srvadmin/bin
LMOD_SETTARG_CMD=:
PWD=/home/wsuNID
LANG=en_US.UTF-8
MODULEPATH=/opt/apps/modulefiles/Linux:/opt/apps/modulefiles/Core:/opt/apps/lmod/lmod/modulefiles/Core
MOABHOMEDIR=/opt/moab
LMOD_CMD=/opt/apps/lmod/lmod/libexec/lmod
HISTCONTROL=ignoredups
SHLVL=1
HOME=/home/wsuNID
BASH_ENV=/opt/apps/lmod/lmod/init/bash
LMOD_arch=x86_64
LOGNAME=wsuNID
SSH_CONNECTION=67.185.209.100 52762 134.121.192.227 22
MODULESHOME=/opt/apps/lmod/lmod
LESSOPEN=||/usr/bin/lesspipe.sh %s
LMOD_FULL_SETTARG_SUPPORT=no
XDG_RUNTIME_DIR=/run/user/1005
LMOD_DIR=/opt/apps/lmod/lmod/libexec
LMOD_COLORIZE=yes
-bash-4.2$ 

This list of variables can easily be searched by “piping” (|) its output to grep

-bash-4.2$ env | grep PATH
MANPATH=/opt/apps/lmod/lmod/share/man::
MODULEPATH_ROOT=/opt/apps/modulefiles
PATH=/usr/local/bin:/usr/local/sbin:/opt/moab/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/ibutils/bin:/opt/dell/srvadmin/bin
MODULEPATH=/opt/apps/modulefiles/Linux:/opt/apps/modulefiles/Core:/opt/apps/lmod/lmod/modulefiles/Core
-bash-4.2$

To use the information stored in the environment variable, we invoke it with the prefix $.

-bash-4.2$ pwd
/home/wsuNID/exampleDirectory
-bash-4.2$ cd $HOME
-bash-4.2$ pwd
/home/wsuNID
-bash-4.2$

The value of a single variable can be seen by using the echo command

-bash-4.2$ echo $HOME
/home/wsuNID
-bash-4.2$

An the value environment variable can be changed (or a new variable created) by invoking export.

-bash-4.2$ export EXAMPLEDIR=$HOME/exampleDirectory/
-bash-4.2$ echo $EXAMPLEDIR
/home/wsuNID/exampleDirectory
-bash-4.2$

There are certain variables whose values determine the behavior of the bash environment. These are called environment variables and are traditionally denoted by ALL CAPS. To see the values of all our environment variables we can invoke the env command

-bash-4.2$ env
MANPATH=/opt/apps/lmod/lmod/share/man::
XDG_SESSION_ID=192
HOSTNAME=login-p1n02.kamiak.wsu.edu
TERM=xterm-256color
SHELL=/bin/bash
HISTSIZE=1000
MODULEPATH_ROOT=/opt/apps/modulefiles
SSH_CLIENT=67.185.209.100 52762 22
PERL5LIB=/opt/moab/lib/perl5
LMOD_PKG=/opt/apps/lmod/lmod
LMOD_VERSION=6.0.1
SSH_TTY=/dev/pts/4
QT_GRAPHICSSYSTEM_CHECKED=1
USER=wsuNID
LMOD_sys=Linux
LMOD_PREPEND_BLOCK=normal
MAIL=/var/spool/mail/wsuNID
PATH=/usr/local/bin:/usr/local/sbin:/opt/moab/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/ibutils/bin:/opt/dell/srvadmin/bin
LMOD_SETTARG_CMD=:
PWD=/home/wsuNID
LANG=en_US.UTF-8
MODULEPATH=/opt/apps/modulefiles/Linux:/opt/apps/modulefiles/Core:/opt/apps/lmod/lmod/modulefiles/Core
MOABHOMEDIR=/opt/moab
LMOD_CMD=/opt/apps/lmod/lmod/libexec/lmod
HISTCONTROL=ignoredups
SHLVL=1
HOME=/home/wsuNID
BASH_ENV=/opt/apps/lmod/lmod/init/bash
LMOD_arch=x86_64
LOGNAME=wsuNID
SSH_CONNECTION=67.185.209.100 52762 134.121.192.227 22
MODULESHOME=/opt/apps/lmod/lmod
LESSOPEN=||/usr/bin/lesspipe.sh %s
LMOD_FULL_SETTARG_SUPPORT=no
XDG_RUNTIME_DIR=/run/user/1005
LMOD_DIR=/opt/apps/lmod/lmod/libexec
LMOD_COLORIZE=yes
-bash-4.2$ 

This list of variables can easily be searched by “piping” (|) its output to grep

-bash-4.2$ env | grep PATH
MANPATH=/opt/apps/lmod/lmod/share/man::
MODULEPATH_ROOT=/opt/apps/modulefiles
PATH=/usr/local/bin:/usr/local/sbin:/opt/moab/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/ibutils/bin:/opt/dell/srvadmin/bin
MODULEPATH=/opt/apps/modulefiles/Linux:/opt/apps/modulefiles/Core:/opt/apps/lmod/lmod/modulefiles/Core
-bash-4.2$

To use the information stored in the environment variable, we invoke it with the prefix $.

-bash-4.2$ pwd
/home/wsuNID/exampleDirectory
-bash-4.2$ cd $HOME
-bash-4.2$ pwd
/home/wsuNID
-bash-4.2$

The value of a single variable can be seen by using the echo command

-bash-4.2$ echo $HOME
/home/wsuNID
-bash-4.2$

An the value environment variable can be changed (or a new variable created) by invoking export.

-bash-4.2$ export EXAMPLEDIR=$HOME/exampleDirectory/
-bash-4.2$ echo $EXAMPLEDIR
/home/wsuNID/exampleDirectory
-bash-4.2$

The PATH variable

The PATH environment variable establishes a search path for executables. That is, when you type a command, the OS searches for that executable starting with the first directory listed in the PATH variable and moving along until it finds a match. Note that order matters, if multiple instances of an executable exist in the path, the one contained in the directory listed first will be used.

-bash-4.2$ echo $PATH
/usr/local/bin:/usr/local/sbin:/opt/moab/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/ibutils/bin:/opt/dell/srvadmin/bin
-bash-4.2$

bash_profile and bashrc

We can control the behavior of bash at login by editing the hidden configuration files in our home directory.

-bash-4.2$ ls -al .bash*
-rw------- 1 wsuNID  myGroup 1259 Aug 18 22:34 .bash_history
-rw-r--r-- 1 wsuNID  myGroup   18 Jul 18  2013 .bash_logout
-rw-r--r-- 1 wsuNID  myGroup  176 Jul 18  2013 .bash_profile
-rw-r--r-- 1 wsuNID  myGroup  124 Sep 30  2014 .bashrc
-bash-4.2$ 

The files we wish to concentrate on are “.bash_profile” and “.bashrc”. Let’s examine their contents

-bash-4.2$ cat .bash_profile 
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

-bash-4.2$ cat .bashrc 
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

# User specific aliases and functions
-bash-4.2$

You can see that the two are nearly identical. The only difference being that our .bash_profile modifies the PATH environment variable.

There is much confusion over the role that each of these files play. When you login to a system, the .bash_profile file is executed UPON LOGIN. The .bashrc file modifies the default environment when connected without login, for example when executing commands remotely using SSH.

The environment defined in .bashrc is available to login and (remote) ssh command invocations. The .bashrc environment is also established for SLURM jobs.
The .bash_profile extends the environment inherited from the .bashrc definition specifically for a login session.

To update the environment immediately after editing the file (without having to login again) use the command:

source .bashrc

and/or

source .bash_profile

SSH config (Linux and Mac)

It can be cumbersome to type your username and host address every time you login to a machine. The .ssh/config file offers the ability to create aliases to speed up this process. Add the following to your .ssh/config file to create a shortcut

  Host kamiak
  Hostname kamiak.wsu.edu
  User <your user ID on kamiak>

Now from your local machine you can simply type ssh kamiak and the username and hostname will be set automatically.