UC Irvine, Information and Computer Science Department Winter 2000

ICS 54: Interacting with Shells: Brief notes for Chapters 8, 9, 20.


Shells

A shell is just another UNIX program/utility/command/tool, whose capabilities/responsibilities include:
  1. Providing a command line interface,
  2. Doing filename expansion/substitution,
  3. Doing variable (name) substitution/expansion,
  4. Doing I/O redirection, and
  5. Acting as a scripting (interpreted programming) language.
Focusing on the first 4 of these, we'll use the C shell, csh, and the "enhanced, but compatible" variant tcsh as our base reference because of their present extensive use on the principal student systems at UCI.

Critical files for csh and tcsh

Other shells: sh, ksh, bash


Shells are Programs

% set prompt="ics54<%\!> "
ics54<100> echo $prompt
ics54<%!>
ics54<101> ps -p $$
   PID TTY      TIME CMD
   487 pts/2    0:47 tcsh
ics54<102> csh
ics54<%51> ps -p $$
   PID TTY      TIME CMD
 21689 pts/2    0:00 csh
ics54<%52> sh 
$ ps -p $$
   PID TTY      TIME CMD
 21696 pts/2    0:00 sh
$ bash
bash$ ps -p $$
   PID TTY      TIME CMD
 21698 pts/2    0:00 bash
bash$ ksh
$ ps -p $$
   PID TTY      TIME CMD
 21700 pts/2    0:00 ksh
$ exit
bash$ exit
exit
$ exit
ics54<%53> exit
ics54<%54> ics54<103> 


Filename Expansion/Substitution

~
My home (login) directory
~user
user's home (login) directory
?
Match any single character in the name of a file
*
Match any number of characters in the name of a file
[...]
Match any character from the set [...] in the name of a file.
For example: ls c?[3-6]* would list ch4 cw4x ct5ab (if they existed), but not c3.
{str1,str2,...,strn}
Match any of the strings {str1, str2, ..., str3}
For example:   ls c.{htm,txt} lists c.htm c.txt
set nomatch
Prevents generation of error message if there is no match.
set noglob
Turns off filename expansion
unset noglob
Turns on filename expansion
Filename expansion is also limited by the same mechanisms governing shell variable expansion/substitution:  "  '  \


Variables and Variable Expansion/Substitution

