UNIX®ShellsbyExample,ThirdEdition
ByEllieQuigley
Publisher :PrenticeHallPTR
PubDate :October01,2001
ISBN :0-13-066538-X
Pages :1040
FiveUNIXshells,threeessentialutilities,one
indispensableresource!
LearnUNIXshellprogrammingtheeasyway,
usinghands-onexamples
CoversallfiveleadingUNIXshells-C,Bourne,
Korn,bash,andtcsh
Bybest-sellingauthorEllieQuigley,Silicon
Valley'stopUNIXinstructor
Thebest-sellingUNIXShellsbyExamplecontinues
tobetheonlybookyouneedtolearnUNIXshell
programming.UNIXShellsbyExample,Third
Editionaddsthoroughcoverageofthenewbashand
tcshshellstothefullexplanationsinQuigley's
famoustreatmentoftheC,Bourne,andKornshells
andtheawk,sed,andgreputilities,makingthisthe
mostcompleteUNIXshellprogrammingbook
Tableof availableanywhere.Usingproventechniquesdrawn
•
Contents fromheracclaimedSiliconValleyUNIXclasses,
• Examples Quigleytransformsyouintoanexpert-levelshell
programmer.You'lllearnwhattheshellsare,what
theydo,andhowtoprogramthem,aswellashow
andwhentouseawk,sed,andgrep.Code
examples,completelyrevisedandclassroom-tested
forthisedition,explainconceptsfirst-handandcan
serveasthebasisforyourownprojects.
ExplainstheC,Bourne,Korn,bash,andtcshshells
inonecohesiveway-you'llunderstandwhichshellto
useandwhyDetailstheessentialawk,sed,and
grepprogrammingutilitiesOffersproventeaching
methodsfromatopUNIXshellinstructorProvides
sourcecodeanddatafilesforallexamplesonthe
CD-ROM,soyoucanexperimentwiththemonyour
ownsystemUNIXsystemadministrators,application
developers,andpoweruserswillturntothisbook
againandagain,bothasavitalclassroomlearning
toolandasafavoritereferencemanual.
777
Copyright
Preface
ACKNOWLEDGMENTS
Chapter1.IntroductiontoUNIXShells
Section1.1.DefinitionandFunction
Section1.2.SystemStartupandtheLoginShell
Section1.3.ProcessesandtheShell
Section1.4.TheEnvironmentandInheritance
Section1.5.ExecutingCommandsfromScripts
Chapter2.TheUNIXToolbox
Section2.1.RegularExpressions
Section2.2.CombiningRegularExpressionMetacharacters
Chapter3.ThegrepFamily
Section3.1.ThegrepCommand
Section3.2.grepExampleswithRegularExpressions
Section3.3.grepwithPipes
Section3.4.grepwithOptions
Section3.5.egrep(Extendedgrep)
Section3.6.FixedgreporFastgrep
UNIXTOOLSLABEXERCISE
Chapter4.sed,theStreamlinedEditor
Section4.1.WhatIssed?
Section4.2.HowDoessedWork?
Section4.3.Addressing
Section4.4.CommandsandOptions
Section4.5.ErrorMessagesandExitStatus
Section4.6.sedExamples
Section4.7.sedScripting
UNIXTOOLSLABEXERCISE
Chapter5.TheawkUtility:awkasaUNIXTool
Section5.1.WhatIsawk?
Section5.2.awk'sFormat
Section5.3.FormattingOutput
Section5.4.awkCommandsfromWithinaFile
Section5.5.RecordsandFields
Section5.6.PatternsandActions
Section5.7.RegularExpressions
Section5.8.awkCommandsinaScriptFile
Section5.9.Review
UNIXTOOLSLABEXERCISE
Chapter6.TheawkUtility:awkProgrammingConstructs
Section6.1.ComparisonExpressions
Section6.2.Review
UNIXTOOLSLABEXERCISE
Chapter7.TheawkUtility:awkProgramming
Section7.1.Variables
Section7.2.RedirectionandPipes
Section7.3.Pipes
Section7.4.ClosingFilesandPipes
Section7.5.Review
UNIXTOOLSLABEXERCISE
Section7.6.ConditionalStatements
Section7.7.Loops
Section7.8.ProgramControlStatements
Section7.9.Arrays
Section7.10.awkBuilt-InFunctions
Section7.11.Built-InArithmeticFunctions
Section7.12.User-DefinedFunctions(nawk)
Section7.13.Review
UNIXTOOLSLABEXERCISE
Section7.14.OddsandEnds
Section7.15.Review
UNIXTOOLSLABEXERCISE
Chapter8.TheInteractiveBourneShell
Section8.1.Startup
Section8.2.ProgrammingwiththeBourneShell
BOURNESHELLLABEXERCISES
Chapter9.TheCShell
Section9.1.TheInteractiveCShell
Section9.2.ProgrammingwiththeCShell
CSHELLLABEXERCISES
Chapter10.TheKornShell
Section10.1.InteractiveKornShell
Section10.2.ProgrammingwiththeKornShell
KORNSHELLLABEXERCISES
Chapter11.TheInteractivebashShell
Section11.1.Introduction
Section11.2.CommandLineShortcuts
Section11.3.Variables
BASHSHELLLABEXERCISES
Chapter12.ProgrammingwiththebashShell
Section12.1.Introduction
Section12.2.ReadingUserInput
Section12.3.Arithmetic
Section12.4.PositionalParametersandCommandLineArguments
Section12.5.ConditionalConstructsandFlowControl
Section12.6.LoopingCommands
Section12.7.Functions
Section12.8.TrappingSignals
Section12.9.Debugging
Section12.10.ProcessingCommandLineOptionswithgetopts
Section12.11.TheevalCommandandParsingtheCommandLine
Section12.12.bashOptions
Section12.13.ShellBuilt-InCommands
BASHSHELLLABEXERCISES
Chapter13.TheInteractiveTCShell
Section13.1.Introduction
Section13.2.TheTCShellEnvironment
Section13.3.CommandLineShortcuts
Section13.4.JobControl
Section13.5.Metacharacters
Section13.6.RedirectionandPipes
Section13.7.Variables
Section13.8.Arrays
Section13.9.SpecialVariablesandModifiers
Section13.10.CommandSubstitution
Section13.11.Quoting
Section13.12.Built-InCommands
TCSHELLLABEXERCISES
AppendixA.UsefulUNIXUtilitiesforShellProgrammers
at—at,batch—executecommandsatalatertime
awk—patternscanningandprocessinglanguage
banner—makeposters
basename—withadirectorynamedeliversportionsofthepathname
bc—processesprecisionarithmetic
bdiff—comparestwobigfiles
cal—displaysacalendar
cat—concatenatesanddisplaysfiles
chmod—changethepermissionsmodeofafile
chown—changesowneroffile
clear—clearstheterminalscreen
cmp—comparestwofiles
compress—compress,uncompress,zcatcompress,uncompressfiles,ordisplay
expandedfiles
cp—copiesfiles
cpio—copyfilearchivesinandout
cron—theclockdaemon
crypt—encodesordecodesafile
cut—removesselectedfieldsorcharactersfromeachlineofafile
date—displaysthedateandtimeorsetsthedate
diff—comparestwofilesfordifferencesdiff[–bitw][–c|–Cn
du—summarizesdiskusage
echo—echoesarguments
egrep—searchesafileforapatternusingfullregularexpressions
expr—evaluatesargumentsasanexpression
fgrep—searchafileforacharacterstring
file—determinesthetypeofafilebylookingatitscontents
find—findsfiles
finger—displaysinformationaboutlocalandremoteusers
fmt—simpletextformatters
fold—foldslonglines
ftp—filetransferprogram
getopt(s)—parsescommandlineoptions
grep—searchesafileforapattern
groups—printsgroupmembershipofuser
id—printstheusername,userID,groupnameandgroupID
jsh—thestandard,jobcontrolshell
line—readsoneline
logname—getsthenameoftheuserrunningtheprocess
lp—sendsoutputtoaprinter(AT&T)
lpr—sendsoutputtoaprinter(UCB)
lpstat—printinformationaboutthestatusoftheLPprintservice(AT&T)
lpq—printinformationaboutthestatusoftheprinter(UCB)
ls—listscontentsofdirectory
mail—mail,rmail—readmailorsendmailtousers
mailx—interactivemessageprocessingsystem
make—maintains,updates,andregeneratesgroupsofrelatedprogramsandfiles
mesg—permitsordeniesmessagesresultingfromthewritecommand
mkdir—createsadirectory
more—browseorpagethroughatextfile
mv—moveorrenamefiles
nawk—patternscanningandprocessinglanguage
newgrp—logintoanewgroup
news—printsnewsitems
nice—runsacommandatlowpriority
nohup—makescommandsimmunetohangupsandquits
od—octaldump
pack—pack,pcat,unpack—compressesandexpandsfiles
passwd—changestheloginpasswordandpasswordattributes
paste—mergessamelinesofseveralfilesorsubsequentlinesofonefile
pcat—(seepack)
pg—displaysfilesapageatatime
pr—printsfiles
ps—reportsprocessstatus
pwd—displaysthepresentworkingdirectoryname
rcp—remotefilecopy
rlogin—remotelogin
rm—removesfilesfromdirectories
rmdir—removesadirectory
rsh—startsaremoteshell
ruptime—showsthehoststatusoflocalmachines
rwho—whoisloggedinonlocalmachines
script—createsatypescriptofaterminalsession
sed—streamlinededitor
size—printssectionsizesinbytesofobjectfiles
sleep—suspendsexecutionforsomenumberofseconds
sort—sortand/ormergefiles
spell—findsspellingerrors
split—splitsafileintopieces
strings—findsanyprintablestringsinanobjectorbinaryfile
stty—setstheoptionsforaterminal
su—becomesuperuseroranotheruser
sum—calculatesachecksumforafile
sync—updatesthesuperblockandsendschangedblockstodisk
tabs—settabstopsonaterminal
tail—displaysthetailendofafile.
talk—allowsyoutotalktoanotheruser
tar—storesandretrievesfilesfromanarchivefile,normallyatapedevice
tee—replicatesthestandardoutput
telnet—communicateswitharemotehost
test—evaluatesanexpression
time—displaysasummaryoftimeusedbythisshellanditschildren
timex—timesacommand;reportsprocessdataandsystemactivity
touch—updatesaccesstimeand/ormodificationtimeofafile
tput—initializesaterminalorqueriestheterminfodatabase
tr—translatescharacters
true—providesuccessfulexitstatus
tsort—topologicalsort
tty—getsthenameoftheterminal
umask—setsfile-creationmodemaskforpermissions
uname—printsnameofcurrentmachine
uncompress—restoresfilestotheiroriginalstateaftertheyhavebeencompressed
usingthecompresscommand
uniq—reportsonduplicatelinesinafile
units—convertsquantitiesexpressedinstandardscalestootherscales
unpack—expandsfilescreatedbypack
uucp—copyfilestoanothersystem,UNIX-to-UNIXsystemcopy
uuencode—uuencode,uudecode—encodeabinaryfileintoASCIItextinordertosendit
throughe-mail,orconvertitbackintoitsoriginalform
wc—countslines,words,andcharacters
what—extractsSCCSversioninformationfromafilebyprintinginformationfoundafter
the@(#)pattern
which—locatesacommandanddisplaysitspathnameoralias(UCB)
whereis—locatesthebinary,source,andmanualpagefilesforacommand(UCB)
who—displayswhoisloggedonthesystem
write—writesamessagetoanotheruser
xargs—constructsanargumentlist(s)andexecutesacommand
zcat—uncompressacompressedfiletostandardoutput.Sameasuncompress–c
AppendixB.ComparisonoftheShells
SectionB.1.TheShellsCompared
SectionB.2.tcshversuscsh
SectionB.3.bashversussh
AppendixC.StepsforUsingQuotingCorrectly
SectionC.1.Backslash
SectionC.2.SingleQuotes
SectionC.3.DoubleQuotes
SectionC.4.CombiningQuotes
SectionC.5.SettingtheShellVariable
Copyright
LibraryofCongressCataloging-in-Publication
Data
Quigley,Ellie.
UNIXshellsbyexample/EllieQuigley.--3rded.
p.cm.
ISBN0-13-066538-X
1.UNIX(Computerfile)2.UNIXShells.I.Title.
QA76.76.O63Q542001
005.4'32--dc21
2001050075
©2002byPrenticeHallPTR
Prentice-Hall,Inc.
UpperSaddleRiver,NJ07458
PrenticeHallbooksarewidelyusedbycorporationsandgovernment
agenciesfortraining,marketing,andresale.
Thepublisheroffersdiscountsonthisbookwhenorderedinbulk
quantities.Formoreinformation,contact:CorporateSalesDepartment,
Phone:800-382-3419;Fax:201-236-7141;E-mail:
;orwrite:PrenticeHallPTR,Corp.SalesDept.,
OneLakeStreet,UpperSaddleRiver,NJ07458.
Allproductsorservicesmentionedinthisbookarethetrademarksor
servicemarksoftheirrespectivecompaniesororganizations.
Allrightsreserved.Nopartofthisbookmaybereproduced,inanyform
orbyanymeans,withoutpermissioninwritingfromthepublisher.
PrintedintheUnitedStatesofAmerica
10987654321
Credits
Productioneditor/compositor:
VanessaMoore
Acquisitionseditor:
MarkTaub
Marketingmanager:
BryanGambrel
Manufacturingmanager:
MauraZaldivar
Editorialassistant:
SarahHand
Coverdesigndirector:
JerryVotta
Coverdesigner:
AnthonyGemmellaro
Projectcoordinator:
AnneR.Garcia
PearsonEducationLtd.
PearsonEducationAustraliaPTY,Limited
PearsonEducationSingapore,Pte.Ltd
PearsonEducationNorthAsiaLtd.
PearsonEducationCanada,Ltd.
PearsonEducacióndeMexico,S.A.deC.V.
PearsonEducation—Japan
PearsonEducationMalaysia,Pte.Ltd.
PearsonEducation,UpperSaddleRiver,NewJersey
Dedication
Thisbookisdedicatedtomypapa,ArchibaldMacNicholMain,Jr.,
thebestfatherintheworld.
Preface
Playingthe"shell"gameisalotoffun.Thisbookwaswrittentomake
yourlearningexperiencebothfunandprofitable.Sincethefirstedition
waspublished,Ihaveheardfrommanyofyouwhohavebeenhelpedby
mybooktorealizethatshellprogrammingdoesn'tneedtobedifficultat
all!Learningbyexamplemakesiteasyandfun.Infact,duetosuch
positivefeedback,IhavebeenaskedbyPrenticeHalltoproducethis
new,updatedversiontoincludetwoadditionalandpopularshells,the
BashandTCshells.AlthoughoftenassociatedwithLinuxsystems,the
BashandTCshellsarefreelyavailabletoanyoneusingUNIXaswell.In
fact,todaymanyUNIXusersprefertheseshellstothetraditionalUNIX
shellsbecausetheyofferanenhancedandflexibleinteractive
environment,aswellasimprovedprogrammingcapabilities.
WritingUNIXShellsbyExampleistheculminationof19yearsof
teachinganddevelopingclassesforthevariousshellsandthoseUNIX
utilitiesmostheavilyusedbyshellprogrammers.ThecoursenotesI
developedforteachingclasseshavebeenusedbytheUniversityof
CaliforniaSantaCruzandUniversityofCaliforniaDavisUNIXprograms,
SunMicrosystemsEducation,AppleComputer,DeAnzaCollege,and
numerousvendorsthroughouttheworld.Dependingontherequirements
ofmyclient,Inormallyteachoneshellatatimeratherthanallofthem.
Toaccommodatetheneedsofsomanyclients,Idevelopedseparate
materialsforeachoftherespectiveUNIXshellsandtools.
WhetherIamteaching"Grep,Sed,andAwk,""BourneShellforthe
SystemAdministrator,"or"TheInteractiveKornShell,"onestudent
alwaysasks,"WhatbookcanIgetthatcoversalltheshellsandthe
importantutilitiessuchasgrep,sed,andawk?ShouldIgettheawk
book,orshouldIgetabookongrepandsed?Isthereonebookthat
reallycoversitall?Idon'twanttobuythreeorfourbooksinorderto
becomeashellprogrammer."
Inresponse,Icanrecommendanumberofexcellentbookscovering
thesetopicsseparately,andsomeUNIXbooksthatattempttodoitall,
butthestudentswantonebookwitheverythingandnotjustaquick
survey.TheywanttheUNIXtools,regularexpressions,allthreeshells,
quotingrules,acomparisonoftheshells,exercises,andsoforth,allin
onebook.Thisisthatbook.AsIwroteit,IthoughtabouthowIteachthe
classesandorganizedthechaptersinthesameformat.Intheshell
programmingclasses,thefirsttopicisalwaysanintroductiontowhatthe
shellisandhowitworks.ThenwetalkabouttheUNIXutilitiessuchas
grep,sed,andawk,themostimportanttoolsintheshellprogrammer's
toolbox.Whenlearningabouttheshell,itispresentedfirstasan
interactiveprogramwhereeverythingcanbeaccomplishedatthe
commandline,andthenasaprogramminglanguagewherethe
programmingconstructsaredescribedanddemonstratedinshellscripts.
(SincetheCandTCshellsarealmostidenticalasprogramming
languages,thereareseparatechaptersdescribinginteractiveuse,but
onlyonechapterdiscussingprogrammingconstructs.)Whenshell
programmingclassesareover,whethertheylasttwodaysoraweekor
evenasemester,thestudentsareproficientandexcitedaboutwriting
scripts.Theyhavelearnedhowtoplaytheshellgame.Thisbookwill
teachhowtoplaythesamegamewhetheryoutakeaclassorjustplay
byyourself.
Havingalwaysfoundthatsimpleexamplesareeasierforquick
comprehension,eachconceptiscapturedinasmallexamplefollowedby
theoutputandanexplanationofeachlineoftheprogram.Thismethod
hasproventobeverypopularwiththosewholearnedPerlprogramming
frommyfirstbook,PerlbyExample,andUNIXShellsbyExamplenow
hasbeenwell-receivedforthosewhoneededtowrite,read,andmaintain
shellprograms.
Thefiveshellsarepresentedinparallelsothatif,forexample,youwant
toknowhowredirectionisperformedinoneshell,thereisaparallel
discussionofthattopicineachoftheothershellchapters.Foraquick
comparisonchart,seeAppendixBofthisbook.
ItisanuisancetohavetogotoanotherbookortheUNIXmanpages
whenallyouwantisenoughinformationaboutaparticularcommandto
jogyourmemoryonhowthecommandworks.Tosaveyoutime,
AppendixAcontainsalistofusefulcommands,theirsyntaxand
definitions.Examplesandexplanationsareprovidedforthemorerobust
andoften-usedcommands.
ThecomparisonchartinAppendixBwillhelpyoukeepthedifferent
shellsstraight,especiallywhenyouportscriptsfromoneshelltoanother,
andserveasaquicksyntaxcheckwhenallyouneedisareminderof
howtheconstructworks.
Oneofthebiggesthurdlesforshellprogrammersisusingquotes
properly.ThesectiononquotingrulesinAppendixCpresentsastep-bystepprocessforsuccessfulquotinginsomeofthemostcomplex
commandlines.Thisprocedurehasdramaticallyreducedtheamountof
timeprogrammerswastewhendebuggingscriptswithfutileattemptsat
matchingquotesproperly.
Ithinkyou'llfindthisbookavaluabletutorialandreference.Theobjective
istoexplainthroughexampleandkeepthingssimplesothatyouhave
funlearningandsavetime.SincethebookreplicateswhatIsayinmy
classes,Iamconfidentthatyouwillbeaproductiveshellprogrammerin
ashortamountoftime.Everythingyouneedisrighthereatyour
fingertips.Playingtheshellgameisfun.You'llsee!
EllieQuigley()
ACKNOWLEDGMENTS
Iwouldliketothankandacknowledgethefollowingpeople,without
whosehelpthisbookwouldnothavebeenpublished:
MarkTaub,myacquisitionseditor,andVanessaMoore,myproduction
editoratPrenticeHall;BethGerra,RobertaHarvey,andGaryWilsonfor
reviewingtheoriginalmaterial;SteveHansenforhardwareandsoftware
support.Finally,IwouldliketothankallmystudentsatUCSantaCruz,
UCDavis,andSunMicrosystemsfortheirfeedback.
CONTENTS
Chapter1.IntroductiontoUNIXShells
1.1DefinitionandFunction
1.2SystemStartupandtheLoginShell
1.3ProcessesandtheShell
1.4TheEnvironmentandInheritance
1.5ExecutingCommandsfromScripts
1.1DefinitionandFunction
Theshellisaspecialprogramusedasaninterfacebetweentheuserand
theheartoftheUNIXoperatingsystem,aprogramcalledthekernel,as
showninFigure1.1.Thekernelisloadedintomemoryatboot-uptime
andmanagesthesystemuntilshutdown.Itcreatesandcontrols
processes,andmanagesmemory,filesystems,communications,andso
forth.Allotherprograms,includingshellprograms,resideoutonthedisk.
Thekernelloadsthoseprogramsintomemory,executesthem,and
cleansupthesystemwhentheyterminate.Theshellisautilityprogram
thatstartsupwhenyoulogon.Itallowsuserstointeractwiththekernel
byinterpretingcommandsthataretypedeitheratthecommandlineorin
ascriptfile.
Figure1.1.Thekernel,theshell,andyou.
Whenyoulogon,aninteractiveshellstartsupandpromptsyouforinput.
Afteryoutypeacommand,itistheresponsibilityoftheshellto(a)parse
thecommandline;(b)handlewildcards,redirection,pipes,andjob
control;and(c)searchforthecommand,andiffound,executethat
command.WhenyoufirstlearnUNIX,youspendmostofyourtime
executingcommandsfromtheprompt.Youusetheshellinteractively.
Ifyoutypethesamesetofcommandsonaregularbasis,youmaywant
toautomatethosetasks.Thiscanbedonebyputtingthecommandsina
file,calledascriptfile,andthenexecutingthefile.Ashellscriptismuch
likeabatchfile:ItisalistofUNIXcommandstypedintoafile,andthen
thefileisexecuted.Moresophisticatedscriptscontainprogramming
constructsformakingdecisions,looping,filetesting,andsoforth.Writing
scriptsnotonlyrequireslearningprogrammingconstructsand
techniques,butassumesthatyouhaveagoodunderstandingofUNIX
utilitiesandhowtheywork.Therearesomeutilities,suchasgrep,sed,
andawk,thatareextremelypowerfultoolsusedinscriptsforthe
manipulationofcommandoutputandfiles.Afteryouhavebecome
familiarwiththesetoolsandtheprogrammingconstructsforyour
particularshell,youwillbereadytostartwritingusefulscripts.When
executingcommandsfromwithinascript,youareusingtheshellasa
programminglanguage.
1.1.1TheThreeMajorUNIXShells
ThethreeprominentandsupportedshellsonmostUNIXsystemsarethe
Bourneshell(AT&Tshell),theCshell(Berkeleyshell),andtheKornshell
(supersetoftheBourneshell).Allthreeofthesebehaveprettymuchthe
samewaywhenrunninginteractively,buthavesomedifferencesin
syntaxandefficiencywhenusedasscriptinglanguages.
TheBourneshellisthestandardUNIXshell,andisusedtoadminister
thesystem.Mostofthesystemadministrationscripts,suchasthercstart
andstopscriptsandshutdownareBourneshellscripts,andwhenin
singleusermode,thisistheshellcommonlyusedbytheadministrator
whenrunningasroot.ThisshellwaswrittenatAT&Tandisknownfor
beingconcise,compact,andfast.ThedefaultBourneshellpromptisthe
dollarsign($).
TheCshellwasdevelopedatBerkeleyandaddedanumberoffeatures,
suchascommandlinehistory,aliasing,built-inarithmetic,filename
completion,andjobcontrol.TheCshellhasbeenfavoredoverthe
Bourneshellbyusersrunningtheshellinteractively,butadministrators
prefertheBourneshellforscripting,becauseBourneshellscriptsare
simplerandfasterthanthesamescriptswritteninCshell.ThedefaultC
shellpromptisthepercentsign(%).
TheKornshellisasupersetoftheBourneshellwrittenbyDavidKornat
AT&T.Anumberoffeatureswereaddedtothisshellaboveandbeyond
theenhancementsoftheCshell.Kornshellfeaturesincludeaneditable
history,aliases,functions,regularexpressionwildcards,built-in
arithmetic,jobcontrol,coprocessing,andspecialdebuggingfeatures.
TheBourneshellisalmostcompletelyupward-compatiblewiththeKorn
shell,soolderBourneshellprogramswillrunfineinthisshell.The
defaultKornshellpromptisthedollarsign($).
1.1.2TheLinuxShells
Althoughoftencalled"Linux"shells,BashandTCshellsarefreely
availableandcanbecompiledonanyUNIXsystem;infact,theshells
arenowbundledwithSolaris8andSun'sUNIXoperatingsystem.But
whenyouinstallLinux,youwillhaveaccesstotheGNUshellsandtools,
andnotthestandardUNIXshellsandtools.AlthoughLinuxsupportsa
numberofshells,theBourneAgainshell(bash)andtheTCshell(tcsh)
arebyfarthemostpopular.TheZshellisanotherLinuxshellthat
incorporatesanumberoffeaturesfromtheBourneAgainshell,theTC
shell,andtheKornshell.ThePublicDomainKornshell(pdksh)aKorn
shellclone,isalsoavailable,andforafeeyoucangetAT&T'sKornshell,
nottomentionahostofotherunknownsmallershells.
ToseewhatshellsareavailableunderyourversionofLinux,lookinthe
file,/etc/shell.
Tochangetooneoftheshellslistedin/etc/shell,typethechshcommand
andthenameoftheshell.Forexample,tochangepermanentlytotheTC
shell,usethechshcommand.Attheprompt,type:
chsh/bin/tcsh
1.1.3HistoryoftheShell
Thefirstsignificant,standardUNIXshellwasintroducedinV7(seventh
editionofAT&T)UNIXinlate1979,andwasnamedafteritscreator,
StephenBourne.TheBourneshellasaprogramminglanguageisbased
onalanguagecalledAlgol,andwasprimarilyusedtoautomatesystem
administrationtasks.Althoughpopularforitssimplicityandspeed,it
lacksmanyofthefeaturesforinteractiveuse,suchashistory,aliasing,
andjobcontrol.Enterbash,theBourneAgainshell,whichwas
developedbyBrianFoxoftheFreeSoftwareFoundationundertheGNU
copyrightlicenseandisthedefaultshellfortheverypopularLinux
operatingsystem.ItwasintendedtoconformtotheIEEEPOSIX
P1003.2/ISO9945.2ShellandToolsstandard.Bashalsooffersanumber
ofnewfeatures(bothattheinteractiveandprogramminglevel)missingin
theoriginalBourneshell(yetBourneshellscriptswillstillrun
unmodified).ItalsoincorporatesthemostusefulfeaturesofboththeC
shellandKornshell.It'sbig.TheimprovementsoverBourneshellare:
commandlinehistoryandediting,directorystacks,jobcontrol,functions,
aliases,arrays,integerarithmetic(inanybasefrom2to64),andKorn
shellfeatures,suchasextendedmetacharacters,selectloopsfor
creatingmenus,theletcommand,etc.
TheCshell,developedattheUniversityofCaliforniaatBerkeleyinthe
late1970s,wasreleasedaspartof2BSDUNIX.Theshell,written
primarilybyBillJoy,offeredanumberofadditionalfeaturesnotprovided
inthestandardBourneshell.TheCshellisbasedontheCprogramming
language,andwhenusedasaprogramminglanguage,itsharesasimilar
syntax.Italsooffersenhancementsforinteractiveuse,suchas
commandlinehistory,aliases,andjobcontrol.Becausetheshellwas
designedonalargemachineandanumberofadditionalfeatureswere
added,theCshellhasatendencytobeslowonsmallmachinesand
sluggishevenonlargemachineswhencomparedtotheBourneshell.
TheTCshellisanexpandedversionoftheCshell.Someofthenew
featuresare:commandlineediting(emacsandvi),scrollingthehistory
list,advancedfilename,variable,andcommandcompletion,spelling
correction,schedulingjobs,automaticlockingandlogout,timestampsin
thehistorylist,etc.It'salsobig.
WithboththeBourneshellandtheCshellavailable,theUNIXusernow
hadachoice,andconflictsaroseoverwhichwasthebettershell.David
Korn,fromAT&T,inventedtheKornshellinthemid-1980s.Itwas
releasedin1986andofficiallybecamepartoftheSVR4distributionof
UNIXin1988.TheKornshell,reallyasupersetoftheBourneshell,runs
notonlyonUNIXsystems,butalsoonOS/2,VMS,andDOS.Itprovides
upward-compatibilitywiththeBourneshell,addsmanyofthepopular
featuresoftheCshell,andisfastandefficient.TheKornshellhasgone
throughanumberofrevisions.ThemostwidelyusedversionoftheKorn
shellisthe1988version,althoughthe1993versionisgainingpopularity.
LinuxusersmayfindtheyarerunningthefreeversionoftheKornshell,
calledThePublicDomainKornshell,orsimplypdksh,acloneofDavid
Korn's1988shell.Itisfreeandportableandcurrentlyworkisunderway
tomakeitfullycompatiblewithitsnamesake,Kornshell,andtomakeit
POSIXcompliant.AlsoavailableistheZshell(zsh),anotherKornshell
clonewithTCshellfeatures,writtenbyPaulFalsted,andfreelyavailable
atanumberofWebsites.
1.1.4UsesoftheShell
Oneofthemajorfunctionsofashellistointerpretcommandsenteredat
thecommandlinepromptwhenrunninginteractively.Theshellparses
thecommandline,breakingitintowords(calledtokens),separatedby
whitespace,whichconsistsoftabs,spaces,oranewline.Ifthewords
containspecialmetacharacters,theshellevaluatesthem.Theshell
handlesfileI/Oandbackgroundprocessing.Afterthecommandlinehas
beenprocessed,theshellsearchesforthecommandandstartsits
execution.
Anotherimportantfunctionoftheshellistocustomizetheuser's
environment,normallydoneinshellinitializationfiles.Thesefilescontain
definitionsforsettingterminalkeysandwindowcharacteristics;setting
variablesthatdefinethesearchpath,permissions,prompts,andthe
terminaltype;andsettingvariablesthatarerequiredforspecific
applicationssuchaswindows,text-processingprograms,andlibrariesfor
programminglanguages.TheKornshellandCshellalsoprovidefurther
customizationwiththeadditionofhistoryandaliases,built-invariables
settoprotecttheuserfromclobberingfilesorinadvertentlyloggingout,
andtonotifytheuserwhenajobhascompleted.
Theshellcanalsobeusedasaninterpretedprogramminglanguage.
Shellprograms,alsocalledscripts,consistofcommandslistedinafile.
Theprogramsarecreatedinaneditor(althoughon-linescriptingis
permitted).TheyconsistofUNIXcommandsinterspersedwith
fundamentalprogrammingconstructssuchasvariableassignment,
conditionaltests,andloops.Youdonothavetocompileshellscripts.The
shellinterpretseachlineofthescriptasifithadbeenenteredfromthe
keyboard.Becausetheshellisresponsibleforinterpretingcommands,it
isnecessaryfortheusertohaveanunderstandingofwhatthose
commandsare.SeeAppendixAforalistofusefulcommands.
1.1.5ResponsibilitiesoftheShell
Theshellisultimatelyresponsibleformakingsurethatanycommands
typedatthepromptgetproperlyexecuted.Includedinthose
responsibilitiesare:
1. Readinginputandparsingthecommandline.
Evaluatingspecialcharacters.
Settinguppipes,redirection,andbackgroundprocessing.
Handlingsignals.
Settingupprogramsforexecution.
Eachofthesetopicsisdiscussedindetailasitpertainstoaparticular
shell.
1.2SystemStartupandtheLoginShell
Whenyoustartupyoursystem,thefirstprocessiscalledinit.Each
processhasaprocessidentificationnumberassociatedwithit,calledthe
PID.Sinceinitisthefirstprocess,itsPIDis1.Theinitprocessinitializes
thesystemandthenstartsanotherprocesstoopenterminallinesandset
upthestandardinput(stdin),standardoutput(stdout),andstandarderror
(stderr),whichareallassociatedwiththeterminal.Thestandardinput
normallycomesfromthekeyboard;thestandardoutputandstandard
errorgotothescreen.Atthispoint,aloginpromptwouldappearonyour
terminal.
Afteryoutypeyourloginname,youwillbepromptedforapassword.The
/bin/loginprogramthenverifiesyouridentitybycheckingthefirstfieldin
thepasswdfile.Ifyourusernameisthere,thenextstepistorunthe
passwordyoutypedthroughanencryptionprogramtodetermineifitis
indeedthecorrectpassword.Onceyourpasswordisverified,thelogin
programsetsupaninitialenvironmentconsistingofvariablesthatdefine
theworkingenvironmentthatwillbepassedontotheshell.TheHOME,
SHELL,USER,andLOGNAMEvariablesareassignedvaluesextracted
frominformationinthepasswdfile.TheHOMEvariableisassignedyour
homedirectory;theSHELLvariableisassignedthenameofthelogin
shell,whichisthelastentryinthepasswdfile.TheUSERand/or
LOGNAMEvariablesareassignedyourloginname.Asearchpath
variableissetsothatcommonlyusedutilitiesmaybefoundinspecified
directories.Whenloginhasfinished,itwillexecutetheprogramfoundin
thelastentryofthepasswdfile.Normally,thisprogramisashell.Ifthe
lastentryinthepasswdfileis/bin/csh,theCshellprogramisexecuted.If
thelastentryinthepasswdfileis/bin/shorisnull,theBourneshellstarts
up.Ifthelastentryis/bin/ksh,theKornshellisexecuted.Thisshellis
calledtheloginshell.
Aftertheshellstartsup,itchecksforanysystemwideinitializationfiles
setupbythesystemadministratorandthenchecksyourhomedirectory
toseeifthereareanyshell-specificinitializationfilesthere.Ifanyof
thesefilesexist,theyareexecuted.Theinitializationfilesareusedto
furthercustomizetheuserenvironment.Afterthecommandsinthose
fileshavebeenexecuted,apromptappearsonthescreen.Theshellis
nowwaitingforyourinput.
1.2.1ParsingtheCommandLine
Whenyoutypeacommandattheprompt,theshellreadsalineofinput
andparsesthecommandline,breakingthelineintowords,calledtokens.
Tokensareseparatedbyspacesandtabsandthecommandlineis
terminatedbyanewline.[1]Theshellthencheckstoseewhetherthefirst
wordisabuilt-incommandoranexecutableprogramlocatedsomewhere
outondisk.Ifitisbuilt-in,theshellwillexecutethecommandinternally.
Otherwise,theshellwillsearchthedirectorieslistedinthepathvariable
tofindoutwheretheprogramresides.Ifthecommandisfound,theshell
willforkanewprocessandthenexecutetheprogram.Theshellwill
sleep(orwait)untiltheprogramfinishesexecutionandthen,if
necessary,willreportthestatusoftheexitingprogram.Apromptwill
appearandthewholeprocesswillstartagain.Theorderofprocessing
thecommandlineisasfollows:
1. Historysubstitutionisperformed(ifapplicable).
Commandlineisbrokenupintotokens,orwords.
Historyisupdated(ifapplicable).
Quotesareprocessed.
Aliassubstitutionandfunctionsaredefined(ifapplicable).
Redirection,background,andpipesaresetup.
Variablesubstitution($user,$name,etc.)isperformed.
Commandsubstitution(echofortodayis'date')isperformed.
Filenamesubstitution,calledglobbing(catabc.??,rm*.c,etc.)is
performed.
Programexecution.
1.2.2TypesofCommands
Whenacommandisexecuted,itisanalias,afunction,abuilt-in
command,oranexecutableprogramondisk.Aliasesareabbreviations
(nicknames)forexistingcommandsandapplytotheC,TC,Bash,and
Kornshells.FunctionsapplytotheBourne(introducedwithAT&TSystem
V,Release2.0),Bash,andKornshells.Theyaregroupsofcommands
organizedasseparateroutines.Aliasesandfunctionsaredefinedwithin
theshell'smemory.Built-incommandsareinternalroutinesintheshell,
andexecutableprogramsresideondisk.Theshellusesthepathvariable
tolocatetheexecutableprogramsondiskandforksachildprocess
beforethecommandcanbeexecuted.Thistakestime.Whentheshellis
readytoexecutethecommand,itevaluatescommandtypesinthe
followingorder:[2]
1. Aliases
Keywords
Functions(bash)
Built-incommands
Executableprograms
If,forexample,thecommandisxyztheshellwillchecktoseeifxyzisan
alias.Ifnot,isitabuilt-incommandorafunction?Ifneitherofthose,it
mustbeanexecutablecommandresidingonthedisk.Theshellthen
mustsearchthepathforthecommand.
1.3ProcessesandtheShell
Aprocessisaprograminexecutionandcanbeidentifiedbyitsunique
PID(processidentification)number.Thekernelcontrolsandmanages
processes.Aprocessconsistsoftheexecutableprogram,itsdataand
stack,programandstackpointer,registers,andalltheinformation
neededfortheprogramtorun.Whenyoustarttheshell,itisaprocess.
Theshellbelongstoaprocessgroupidentifiedbythegroup'sPID.Only
oneprocessgrouphascontroloftheterminalatatimeandissaidtobe
runningintheforeground.Whenyoulogon,yourshellisincontrolofthe
terminalandwaitsforyoutotypeacommandattheprompt.
Theshellcanspawnotherprocesses.Infact,whenyouentera
commandatthepromptorfromashellscript,theshellhasthe
responsibilityoffindingthecommandeitherinitsinternalcode(built-in)
oroutonthediskandthenarrangingforthecommandtobeexecuted.
Thisisdonewithcallstothekernel,calledsystemcalls.Asystemcallis
arequestforkernelservicesandistheonlywayaprocesscanaccess
thesystem'shardware.Thereareanumberofsystemcallsthatallow
processestobecreated,executed,andterminated.(Theshellprovides
otherservicesfromthekernelwhenitperformsredirectionandpiping,
commandsubstitution,andtheexecutionofusercommands.)
Thesystemcallsusedbytheshelltocausenewprocessestorunare
discussedinthefollowingsections.SeeFigure1.2.
Figure1.2.Theshellandcommandexecution.
1.3.1WhatProcessesAreRunning?
ThepsCommand.Thepscommandwithitsmanyoptionsdisplaysalist
oftheprocessescurrentlyrunninginanumberofformats.Example1.1
showsallprocessesthatarerunningbyusersonaLinuxsystem.(See
AppendixAforpsanditsoptions.)