System Programming
UNIX Shell Environments
Chapter Seven Unix Shell Environments 1
Shell Characteristics
n Command-line interface between the
user and the operating system
n Automatically starts on login, wait for
user to type in commands
n Both a command interpreter and a
programming language
n Shell script is a text file containing
logic for shell interpretation
Chapter Seven Unix Shell Environments 2
Shell Interactivity
n Command line parsing
n Environment
n Textual completion
n Aliases
n Command line editing
n Command history
n Configuration
Chapter Seven Unix Shell Environments 3
Shell Programming
n Variables
n Control structures
n Loops and conditionals
n Function definition and invocation
n Shell scripts
n Next chapter
Chapter Seven Unix Shell Environments 4
Various Unix Shells
n sh (Bourne shell, original Unix shell)
n ksh (Korn shell)
n csh (C shell, developed at Berkeley)
n tcsh
n bash (Bourne again SHell)
n Default user shell in Linux
n …
n Differences mostly in level of interactivity
support and scripting details
n [Link]
differences/
Chapter Seven Unix Shell Environments 5
Shell Features
Chapter Seven Unix Shell Environments 6
Bourne Again SHell (bash)
n Bash is the standard shell for this
lecture
n Superset of the Bourne shell (sh)
n Borrows features from sh, csh, tcsh &
ksh
n Part of the GNU project
Chapter Seven Unix Shell Environments 7
Variables
n Three main types of variables for a
running shell
n Local variables
n Present within current instance of shell
n Set at command prompt
n Environment variables
n Available to any child process of shell
n Shell variables
n Set and required by shell
Chapter Seven Unix Shell Environments 8
Shell Variables
n A set of variables the shell uses for certain
operations
n Shell variables include
n local variables
n environment variables
n Current list of environment variables can
be displayed with the env command
n Variables have a name and a value
n Send value of varname to standard output
with echo $varname
Chapter Seven Unix Shell Environments 9
Environment Variables
n Some interesting variables: HOME, PWD,
PATH, PS1, USER, HOSTNAME
$HOME: home directory (default argument for cd)
Example: /home/0607/student
$PWD: current working directory
Example: /export/home/staff/usern
$PATH: search path for commands
Example: /usr/local/bin:/bin:/usr/bin
$PS1: command prompt (default “$ ”)
Example: \u@\h:\w\$
$USER: user name
Example: usern
$HOSTNAME: computer hostname
Example: ktuce
Chapter Seven Unix Shell Environments 10
Environment Variables
n Other interesting variables: UID, PPID,
RANDOM, HISTFILE, HISTSIZE, MAIL,
MAILCHECK, PS2
$UID: current user ID
$PPID: process ID of program that invoked the shell
$RANDOM: generate a random integer between 0-32767
$HISTFILE: file for storing command history
$HISTSIZE: the number of commands to be stored
$MAIL: file checked by shell for the arrival of mail
$MAILCHECK: the number of seconds between checks
$PS2: secondary command prompt (default “> ”)
Chapter Seven Unix Shell Environments 11
Setting Variables
n Set variable with varname=value
n PS1=$USER@$HOSTNAME:
Change default shell prompt
n PS1="bash_prompt> "
n PATH=$PATH:$HOME/bin
n Append :$HOME/bin to PATH
n PATH=$PATH:~:.
n Append :~:. to PATH
n DATE=`date` or DATE=$(date)
n Capture output from date command
Chapter Seven Unix Shell Environments 12
Textual Completion
n <tab> attempts to complete the
current command or filename
n pus<tab> expands to pushd<space>
n pu<tab> gives the alternatives
n pu pup pushd
n In /etc, entering ls init<tab> gives
init init.d initpipe inittab
[lecture]$ ls init
Chapter Seven Unix Shell Environments 13
Aliases
n Aliases are used as shorthand for
frequently-used commands
n Syntax: alias shortcut=command
n Examples:
n alias pu=pushd
n alias po=popd
n alias l= "ls –F -C "
n alias ll="ls –L –l -F"
n alias d=dirs
n alias hide="chmod og-rwx"
n alias unhide="chmod og+r"
Chapter Seven Unix Shell Environments 14
Command History
n Use history command to list
previously entered commands
n Use fc -l <m> <n> to list
previously typed commands from m
through n
n Use up and down cursor keys to scroll
through history list
Chapter Seven Unix Shell Environments 15
Editing on the Command Line
n bash provides a number of line editing commands
n Default emacs-mode commands
n Esc-b Move back one word
n Esc-f Move forward one word
n Ctrl-a Move to beginning of line
n Ctrl-e Move to end of line
n Ctrl-k Kill text from cursor to end of line
n On the other hand, you can interactively edit the
command line in several ways if using ksh
n set -o vi allows you to use vi commands to
edit the command line
n set -o vi-tabcomplete also lets you
complete commands/ filenames by entering a TAB
Chapter Seven Unix Shell Environments 16
Login Scripts
n You don’t want to enter aliases, set
environment variables, set up
command line editing, etc. each time
you log in
n All of these things can be done in a
script that is run each time the shell is
started
Chapter Seven Unix Shell Environments 17
Login Scripts (cont.)
n Startup scripts executed at login
n /etc/profile
n ~/.bash_profile
n ~/.bash_login (if no .bash_profile)
n ~/.profile (if neither are present)
n Script executed after login
n ~/.bashrc
n Script executed upon logout
n ~/.bash_logout
Chapter Seven Unix Shell Environments 18
Example .bash_ profile (partial)
# .bash_ profile: executed by bash for login shells
umask 022 (0666 & ~022 = 0644 = rw-r--r--)
# include .bashrc if it exists
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# Set variables for a warm fuzzy environment
export CVSROOT=~/.cvsroot
export EDITOR=/bin/vi
export PAGER=/usr/bin/less
Chapter Seven Unix Shell Environments 19
Example .bashrc (partial)
# .bashrc
# abbreviations for some common commands
alias bye=logout
alias h=history
alias l='ls –F -C'
alias ll='ls-L –l -F'
alias po=popd
alias pu=pushd
Chapter Seven Unix Shell Environments 20
Login Scripts (cont.)
n For csh, login shells execute:
n ~/.profile
n If ENV is set:
n That file is executed for each new
terminal
n Example:
n ENV=$HOME/.cshrc
n EXPORT ENV (for bash)
Chapter Seven Unix Shell Environments 21
Login and Other Shells
/etc/profile login
~/.bash_profile shell
~/.bashrc
Interactive shell
~/.bashrc
Interactive shell Interactive shell
~/.bashrc ~/.bashrc
Chapter Seven Unix Shell Environments 22