AnIntroductiontotheUNIXShell S.R.Bourne ABSTRACT Theshellisacommandprogramminglanguagethatprovidesaninterfacetothe UNIX◯roperatingsystem.Itsfeaturesincludecontrol-flowprimitives,parameter passing,variablesandstringsubstitution.Constructssuchaswhile,ifthenelse, caseandforareavailable.Two-waycommunicationispossiblebetweentheshell andcommands.String-valuedparameters,typicallyfilenamesorflags,maybe passedtoacommand.Areturncodeissetbycommandsthatmaybeusedto determinecontrol-flow,andthestandardoutputfromacommandmaybeusedas shellinput. Theshellcanmodifytheenvironmentinwhichcommandsrun.Inputandoutput canberedirectedtofiles,andprocessesthatcommunicatethrough‘pipes’canbe invoked.Commandsarefoundbysearchingdirectoriesinthefilesystemina sequencethatcanbedefinedbytheuser.Commandscanbereadeitherfromthe terminalorfromafile,whichallowscommandprocedurestobestoredforlater use. 9April1993 AnIntroductiontotheUNIXShell S.R.Bourne 1.0Introduction Theshellisbothacommandlanguageandaprogramminglanguagethatprovidesaninterfaceto theUNIXoperatingsystem.Thismemorandumdescribes,withexamples,theUNIXshell.The firstsectioncoversmostoftheeverydayrequirementsofterminalusers.Somefamiliaritywith UNIXisanadvantagewhenreadingthissection;see,forexample,"UNIXforbeginners".1Section 2describesthosefeaturesoftheshellprimarilyintendedforusewithinshellprocedures.These includethecontrol-flowprimitivesandstring-valuedvariablesprovidedbytheshell.Aknowledge ofaprogramminglanguagewouldbeahelpwhenreadingthissection.Thelastsectiondescribes themoreadvancedfeaturesoftheshell.Referencesoftheform"seepipe(2)"aretoasectionof theUNIXmanual.2 1.1Simplecommands Simplecommandsconsistofoneormorewordsseparatedbyblanks.Thefirstwordisthename ofthecommandtobeexecuted;anyremainingwordsarepassedasargumentstothecommand. Forexample, who isacommandthatprintsthenamesofusersloggedin.Thecommand ls−l printsalistoffilesinthecurrentdirectory.Theargument−ltellslstoprintstatusinformation, sizeandthecreationdateforeachfile. 1.2Backgroundcommands Toexecuteacommandtheshellnormallycreatesanewprocessandwaitsforittofinish.Acommandmayberunwithoutwaitingforittofinish.Forexample, ccpgm.c& callstheCcompilertocompilethefilepgm.c.Thetrailing&isanoperatorthatinstructsthe shellnottowaitforthecommandtofinish.Tohelpkeeptrackofsuchaprocesstheshellreports itsprocessnumberfollowingitscreation.Alistofcurrentlyactiveprocessesmaybeobtained usingthepscommand. 1.3Inputoutputredirection Mostcommandsproduceoutputonthestandardoutputthatisinitiallyconnectedtotheterminal.Thisoutputmaybesenttoafilebywriting,forexample, ls−l>file Thenotation>fileisinterpretedbytheshellandisnotpassedasanargumenttols.Iffiledoes notexistthentheshellcreatesit;otherwisetheoriginalcontentsoffilearereplacedwiththeoutputfromls.Outputmaybeappendedtoafileusingthenotation ls−l>>file Inthiscasefileisalsocreatedifitdoesnotalreadyexist.

-2-Thestandardinputofacommandmaybetakenfromafileinsteadoftheterminalbywriting,for example, wc<file Thecommandwcreadsitsstandardinput(inthiscaseredirectedfromfile)andprintsthe numberofcharacters,wordsandlinesfound.Ifonlythenumberoflinesisrequiredthen wc−l<file couldbeused. 1.4Pipelinesandfilters Thestandardoutputofonecommandmaybeconnectedtothestandardinputofanotherbywritingthe‘pipe’operator,indicatedby|,asin, ls−l|wc Twocommandsconnectedinthiswayconstituteapipelineandtheoveralleffectisthesameas ls−l>file;wc<file exceptthatnofileisused.Insteadthetwoprocessesareconnectedbyapipe(seepipe(2))and areruninparallel.Pipesareunidirectionalandsynchronizationisachievedbyhaltingwcwhen thereisnothingtoreadandhaltinglswhenthepipeisfull. Afilterisacommandthatreadsitsstandardinput,transformsitinsomeway,andprintsthe resultasoutput.Onesuchfilter,grep,selectsfromitsinputthoselinesthatcontainsomespecifiedstring.Forexample, ls|grepold printsthoselines,ifany,oftheoutputfromlsthatcontainthestringold.Anotherusefulfilteris sort.Forexample, who|sort willprintanalphabeticallysortedlistofloggedinusers. Apipelinemayconsistofmorethantwocommands,forexample, ls|grepold|wc−l printsthenumberoffilenamesinthecurrentdirectorycontainingthestringold. 1.5Filenamegeneration Manycommandsacceptargumentswhicharefilenames.Forexample, ls−lmain.c printsinformationrelatingtothefilemain.c. Theshellprovidesamechanismforgeneratingalistoffilenamesthatmatchapattern.For example, ls−l*.c generates,asargumentstols,allfilenamesinthecurrentdirectorythatendin.c.Thecharacter *isapatternthatwillmatchanystringincludingthenullstring.Ingeneralpatternsarespecifiedasfollows. *Matchesanystringofcharactersincludingthenullstring. ?Matchesanysinglecharacter. [...]Matchesanyoneofthecharactersenclosed.Apairofcharactersseparatedbya minuswillmatchanycharacterlexicallybetweenthepair.

