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:
Providing a command line interface,
Doing filename expansion/substitution,
Doing variable (name) substitution/expansion,
Doing I/O redirection, and
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
When it starts, shell does
source ~/.cshrc
If this is a login, the shell sources/etc/.login ~/.cshrc ~/.login in that order.
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
HOMEUSERGROUPHOSTTERMTZ
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.
cwdhomeshellterm
Current Working Directory, Home Directory, Shell, Terminal
groupgiduseruid
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:
% = the current job
%n = job number n
%strn = job whose command matches strn
%+ = most recently suspended job
%- = next most recently suspended job
Note that all begin with % We'll use %jobID to indicate any of them.
Some useful job control commands:
% cmd & = run cmd in the background
% kill %1 = kills job #1
% fg %vi = brings job #2 to the foreground
% bg = continues current job in the background
% %jobID = same as fg %jobID
^Z = suspend the current (foreground) job
^C = kill the current (foreground) job
% kill -9 %jobID = kill job %jobID
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.