For the C shell (and its immediate relatives)
set somename
Defines the variable somename to have a null value.
set somename=somev
Defines the variable somename to have the value somev
unset somename
Undefine the variable somename
$somename
Replace by value of the variable somename:  ls $HOME
A further example:
% echo somename
somename
% echo $somename
somename: Undefined variable.
% set somename
% echo $somename
% set somename="sam"
% echo $somename
sam
% unset somename
% echo $somename
somename: Undefined variable.
\
Turn off any special meaning for next character.
For example:  ls \$HOME
Often used at the end of a line when you don't want the line to end.
'
Don't interpret the enclosed text except for  !. For example:  ls '$HOME'
"
Don't interpret enclosed the text except for   \    '    $    !
`
Evaluate what is inside the back-quotes
% set prompt="`hostname`% "
octavian.ics.uci.edu%


Environment Variables

(Normal) shell variables apply only to the shell in which they are defined.

Environment Variables are exported/available-to any shells that are created/spawned by the shell in which they are defined.

In other words, a child shell inherits its parent shell's environment variables, but not its (normal/regular) shell variables.

setenv somename
Defines the environment variable somename to have a null value.
setenv somename    somev
Defines the environment variable somename to have the value somev
unsetenv somename
Undefine the environment variable somename
An example
% set a=apple
% setenv b banana
% csh
% echo $a
a: Undefined variable
% echo $b
banana
% set c=cat
% setenv d dog
% exit
%  % echo $a
apple
% echo $b
banana
% echo $c
c: Undefined variable.
% echo $d
d: Undefined variable


Some Important Shell Variables

HOME    USER    GROUP    HOST    TERM    TZ   
These listed above are Actually Environment Variables
$
The PID (processes ID, a number) of the currently running shell
path
Where to look for executable programs.
With C shell, use  rehash  after adding a program to one of the directories (other than ".") in the search path.
But, anyway, because of security, is it a bad idea to include "." in the search path.
prompt
How does the shell indicate it is ready for another command.
noclobber
Don't overwrite existing files when redirecting output.
ignoreeof
Don't exit on CTRL-D (^D)
history and savehist
How many previous commands to remember during this invocation of the shell and to save for the next invocation.
noglob
Do not expand filenames
filec
Enable filename completion.
cwd   home   shell   term  
Current Working Directory, Home Directory, Shell, Terminal
group   gid   user   uid  
Group Name and ID, User Name and ID.
setenv   shows all (set) environment variables

set   shows all (set) variables


I/O Redirection

>
Redirect standard output:  ls > listing
>!
Redirect standard output, "clobbering" if need be:  ls >! listing
>>
Redirect standard output, appending it:  ls xx >> listing
>&
Redirect standard output and error output:  ls xx >& listing
<
Redirect standard input
<<
Take STDIN from what follows. For example:
% cat << EOF > Data
This
is it.
EOF
% cat Data
This
is it.
%
|
Pipe (redirect) one process's output process to be another's input:
 ls -lR | more
;
Multiple commands on a single line
% pwd; date
/home/ics54/public_html/doc
Mon Jan 10 15:29:37 PST 2000
-
Often (but not always) used before command options or
(just often enough to be useful) to mean STDIN ("standard input").


Job Control

One can do work "in parallel," having more than than one job running at the same time, by using job control.
    % gcc big_program.c >& output &
    [1] 2014
    % jobs
    [1]  + Running    gcc big_program.c > & output
    % vi foo.c
    ^Z
    Stopped
    % jobs
    [1]  - Running    gcc big_program.c > & output
    [2]  + Stopped    vi foo.c

Various ways to refer to a job:

Note that all begin with %
We'll use %jobID to indicate any of them.

Some useful job control commands:


History

history [ -rh ] [ n ]
display last n commands:   r = in reverse order,   h = without numbers.
!!
previous command
!n
command numbered n
!-3
3rd most recent command
!strt
command beginning strt
!?strn
command containing strn
!n:$
last argument of command numbered n
!n:*
all arguments of command numbered n
!n:^
first argument of command numbered n
!$
last argument of previous command
!*
all arguments of previous command
!^
first argument of previous command
^str1^str2^
Redo last command with str1 replaced by str2
% ls ch01.html
ch01.html
% ^1^2^
ls ch02.html
ch02.html
%
tcsh has cursor-based command line editing:
Use up (and down) arrow to get to earlier commands.
Use left and right arrow to move within command.
Backspace and Delete keys remove characters.
Character keys insert characters.


Aliases

Aliases are abbreviations for useful commands.
alias name string
Define name to be an abbreviation for string
alias j jobs
alias rm "rm -i"
alias - "fg %-"
alias a alias
a pd pushd
alias
Lists all aliases
unalias name
Remove the alias definiton for name
\!*
All the arguments to the alias. For example,
alias rm '/bin/mv \!* ~/.RM'
\!:i
The ith argument to the alias. For example,
alias cd 'cd \!:1; set prompt = "\!:1% "'
\!$
The last argument to the alias
\aliasedCommand
Reverts to the original meaning of aliasedCommand.
Thus, \rm   really does give you   /usr/bin/rm

For C shell and tcsh,
alias definitions that are put in .cshrc
are available for use every time the shell starts.

(Other shells use other files at startup.)


Processes

ps does for processes what ls does for files.
% ps -H
ps: illegal option -- H
usage: ps [ -aAdeflcjLPy ] [ -o format ] [ -t termlist ]
        [ -u userlist ] [ -U userlist ] [ -G grouplist ]
        [ -p proclist ] [ -g pgrplist ] [ -s sidlist ]
  'format' is one or more of:
        user ruser group rgroup uid ruid gid rgid pid ppid pgid sid
        pri opri pcpu pmem vsz rss osz nice class time etime stime
        f s c lwp nlwp psr tty addr wchan fname comm args
% ps
   PID TTY      TIME CMD
 17020 pts/3    0:01 tcsh
% ps -f
     UID   PID  PPID  C    STIME TTY      TIME CMD
   ics54 17020 17018  0 14:03:26 pts/3    0:01 -tcsh
% ps -u$USER | more
   PID TTY      TIME CMD
 17322 pts/5    0:00 more
 17212 pts/5    0:01 tcsh
 17020 pts/3    0:02 tcsh
 17332 pts/3    0:00 more
 17330 pts/5    0:00 vi
 17256 pts/5    0:01 vi
% ps -f -u$USER | more
     UID   PID  PPID  C    STIME TTY      TIME CMD
   ics54 17322 17212  0 14:41:18 pts/5    0:00 more
   ics54 17212 17210  0 14:34:16 pts/5    0:01 -tcsh
   ics54 17020 17018  0 14:03:26 pts/3    0:02 -tcsh
   ics54 17325 17212  0 14:42:14 pts/5    0:00 vi s1.txt s2.txt
   ics54 17327 17020  0 14:42:19 pts/3    0:00 more
   ics54 17256 17212  0 14:35:58 pts/5    0:01 vi ../c20.html
kill  [ -sig ] [ pid ] [ %jobID ]
Sends a signal sig to the process pid or the job %jobID
kill -l
Lists the signals. Most often used are HUP (1) and KILL (9)

Comments are welcome.
Current as of 24 January 2000
HTML 4.01 Checked.