-3-Forexample, [a−z]* matchesallnamesinthecurrentdirectorybeginningwithoneofthelettersathroughz. /usr/fred/test/? matchesallnamesinthedirectory/usr/fred/testthatconsistofasinglecharacter.Ifnofile nameisfoundthatmatchesthepatternthenthepatternispassed,unchanged,asanargument. Thismechanismisusefulbothtosavetypingandtoselectnamesaccordingtosomepattern.It mayalsobeusedtofindfiles.Forexample, echo/usr/fred/*/core findsandprintsthenamesofallcorefilesinsub-directoriesof/usr/fred.(echoisastandard UNIXcommandthatprintsitsarguments,separatedbyblanks.)Thislastfeaturecanbeexpensive,requiringascanofallsub-directoriesof/usr/fred. Thereisoneexceptiontothegeneralrulesgivenforpatterns.Thecharacter‘.’atthestartofa filenamemustbeexplicitlymatched. echo* willthereforeechoallfilenamesinthecurrentdirectorynotbeginningwith‘.’. echo.* willechoallthosefilenamesthatbeginwith‘.’.Thisavoidsinadvertentmatchingofthenames ‘.’and‘..’whichmean‘thecurrentdirectory’and‘theparentdirectory’respectively.(Notice thatlssuppressesinformationforthefiles‘.’and‘..’.) 1.6Quoting Charactersthathaveaspecialmeaningtotheshell,suchas<>*?|&,arecalledmetacharacters.AcompletelistofmetacharactersisgiveninappendixB.Anycharacterprecededbya\ isquotedandlosesitsspecialmeaning,ifany.The\iselidedsothat echo\\? willechoasingle?,and echo\\\\ willechoasingle\.Toallowlongstringstobecontinuedovermorethanonelinethesequence \newlineisignored. \isconvenientforquotingsinglecharacters.Whenmorethanonecharacterneedsquotingthe abovemechanismisclumsyanderrorprone.Astringofcharactersmaybequotedbyenclosing thestringbetweensinglequotes.Forexample, echoxx´ ****´ xx willecho xx****xx Thequotedstringmaynotcontainasinglequotebutmaycontainnewlines,whicharepreserved. Thisquotingmechanismisthemostsimpleandisrecommendedforcasualuse. Athirdquotingmechanismusingdoublequotesisalsoavailablethatpreventsinterpretationof somebutnotallmetacharacters.Discussionofthedetailsisdeferredtosection3.4. 1.7Prompting Whentheshellisusedfromaterminalitwillissueapromptbeforereadingacommand.By defaultthispromptis‘$’.Itmaybechangedbysaying,forexample, -4-PS1=yesdear thatsetstheprompttobethestringyesdear.Ifanewlineistypedandfurtherinputisneeded thentheshellwillissuetheprompt‘>’.Sometimesthiscanbecausedbymistypingaquote mark.Ifitisunexpectedthenaninterrupt(DEL)willreturntheshelltoreadanothercommand. Thispromptmaybechangedbysaying,forexample, PS2=more 1.8Theshellandlogin Followinglogin(1)theshelliscalledtoreadandexecutecommandstypedattheterminal.Ifthe user’slogindirectorycontainsthefile.profilethenitisassumedtocontaincommandsandis readbytheshellbeforereadinganycommandsfromtheterminal. 1.9Summary •ls Printthenamesoffilesinthecurrentdirectory. •ls>file Puttheoutputfromlsintofile. •ls|wc−l Printthenumberoffilesinthecurrentdirectory. •ls|grepold Printthosefilenamescontainingthestringold. •ls|grepold|wc−l Printthenumberoffileswhosenamecontainsthestringold. •ccpgm.c& Runccinthebackground.

-5-2.0Shellprocedures Theshellmaybeusedtoreadandexecutecommandscontainedinafile.Forexample, shfile[args...] callstheshelltoreadcommandsfromfile.Suchafileiscalledacommandprocedureorshellprocedure.Argumentsmaybesuppliedwiththecallandarereferredtoinfileusingthepositional parameters$1,$2,....Forexample,ifthefilewgcontains who|grep$1 then shwgfred isequivalentto who|grepfred UNIXfileshavethreeindependentattributes,read,writeandexecute.TheUNIXcommand chmod(1)maybeusedtomakeafileexecutable.Forexample, chmod+xwg willensurethatthefilewghasexecutestatus.Followingthis,thecommand wgfred isequivalentto shwgfred Thisallowsshellproceduresandprogramstobeusedinterchangeably.Ineithercaseanewprocessiscreatedtorunthecommand. Aswellasprovidingnamesforthepositionalparameters,thenumberofpositionalparametersin thecallisavailableas$#.Thenameofthefilebeingexecutedisavailableas$0. Aspecialshellparameter$*isusedtosubstituteforallpositionalparametersexcept$0.Atypicaluseofthisistoprovidesomedefaultarguments,asin, nroff−T450−ms$* whichsimplyprependssomeargumentstothosealreadygiven. 2.1Controlflow-for Afrequentuseofshellproceduresistoloopthroughthearguments($1,$2,...)executingcommandsonceforeachargument.Anexampleofsuchaprocedureistelthatsearchesthefile /usr/lib/telnosthatcontainslinesoftheform ... fredmh0123 bertmh0789 ... Thetextoftelis fori dogrep$i/usr/lib/telnos;done Thecommand telfred printsthoselinesin/usr/lib/telnosthatcontainthestringfred.

-6-telfredbert printsthoselinescontainingfredfollowedbythoseforbert. Theforloopnotationisrecognizedbytheshellandhasthegeneralform fornameinw1w2... docommand-list done Acommand-listisasequenceofoneormoresimplecommandsseparatedorterminatedbyanew-lineorsemicolon.Furthermore,reservedwordslikedoanddoneareonlyrecognizedfollowinga newlineorsemicolon.nameisashellvariablethatissettothewordsw1w2...inturneach timethecommand-listfollowingdoisexecuted.Ifinw1w2...isomittedthentheloopisexe-cutedonceforeachpositionalparameter;thatis,in$*isassumed. Anotherexampleoftheuseoftheforloopisthecreatecommandwhosetextis forido>$i;done Thecommand createalphabeta ensuresthattwoemptyfilesalphaandbetaexistandareempty.Thenotation>filemaybeused onitsowntocreateorclearthecontentsofafile.Noticealsothatasemicolon(ornewline)is requiredbeforedone. 2.2Controlflow-case Amultiplewaybranchisprovidedforbythecasenotation.Forexample, case$#in 1)cat>>$1;; 2)cat>>$2<$1;; *)echo\’usage:append[from]to\’;; esac isanappendcommand.Whencalledwithoneargumentas appendfile $#isthestring1andthestandardinputiscopiedontotheendoffileusingthecatcommand. appendfile1file2 appendsthecontentsoffile1ontofile2.Ifthenumberofargumentssuppliedtoappendisother than1or2thenamessageisprintedindicatingproperusage. Thegeneralformofthecasecommandis casewordin pattern)command-list;; ... esac Theshellattemptstomatchwordwitheachpattern,intheorderinwhichthepatternsappear.If amatchisfoundtheassociatedcommand-listisexecutedandexecutionofthecaseiscomplete. Since*isthepatternthatmatchesanystringitcanbeusedforthedefaultcase. Awordofcaution:nocheckismadetoensurethatonlyonepatternmatchesthecaseargument. Thefirstmatchfounddefinesthesetofcommandstobeexecuted.Intheexamplebelowthe commandsfollowingthesecond*willneverbeexecuted.

-7-case$#in *)...;; *)...;; esac Anotherexampleoftheuseofthecaseconstructionistodistinguishbetweendifferentformsof anargument.Thefollowingexampleisafragmentofacccommand. fori docase$iin −[ocs])...;; −*)echo\’unknownflag$i\’;; *.c)/lib/c0$i...;; *)echo\’unexpectedargument$i\’;; esac done Toallowthesamecommandstobeassociatedwithmorethanonepatternthecasecommand providesforalternativepatternsseparatedbya|.Forexample, case$iin −x|−y)... esac isequivalentto case$iin −[xy])... esac Theusualquotingconventionsapplysothat case$iin \\?)... willmatchthecharacter?. 2.3Heredocuments Theshellproceduretelinsection2.1usesthefile/usr/lib/telnostosupplythedataforgrep. Analternativeistoincludethisdatawithintheshellprocedureasaheredocument,asin, fori dogrep$i<<! ... fredmh0123 bertmh0789 ... ! done Inthisexampletheshelltakesthelinesbetween<<!and!asthestandardinputforgrep.The string!isarbitrary,thedocumentbeingterminatedbyalinethatconsistsofthestringfollowing <<. Parametersaresubstitutedinthedocumentbeforeitismadeavailabletogrepasillustratedby thefollowingprocedurecallededg.

-8-ed$3<<% g/$1/s//$2/g w % Thecall edgstring1string2file isthenequivalenttothecommand edfile<<% g/string1/s//string2/g w % andchangesalloccurrencesofstring1infiletostring2.Substitutioncanbepreventedusing\to quotethespecialcharacter$asin ed$3<<+ 1,\\$s/$1/$2/g w + (Thisversionofedgisequivalenttothefirstexceptthatedwillprinta?iftherearenooccurrencesofthestring$1.)Substitutionwithinaheredocumentmaybepreventedentirelybyquotingtheterminatingstring,forexample, grep$i<<\\# ... # Thedocumentispresentedwithoutmodificationtogrep.Ifparametersubstitutionisnotrequired inaheredocumentthislatterformismoreefficient. 2.4Shellvariables Theshellprovidesstring-valuedvariables.Variablenamesbeginwithaletterandconsistoflet-ters,digitsandunderscores.Variablesmaybegivenvaluesbywriting,forexample, user=fredbox=m000acct=mh0000 whichassignsvaluestothevariablesuser,boxandacct.Avariablemaybesettothenull stringbysaying,forexample, null= Thevalueofavariableissubstitutedbyprecedingitsnamewith$;forexample, echo$user willechofred. Variablesmaybeusedinteractivelytoprovideabbreviationsforfrequentlyusedstrings.For example, b=/usr/fred/bin mvpgm$b willmovethefilepgmfromthecurrentdirectorytothedirectory/usr/fred/bin.Amoregeneralnotationisavailableforparameter(orvariable)substitution,asin, echo${user} whichisequivalentto -9-echo$user andisusedwhentheparameternameisfollowedbyaletterordigit.Forexample, tmp=/tmp/ps psa>${tmp}a willdirecttheoutputofpstothefile/tmp/psa,whereas, psa>$tmpa wouldcausethevalueofthevariabletmpatobesubstituted. Exceptfor$?thefollowingaresetinitiallybytheshell.$?issetafterexecutingeachcommand. $?Theexitstatus(returncode)ofthelastcommandexecutedasadecimalstring. Mostcommandsreturnazeroexitstatusiftheycompletesuccessfully,otherwisea non-zeroexitstatusisreturned.Testingthevalueofreturncodesisdealtwith laterunderifandwhilecommands. $#Thenumberofpositionalparameters(indecimal).Used,forexample,inthe appendcommandtocheckthenumberofparameters. $$Theprocessnumberofthisshell(indecimal).Sinceprocessnumbersareunique amongallexistingprocesses,thisstringisfrequentlyusedtogenerateuniquetemporaryfilenames.Forexample, psa>/tmp/ps$$ ... rm/tmp/ps$$ $!Theprocessnumberofthelastprocessruninthebackground(indecimal). $−Thecurrentshellflags,suchas−xand−v. Somevariableshaveaspecialmeaningtotheshellandshouldbeavoidedforgeneraluse. $MAILWhenusedinteractivelytheshelllooksatthefilespecifiedbythisvariablebefore itissuesaprompt.Ifthespecifiedfilehasbeenmodifiedsinceitwaslastlookedat theshellprintsthemessageyouhavemailbeforepromptingforthenextcommand. Thisvariableistypicallysetinthefile.profile,intheuser’slogindirectory.For example, MAIL=/usr/mail/fred $HOME Thedefaultargumentforthecdcommand.Thecurrentdirectoryisusedtoresolve filenamereferencesthatdonotbeginwitha/,andischangedusingthecdcommand.Forexample, cd/usr/fred/bin makesthecurrentdirectory/usr/fred/bin. catwn willprintontheterminalthefilewninthisdirectory.Thecommandcdwithno argumentisequivalentto cd$HOME Thisvariableisalsotypicallysetinthetheuser’sloginprofile. $PATHAlistofdirectoriesthatcontaincommands(thesearchpath).Eachtimeacommandisexecutedbytheshellalistofdirectoriesissearchedforanexecutablefile.

-10-If$PATHisnotsetthenthecurrentdirectory,/bin,and/usr/binaresearched bydefault.Otherwise$PATHconsistsofdirectorynamesseparatedby:.For example, PATH=:/usr/fred/bin:/bin:/usr/bin specifiesthatthecurrentdirectory(thenullstringbeforethefirst:), /usr/fred/bin,/binand/usr/binaretobesearchedinthatorder.Inthisway individualuserscanhavetheirown‘private’commandsthatareaccessibleindependentlyofthecurrentdirectory.Ifthecommandnamecontainsa/thenthisdirectorysearchisnotused;asingleattemptismadetoexecutethecommand. $PS1Theprimaryshellpromptstring,bydefault,‘$’. $PS2Theshellpromptwhenfurtherinputisneeded,bydefault,‘>’. $IFSThesetofcharactersusedbyblankinterpretation(seesection3.4). 2.5Thetestcommand Thetestcommand,althoughnotpartoftheshell,isintendedforusebyshellprograms.For example, test−ffile returnszeroexitstatusiffileexistsandnon-zeroexitstatusotherwise.Ingeneraltestevaluatesa predicateandreturnstheresultasitsexitstatus.Someofthemorefrequentlyusedtestargumentsaregivenhere,seetest(1)foracompletespecification. teststrueiftheargumentsisnotthenullstring test−ffiletrueiffileexists test−rfiletrueiffileisreadable test−wfiletrueiffileiswritable test−dfiletrueiffileisadirectory 2.6Controlflow-while Theactionsoftheforloopandthecasebrancharedeterminedbydataavailabletotheshell.A whileoruntilloopandanifthenelsebrancharealsoprovidedwhoseactionsaredetermined bytheexitstatusreturnedbycommands.Awhileloophasthegeneralform whilecommand-list1 docommand-list2 done Thevaluetestedbythewhilecommandistheexitstatusofthelastsimplecommandfollowing while.Eachtimeroundtheloopcommand-list1isexecuted;ifazeroexitstatusisreturnedthen command-list2isexecuted;otherwise,theloopterminates.Forexample, whiletest$1 do... shift done isequivalentto fori do... done shiftisashellcommandthatrenamesthepositionalparameters$2,$3,...as$1,$2,...and loses$1.

-11-Anotherkindofuseforthewhile/untilloopistowaituntilsomeexternaleventoccursandthen runsomecommands.Inanuntillooptheterminationconditionisreversed.Forexample, untiltest−ffile dosleep300;done commands willloopuntilfileexists.Eachtimeroundtheloopitwaitsfor5minutesbeforetryingagain. (Presumablyanotherprocesswilleventuallycreatethefile.) 2.7Controlflow-if Alsoavailableisageneralconditionalbranchoftheform, ifcommand-list thencommand-list elsecommand-list fi thatteststhevaluereturnedbythelastsimplecommandfollowingif. Theifcommandmaybeusedinconjunctionwiththetestcommandtotestfortheexistenceofa fileasin iftest−ffile thenprocessfile elsedosomethingelse fi Anexampleoftheuseofif,caseandforconstructionsisgiveninsection2.10. Amultipletestifcommandoftheform if... then... elseif... then... elseif... ... fi fi fi maybewrittenusinganextensionoftheifnotationas, if... then... elif... then... elif... ... fi Thefollowingexampleisthetouchcommandwhichchangesthe‘lastmodified’timeforalistof files.Thecommandmaybeusedinconjunctionwithmake(1)toforcerecompilationofalistof files.

-12-flag= fori docase$iin −c)flag=N;; *)iftest−f$i thenln$ijunk$$;rmjunk$$ eliftest$flag thenechofile\\´ $i\\´ doesnotexist else>$i fi esac done The−cflagisusedinthiscommandtoforcesubsequentfilestobecreatediftheydonotalready exist.Otherwise,ifthefiledoesnotexist,anerrormessageisprinted.Theshellvariableflagis settosomenon-nullstringifthe−cargumentisencountered.Thecommands ln...;rm... makealinktothefileandthenremoveitthuscausingthelastmodifieddatetobeupdated. Thesequence ifcommand1 thencommand2 fi maybewritten command1&&command2 Conversely, command1||command2 executescommand2onlyifcommand1fails.Ineachcasethevaluereturnedisthatofthelast simplecommandexecuted. 2.8Commandgrouping Commandsmaybegroupedintwoways, {command-list;} and (command-list) Inthefirstcommand-listissimplyexecuted.Thesecondformexecutescommand-listasasepa-rateprocess.Forexample, (cdx;rmjunk) executesrmjunkinthedirectoryxwithoutchangingthecurrentdirectoryoftheinvokingshell. Thecommands cdx;rmjunk havethesameeffectbutleavetheinvokingshellinthedirectoryx. 2.9Debuggingshellprocedures Theshellprovidestwotracingmechanismstohelpwhendebuggingshellprocedures.Thefirstis invokedwithintheprocedureas -13-set−v (vforverbose)andcauseslinesoftheproceduretobeprintedastheyareread.Itisusefulto helpisolatesyntaxerrors.Itmaybeinvokedwithoutmodifyingtheprocedurebysaying sh−vproc... whereprocisthenameoftheshellprocedure.Thisflagmaybeusedinconjunctionwiththe−n flagwhichpreventsexecutionofsubsequentcommands.(Notethatsayingset−nataterminal willrendertheterminaluselessuntilanend-of-fileistyped.) Thecommand set−x willproduceanexecutiontrace.Followingparametersubstitutioneachcommandisprintedasit isexecuted.(Trytheseattheterminaltoseewhateffecttheyhave.)Bothflagsmaybeturned offbysaying set− andthecurrentsettingoftheshellflagsisavailableas$−. 2.10Themancommand ThefollowingisthemancommandwhichisusedtoprintsectionsoftheUNIXmanual.Itis called,forexample,as mansh man−ted man2fork Inthefirstthemanualsectionforshisprinted.Sincenosectionisspecified,section1isused. Thesecondexamplewilltypeset(−toption)themanualsectionfored.Thelastprintsthefork manualpagefromsection2.

-14-cd/usr/man :´ colonisthecommentcommand´ :´ defaultisnroff($N),section1($s)´ N=ns=1 fori docase$iin [1−9]*)s=$i;; −t)N=t;; −n)N=n;; −*)echounknownflag\\´ $i\\´ ;; *)iftest−fman$s/$i.$s then${N}roffman0/${N}aaman$s/$i.$s else:´ lookthroughallmanualsections´ found=no forjin123456789 doiftest−fman$j/$i.$j thenman$j$i found=yes fi done case$foundin no)echo\’$i:manualpagenotfound\’ esac fi esac done Figure1.Aversionofthemancommand -15-3.0Keywordparameters Shellvariablesmaybegivenvaluesbyassignmentorwhenashellprocedureisinvoked.Anargumenttoashellprocedureoftheformname=valuethatprecedesthecommandnamecausesvalue tobeassignedtonamebeforeexecutionoftheprocedurebegins.Thevalueofnameintheinvokingshellisnotaffected.Forexample, user=fredcommand willexecutecommandwithusersettofred.The−kflagcausesargumentsoftheform name=valuetobeinterpretedinthiswayanywhereintheargumentlist.Suchnamesaresometimescalledkeywordparameters.Ifanyargumentsremaintheyareavailableaspositionalparameters$1,$2,.... Thesetcommandmayalsobeusedtosetpositionalparametersfromwithinaprocedure.For example, set−* willset$1tothefirstfilenameinthecurrentdirectory,$2tothenext,andsoon.Notethatthe firstargument,−,ensurescorrecttreatmentwhenthefirstfilenamebeginswitha−. 3.1Parametertransmission Whenashellprocedureisinvokedbothpositionalandkeywordparametersmaybesuppliedwith thecall.Keywordparametersarealsomadeavailableimplicitlytoashellprocedurebyspecifying inadvancethatsuchparametersaretobeexported.Forexample, exportuserbox marksthevariablesuserandboxforexport.Whenashellprocedureisinvokedcopiesaremade ofallexportablevariablesforusewithintheinvokedprocedure.Modificationofsuchvariables withintheproceduredoesnotaffectthevaluesintheinvokingshell.Itisgenerallytrueofashell procedurethatitmaynotmodifythestateofitscallerwithoutexplicitrequestonthepartofthe caller.(Sharedfiledescriptorsareanexceptiontothisrule.) Nameswhosevalueisintendedtoremainconstantmaybedeclaredreadonly.Theformofthis commandisthesameasthatoftheexportcommand, readonlyname... Subsequentattemptstosetreadonlyvariablesareillegal. 3.2Parametersubstitution Ifashellparameterisnotsetthenthenullstringissubstitutedforit.Forexample,ifthevariabledisnotset echo$d or echo${d} willechonothing.Adefaultstringmaybegivenasin echo${d−.} whichwillechothevalueofthevariabledifitissetand‘.’otherwise.Thedefaultstringisevaluatedusingtheusualquotingconventionssothat echo${d−´ *´ } willecho*ifthevariabledisnotset.Similarly -16-echo${d−$1} willechothevalueofdifitissetandthevalue(ifany)of$1otherwise.Avariablemaybe assignedadefaultvalueusingthenotation echo${d=.} whichsubstitutesthesamestringas echo${d−.} andifdwerenotpreviouslysetthenitwillbesettothestring‘.’.(Thenotation${...=...}is notavailableforpositionalparameters.) Ifthereisnosensibledefaultthenthenotation echo${d?message} willechothevalueofthevariabledifithasone,otherwisemessageisprintedbytheshelland executionoftheshellprocedureisabandoned.Ifmessageisabsentthenastandardmessageis printed.Ashellprocedurethatrequiressomeparameterstobesetmightstartasfollows. :${user?}${acct?}${bin?} ... Colon(:)isacommandthatisbuiltintotheshellanddoesnothingonceitsargumentshave beenevaluated.Ifanyofthevariablesuser,acctorbinarenotsetthentheshellwillabandon executionoftheprocedure. 3.3Commandsubstitution Thestandardoutputfromacommandcanbesubstitutedinasimilarwaytoparameters.The commandpwdprintsonitsstandardoutputthenameofthecurrentdirectory.Forexample,if thecurrentdirectoryis/usr/fred/binthenthecommand d=` pwd` isequivalentto d=/usr/fred/bin Theentirestringbetweengraveaccents(` ...` )istakenasthecommandtobeexecutedandis replacedwiththeoutputfromthecommand.Thecommandiswrittenusingtheusualquoting conventionsexceptthata` mustbeescapedusinga\.Forexample, ls` echo"$1"` isequivalentto ls$1 Commandsubstitutionoccursinallcontextswhereparametersubstitutionoccurs(includinghere documents)andthetreatmentoftheresultingtextisthesameinbothcases.Thismechanism allowsstringprocessingcommandstobeusedwithinshellprocedures.Anexampleofsucha commandisbasenamewhichremovesaspecifiedsuffixfromastring.Forexample, basenamemain.c.c willprintthestringmain.Itsuseisillustratedbythefollowingfragmentfromacccommand. case$Ain ... *.c)B=` basename$A.c` ... esac -17-thatsetsBtothepartof$Awiththesuffix.cstripped. Herearesomecompositeexamples. •foriin` ls−t` ;do... Thevariableiissettothenamesoffilesintimeorder,mostrecentfirst. •set` date` ;echo$6$2$3,$4 willprint,e.g.,1977Nov1,23:59:59 3.4Evaluationandquoting Theshellisamacroprocessorthatprovidesparametersubstitution,commandsubstitutionand filenamegenerationfortheargumentstocommands.Thissectiondiscussestheorderinwhich theseevaluationsoccurandtheeffectsofthevariousquotingmechanisms. CommandsareparsedinitiallyaccordingtothegrammargiveninappendixA.Beforeacommandisexecutedthefollowingsubstitutionsoccur. •parametersubstitution,e.g.$user •commandsubstitution,e.g.` pwd` Onlyoneevaluationoccurssothatif,forexample,thevalueofthevariableXisthe string$ythen echo$X willecho$y. •blankinterpretation Followingtheabovesubstitutionstheresultingcharactersarebrokenintonon-blank words(blankinterpretation).Forthispurpose‘blanks’arethecharactersofthestring $IFS.Bydefault,thisstringconsistsofblank,tabandnewline.Thenullstringisnot regardedasawordunlessitisquoted.Forexample, echo´ ´ willpassonthenullstringasthefirstargumenttoecho,whereas echo$null willcallechowithnoargumentsifthevariablenullisnotsetorsettothenullstring. •filenamegeneration Eachwordisthenscannedforthefilepatterncharacters*,?and[...]andanalphabeticallistoffilenamesisgeneratedtoreplacetheword.Eachsuchfilenameisaseparateargument. Theevaluationsjustdescribedalsooccurinthelistofwordsassociatedwithaforloop.Only substitutionoccursinthewordusedforacasebranch. Aswellasthequotingmechanismsdescribedearlierusing\and´ ...´ athirdquotingmechanism isprovidedusingdoublequotes.Withindoublequotesparameterandcommandsubstitution occursbutfilenamegenerationandtheinterpretationofblanksdoesnot.Thefollowingcharactershaveaspecialmeaningwithindoublequotesandmaybequotedusing\. $parametersubstitution ` commandsubstitution "endsthequotedstring \quotesthespecialcharacters$` "\ Forexample, echo"$x" willpassthevalueofthevariablexasasingleargumenttoecho.Similarly, -18-echo"$*" willpassthepositionalparametersasasingleargumentandisequivalentto echo"$1$2..." Thenotation$@isthesameas$*exceptwhenitisquoted. echo"$@" willpassthepositionalparameters,unevaluated,toechoandisequivalentto echo"$1""$2"... Thefollowingtablegives,foreachquotingmechanism,theshellmetacharactersthatareevaluated. metacharacter

\$*` "´

´ nnnnnt ` ynntnn "yynytn tterminator yinterpreted nnotinterpreted Figure2.Quotingmechanisms Incaseswheremorethanoneevaluationofastringisrequiredthebuilt-incommandevalmaybe used.Forexample,ifthevariableXhasthevalue$y,andifyhasthevaluepqrthen evalecho$X willechothestringpqr. Ingeneraltheevalcommandevaluatesitsarguments(asdoallcommands)andtreatstheresult asinputtotheshell.Theinputisreadandtheresultingcommand(s)executed.Forexample, wg=\’evalwho|grep\’ $wgfred isequivalentto who|grepfred Inthisexample,evalisrequiredsincethereisnointerpretationofmetacharacters,suchas|,followingsubstitution. 3.5Errorhandling Thetreatmentoferrorsdetectedbytheshelldependsonthetypeoferrorandonwhetherthe shellisbeingusedinteractively.Aninteractiveshellisonewhoseinputandoutputareconnected toaterminal(asdeterminedbygtty(2)).Ashellinvokedwiththe−iflagisalsointeractive. Executionofacommand(seealso3.7)mayfailforanyofthefollowingreasons. •Inputoutputredirectionmayfail.Forexample,ifafiledoesnotexistorcannotbecreated. •Thecommanditselfdoesnotexistorcannotbeexecuted. •Thecommandterminatesabnormally,forexample,witha"buserror"or"memoryfault". SeeFigure2belowforacompletelistofUNIXsignals. •Thecommandterminatesnormallybutreturnsanon-zeroexitstatus.

-19-Inallofthesecasestheshellwillgoontoexecutethenextcommand.Exceptforthelastcasean errormessagewillbeprintedbytheshell.Allremainingerrorscausetheshelltoexitfroma commandprocedure.Aninteractiveshellwillreturntoreadanothercommandfromtheterminal. Sucherrorsincludethefollowing. •Syntaxerrors.e.g.,if...then...done •Asignalsuchasinterrupt.Theshellwaitsforthecurrentcommand,ifany,tofinishexecutionandtheneitherexitsorreturnstotheterminal. •Failureofanyofthebuilt-incommandssuchascd. Theshellflag−ecausestheshelltoterminateifanyerrorisdetected. 1hangup 2interrupt 3*quit 4*illegalinstruction 5*tracetrap 6*IOTinstruction 7*EMTinstruction 8*floatingpointexception 9kill(cannotbecaughtorignored) 10*buserror 11*segmentationviolation 12*badargumenttosystemcall 13writeonapipewithnoonetoreadit 14alarmclock 15softwaretermination(fromkill(1)) Figure3.UNIXsignals Thosesignalsmarkedwithanasteriskproduceacoredumpifnotcaught.However,theshell itselfignoresquitwhichistheonlyexternalsignalthatcancauseadump.Thesignalsinthislist ofpotentialinteresttoshellprogramsare1,2,3,14and15. 3.6Faulthandling Shellproceduresnormallyterminatewhenaninterruptisreceivedfromtheterminal.Thetrap commandisusedifsomecleaningupisrequired,suchasremovingtemporaryfiles.Forexample, trap´ rm/tmp/ps$$;exit´ 2 setsatrapforsignal2(terminalinterrupt),andifthissignalisreceivedwillexecutethecommands rm/tmp/ps$$;exit exitisanotherbuilt-incommandthatterminatesexecutionofashellprocedure.Theexitis required;otherwise,afterthetraphasbeentaken,theshellwillresumeexecutingtheprocedureat theplacewhereitwasinterrupted. UNIXsignalscanbehandledinoneofthreeways.Theycanbeignored,inwhichcasethesignal isneversenttotheprocess.Theycanbecaught,inwhichcasetheprocessmustdecidewhat actiontotakewhenthesignalisreceived.Lastly,theycanbelefttocauseterminationofthe processwithoutithavingtotakeanyfurtheraction.Ifasignalisbeingignoredonentrytothe shellprocedure,forexample,byinvokingitinthebackground(see3.7)thentrapcommands(and thesignal)areignored. Theuseoftrapisillustratedbythismodifiedversionofthetouchcommand(Figure4).The cleanupactionistoremovethefilejunk$$.

-20-flag= trap´ rm−fjunk$$;exit´ 12315 fori docase$iin −c)flag=N;; *)iftest−f$i thenln$ijunk$$;rmjunk$$ eliftest$flag thenechofile\\´ $i\\´ doesnotexist else>$i fi esac done Figure4.Thetouchcommand Thetrapcommandappearsbeforethecreationofthetemporaryfile;otherwiseitwouldbepossiblefortheprocesstodiewithoutremovingthefile. Sincethereisnosignal0inUNIXitisusedbytheshelltoindicatethecommandstobeexecuted onexitfromtheshellprocedure. Aproceduremay,itself,electtoignoresignalsbyspecifyingthenullstringastheargumentto trap.Thefollowingfragmentistakenfromthenohupcommand. trap´ ´ 12315 whichcauseshangup,interrupt,quitandkilltobeignoredbothbytheprocedureandbyinvoked commands. Trapsmayberesetbysaying trap23 whichresetsthetrapsforsignals2and3totheirdefaultvalues.Alistofthecurrentvaluesof trapsmaybeobtainedbywriting trap Theprocedurescan(Figure5)isanexampleoftheuseoftrapwherethereisnoexitinthetrap command.scantakeseachdirectoryinthecurrentdirectory,promptswithitsname,andthen executescommandstypedattheterminaluntilanendoffileoraninterruptisreceived.Interruptsareignoredwhileexecutingtherequestedcommandsbutcauseterminationwhenscanis waitingforinput. d=` pwd` foriin* doiftest−d$d/$i thencd$d/$i whileecho"$i:" trapexit2 readx dotrap:2;eval$x;done fi done Figure5.Thescancommand readxisabuilt-incommandthatreadsonelinefromthestandardinputandplacestheresultin -21-thevariablex.Itreturnsanon-zeroexitstatusifeitheranend-of-fileisreadoraninterruptis received. 3.7Commandexecution Torunacommand(otherthanabuilt-in)theshellfirstcreatesanewprocessusingthesystem callfork.Theexecutionenvironmentforthecommandincludesinput,outputandthestatesof signals,andisestablishedinthechildprocessbeforethecommandisexecuted.Thebuilt-incom-mandexecisusedintherarecaseswhennoforkisrequiredandsimplyreplacestheshellwitha newcommand.Forexample,asimpleversionofthenohupcommandlookslike trap\’\’12315 exec$* Thetrapturnsoffthesignalsspecifiedsothattheyareignoredbysubsequentlycreatedcommandsandexecreplacestheshellbythecommandspecified. Mostformsofinputoutputredirectionhavealreadybeendescribed.Inthefollowingwordisonly subjecttoparameterandcommandsubstitution.Nofilenamegenerationorblankinterpretation takesplacesothat,forexample, echo...>*.c willwriteitsoutputintoafilewhosenameis*.c.Inputoutputspecificationsareevaluatedleft torightastheyappearinthecommand. >wordThestandardoutput(filedescriptor1)issenttothefilewordwhichiscreatedifit doesnotalreadyexist. >>wordThestandardoutputissenttofileword.Ifthefileexiststhenoutputisappended (byseekingtotheend);otherwisethefileiscreated. <wordThestandardinput(filedescriptor0)istakenfromthefileword. <<wordThestandardinputistakenfromthelinesofshellinputthatfollowuptobutnot includingalineconsistingonlyofword.Ifwordisquotedthennointerpretationof thedocumentoccurs.Ifwordisnotquotedthenparameterandcommandsubstitutionoccurand\isusedtoquotethecharacters\$` andthefirstcharacterof word.Inthelattercase\newlineisignored(c.f.quotedstrings). >&digitThefiledescriptordigitisduplicatedusingthesystemcalldup(2)andtheresultis usedasthestandardoutput. <&digitThestandardinputisduplicatedfromfiledescriptordigit. <&−Thestandardinputisclosed. >&−Thestandardoutputisclosed. Anyoftheabovemaybeprecededbyadigitinwhichcasethefiledescriptorcreatedisthatspecifiedbythedigitinsteadofthedefault0or1.Forexample, ...2>file runsacommandwithmessageoutput(filedescriptor2)directedtofile. ...2>&1 runsacommandwithitsstandardoutputandmessageoutputmerged.(Strictlyspeakingfile descriptor2iscreatedbyduplicatingfiledescriptor1buttheeffectisusuallytomergethetwo streams.) Theenvironmentforacommandruninthebackgroundsuchas list*.c|lpr& ismodifiedintwoways.Firstly,thedefaultstandardinputforsuchacommandistheemptyfile /dev/null.Thispreventstwoprocesses(theshellandthecommand),whicharerunningin -22-parallel,fromtryingtoreadthesameinput.Chaoswouldensueifthiswerenotthecase.For example, edfile& wouldallowboththeeditorandtheshelltoreadfromthesameinputatthesametime. TheothermodificationtotheenvironmentofabackgroundcommandistoturnofftheQUITand INTERRUPTsignalssothattheyareignoredbythecommand.Thisallowsthesesignalstobe usedattheterminalwithoutcausingbackgroundcommandstoterminate.Forthisreasonthe UNIXconventionforasignalisthatifitissetto1(ignored)thenitisneverchangedevenfora shorttime.Notethattheshellcommandtraphasnoeffectforanignoredsignal. 3.8Invokingtheshell Thefollowingflagsareinterpretedbytheshellwhenitisinvoked.Ifthefirstcharacterofargumentzeroisaminus,thencommandsarereadfromthefile.profile. −cstring Ifthe−cflagispresentthencommandsarereadfromstring. −sIfthe−sflagispresentorifnoargumentsremainthencommandsarereadfromthestandardinput.Shelloutputiswrittentofiledescriptor2. −iIfthe−iflagispresentoriftheshellinputandoutputareattachedtoaterminal(astold bygtty)thenthisshellisinteractive.InthiscaseTERMINATEisignored(sothatkill0 doesnotkillaninteractiveshell)andINTERRUPTiscaughtandignored(sothatwaitis interruptable).InallcasesQUITisignoredbytheshell. Acknowledgements ThedesignoftheshellisbasedinpartontheoriginalUNIXshell3andthePWB/UNIXshell,4 somefeatureshavingbeentakenfromboth.Similaritiesalsoexistwiththecommandinterpreters oftheCambridgeMultipleAccessSystem5andofCTSS.6 IwouldliketothankDennisRitchieandJohnMasheyformanydiscussionsduringthedesignof theshell.IamalsogratefultothemembersoftheComputingScienceResearchCenterandto JoeMaranzanofortheircommentsondraftsofthisdocument. References 1. 2. 3. 4. 5. 6.

-23-AppendixA-Grammar item:word input-output name=value simple-command:item simple-commanditem command:simple-command (command-list) {command-list} fornamedocommand-listdone fornameinword...docommand-listdone whilecommand-listdocommand-listdone untilcommand-listdocommand-listdone casewordincase-part...esac ifcommand-listthencommand-listelse-partfi pipeline:command pipeline|command andor:pipeline andor&&pipeline andor||pipeline command-list:andor command-list; command-list& command-list;andor command-list&andor input-output:>file <file >>word <<word file:word &digit &− case-part:pattern)command-list;; pattern:word pattern|word else-part:elifcommand-listthencommand-listelse-part elsecommand-list empty empty: word:asequenceofnon-blankcharacters name:asequenceofletters,digitsorunderscoresstartingwithaletter digit:0123456789 -24-AppendixB-Meta-charactersandReservedWords a)syntactic |pipesymbol &&‘andf’symbol ||‘orf’symbol ;commandseparator ;;casedelimiter &backgroundcommands ()commandgrouping <inputredirection <<inputfromaheredocument >outputcreation >>outputappend b)patterns *matchanycharacter(s)includingnone ?matchanysinglecharacter [...]matchanyoftheenclosedcharacters c)substitution ${...} substituteshellvariable ` ...` substitutecommandoutput d)quoting \quotethenextcharacter ´ ...´ quotetheenclosedcharactersexceptfor´ "..."quotetheenclosedcharactersexceptfor$` \" e)reservedwords ifthenelseeliffi caseinesac forwhileuntildodone {}