Lesson 10: UNIX Shell Environment
- Objectives
- Be able to set, view and unset shell variables
- Describe the difference between the set and env commands
- Explain the importance of the export command.
- Describe three actions that are handled by the .bash_profile file
- Define user-defined aliases
- Explain the . (dot) command and the exec command.
- Shell Variables
- Shell variables are names consisting of alpha-numeric characters.
- Variables defined by the Operating System are uppercase, e.g. TERM, PS1, PATH
- The set command will display the shell’s current variables and their values.
- Shell variables are initialized using the assignment operator: TERM=vt100
Note: Quotes must be used for white space: VALUE="any value"
- Variables may be viewed using the echo command: echo $TERM
The $ in front of a variable name denotes the value of that variable.
- To remove the value from a variable, use the unset command: unset PS1
- Shell variables hold their values for the duration of the session i.e. until the shell is exited
- The Shell Environment

When a shell forks a child, not all of the variables get passed on to the child.
Only those variables that are in the shell’s environment are passed on to the child.
To see the variables that are in the shell’s environment with the command: env
To add a shell variable to the environment, use the export command: export PS1
- .bash_profile
The .bash_profile is a shell script that sets up a user's shell environment.
This script is executed each time the user logs in. The .bash_profile is used for initializing
shell variables, running the user's .bashrc file, running basic commands like umask and
set -o options.
- .bashrc
The .bashrc is a shell script that is executed during user login and whenever a new shell
is invoked. This file usually contains the user defined aliases. e.g.
alias bye="clear;exit"
alias rm="rm -i"
- The . and exec Commands
In normal execution of a unix command, shell-script or binary, the child process in unable
to affect the login shell environment. Sometimes it is desireable to run a shell script that
will initialize or change shell variables in the parent environment. To do this, the shell
(bash) provides a . (dot) or source command, which instructs the shell to
execute the shell script itself, without spawning a child process to run the script.
. shscript or
source shscript
In this example, the commands in the file shscript are run by the parent shell, and
therefore, any changes made to the environment will last for the duration of the login session.
If a UNIX command is run using the exec command, the shell will terminate upon the exiting
of that command:
exec clear
This will have the effect of clearing the screen and logging off the computer.
- UNIX Printing
Users have access to three print commands for viewiing the status of the print system,
for printing a file to a particular printer, and for viewing and cancelling their print jobs.
- lpstat - With the -t option, shows the state of the current print system.
- lp or lpr - sends the specified file to the default print queue as a printjob.
- cancel - cancels the printjob specified by the command-line argument(s)