The shell stores two basic types of data in the environment

  • environment variables
  • shell variables

In addition to variables, the shell stores some programmatic data, namely, aliases and shell functions.

Examining the Environment

printenv Environment Variables

Print all or part of environment
printenv | less
printenv USER

set Environment Variables and Shell Variables and Shell Functions

Set or unset values of shell options and positional parameters. Change the value of shell attributes and positional parameters, or display the names and values of shell variables.
set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]

echo Show Specified Variables

  1. $ echo $USER $HOME
  2. ronnie /home/ronnie

alias Show or Set alias

$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alhF'
alias ls='ls --color=auto'

Some Common Variables

Variable Contents
DISPLAY The name of your display if you are running a graphical environment. Usually this is :0, meaning the first display generated by the X server.
EDITOR The name of the program to be used for text editing.
SHELL The name of your shell program.
HOME The pathname of your home directory.
LANG Defines the character set and collation order of your language.
OLDPWD The previous working directory.
PAGER The name of the program to be used for paging output. This is often set to /usr/bin/less.
PATH A colon-separated list of directories that are searched when you enter the name of a executable program.
PS1 Stands for “prompt string 1.” This defines the contents of the shell prompt. As we will later see, this can be extensively customized.
PWD The current working directory.
TERM The name of your terminal type. Unix-like systems support many terminal protocols; this variable sets the protocol to be used with your terminal emulator.
TZ Specifies your time zone. Most Unix-like systems maintain the computer’s internal clock in Coordinated Universal Time (UTC) and then display the local time by applying an offset specified by this variable.
USER Your username.

startup files

For_ _A login shell session

A login shell session is one in which we are prompted for username and password. This happens when we start a virtual console session, for example.

Startup Files for Login Shell Sessions
File Contents
/etc/profile A global configuration script that applies to all users.
~/.bash_profile A user’s personal startup file. It can be used to extend or override settings in the global configuration script.
~/.bash_login If ~/.bash_profile is not found, bash attempts to read this script.
~/.profile if neither ~/.bash_profile nor ~/.bash_login is found, bash attempts to read this file. This is the default in Debian-based distributions, such as
Ubuntu
.

For A non-login shell session

A non-login shell session typically occurs when we launch a terminal session in the GUI.

Startup Files for Non-Login Shell Sessions
File Contents
/etc/bash.bashrc A global configuration script that applies to all users.
~/.bashrc A user’s personal startup file. It can be used to extend or override
settings in the global configuration script.

The ~/.bashrc file is probably the most important startup file from the ordinary user’s point of view, because it is almost always read. Non-login shells read it by default, and most startup files for login shells are written in such a way as to read the ~/.bashrc file as well.

export

export PATH

  • The export command tells the shell to make the contents of PATH available to child processes of this shell.

    source

    source .bashrc

  • Read and execute commands from FILENAME in the current shell.

    Changing Environment Variables Temporarily

    The shell allows one or more variable assignments to take place immediately before a command.
    These assignments alter the environment for the command that follows.
    The effect of the assignment is temporary, changing the environment only for the duration of the command.

    $ file_info=$(grep "^$USER:" /etc/passwd)
    $ IFS=":" read user pw uid gid name home shell <<< "$file_info"