📄 article.ms
字号:
.de SE \" start example.sp .5.RS.ft CR.nf...de EE \" end example.fi.sp .5.RE.ft R...TLBash \- The GNU shell*.AUChet RameyCase Western Reserve Universitychet@po.cwru.edu.FS*An earlier version of this article appeared in The Linux Journal..FE.NH 1Introduction.PP.B Bashis the shell, or command language interpreter,that will appear in the GNU operating system.The name is an acronym forthe \*QBourne-Again SHell\*U, a pun on Steve Bourne, the authorof the direct ancestor of the current.UXshell \fI/bin/sh\fP,which appeared in the Seventh Edition Bell Labs Research versionof \s-1UNIX\s+1..PPBash is an \fBsh\fP\-compatible shell that incorporates usefulfeatures from the Korn shell (\fBksh\fP) and the C shell (\fBcsh\fP),described later in this article. It is ultimately intended to be aconformant implementation of the IEEE POSIX Shell and Utilitiesspecification (IEEE Working Group 1003.2). It offers functionalimprovements over sh for both interactive and programming use..PPWhile the GNU operating system will most likely include a versionof the Berkeley shell csh, Bash will be the default shell.Like other GNU software, Bash is quite portable. It currently runson nearly every version of.UXand a few other operating systems \- an independently-supportedport exists for OS/2, and there are rumors of ports to DOS andWindows NT. Ports to \s-1UNIX\s+1-like systems such as QNX and Minixare part of the distribution..PPThe original author of Bashwas Brian Fox, an employee of the Free Software Foundation. Thecurrent developer and maintainer is Chet Ramey, a volunteer whoworks at Case Western Reserve University..NH 1What's POSIX, anyway?.PP.I POSIXis a name originally coined by Richard Stallman for a family of opensystem standards based on \s-1UNIX\s+1. There are a number of aspects of \s-1UNIX\s+1under consideration for standardization, from the basic system servicesat the system call and C library level to applications and tools to systemadministration and management. Each area of standardization isassigned to a working group in the 1003 series..PPThe POSIX Shell and Utilities standard has been developed by IEEE WorkingGroup 1003.2 (POSIX.2).\(dd.FS\(ddIEEE, \fIIEEE Standard for Information Technology -- PortableOperating System Interface (POSIX) Part 2: Shell and Utilities\fP,1992..FEIt concentrates on the command interpreterinterface and utility programscommonly executed from the command line or by other programs.An initial version of the standard has beenapproved and published by the IEEE, and work is currently underway toupdate it.There are four primary areas of work in the 1003.2 standard:.IP \(buAspects of the shell's syntax and command language.A number of special builtins such as.B cdand.B execare being specified as part of the shell, since theirfunctionality usually cannot be implemented by a separate executable;.IP \(buA set of utilities to be called by shell scripts and applications.Examples are programs like.I sed,.I tr,and.I awk.Utilities commonly implemented as shell builtinsare described in this section, such as.B testand.B kill .An expansion of this section's scope, termed the User PortabilityExtension, or UPE, has standardized interactive programs such as.I viand.I mailx;.IP \(buA group of functional interfaces to services provided by theshell, such as the traditional \f(CRsystem()\fPC library function. There are functions to perform shell wordexpansions, perform filename expansion (\fIglobbing\fP), obtain valuesof POSIX.2 system configuration variables, retrieve values ofenvironment variables (\f(CRgetenv()\fP\^), and other services;.IP \(buA suite of \*Qdevelopment\*U utilities such as.I c89(the POSIX.2 version of \fIcc\fP),and.I yacc..PPBash is concerned with the aspects of the shell's behaviordefined by POSIX.2. The shell command language has ofcourse been standardized, including the basic flow controland program execution constructs, I/O redirection andpipelining, argument handling, variable expansion, and quoting.The.I specialbuiltins, which must be implemented as part of the shell toprovide the desired functionality, are specified as beingpart of the shell; examples of these are.B evaland.B export .Other utilities appear in the sections of POSIX.2 notdevoted to the shell which are commonly (and in somecases must be) implemented as builtin commands, such as.B readand.B test .POSIX.2 also specifies aspects of the shell'sinteractive behavior as part ofthe UPE, including job control and command line editing.Interestingly enough, only \fIvi\fP-style line editing commandshave been standardized; \fIemacs\fP editing commands were leftout due to objections..PPWhile POSIX.2 includes much of what the shell has traditionallyprovided, some important things have been omitted as being\*Qbeyond its scope.\*U There is, for instance, no mention ofa difference between a.I loginshell and any other interactive shell (since POSIX.2 does notspecify a login program). No fixed startup files are defined,either \- the standard does not mention.I .profile ..NH 1Basic Bash features.PPSince the Bourne shellprovides Bash with most of its philosophical underpinnings,Bash inherits most of its features and functionality from sh.Bash implements all of the traditional sh flowcontrol constructs (\fIfor\fP, \fIif\fP, \fIwhile\fP, etc.).All of the Bourne shell builtins, including those not specified inthe POSIX.2 standard, appear in Bash. Shell \fIfunctions\fP,introduced in the SVR2 version of the Bourne shell,are similar to shell scripts, but are defined using a specialsyntax and are executed in the same process as the calling shell.Bash has shell functionswhich behave in a fashion upward-compatible with sh functions.There are certain shellvariables that Bash interprets in the same way as sh, such as.B PS1 ,.B IFS ,and.B PATH .Bash implements essentially the same grammar, parameter andvariable expansion semantics, redirection, and quoting as theBourne shell. Where differences appear between the POSIX.2standard and traditional sh behavior, Bash follows POSIX..PPThe Korn Shell (\fBksh\fP) is a descendent of the Bourne shell writtenat AT&T Bell Laboratories by David Korn\(dg. It provides a number ofuseful features that POSIX and Bash have adopted. Many of theinteractive facilities in POSIX.2 have their roots in the ksh:for example, the POSIX and ksh job control facilities are nearlyidentical. Bash includes features from the Korn Shell for bothinteractive use and shell programming. For programming, Bash providesvariables such as.B RANDOMand.B REPLY ,the.B typesetbuiltin,the ability to remove substrings from variables based on patterns,and shell arithmetic..FS\(dgMorris Bolsky and David Korn, \fIThe KornShell Command andProgramming Language\fP, Prentice Hall, 1989..FE.B RANDOMexpands to a random number each time it is referenced; assigning avalue to.B RANDOMseeds the random number generator..B REPLYis the default variable used by the.B readbuiltin when no variable names are supplied as arguments.The.B typesetbuiltin is used to define variables and give them attributessuch as \fBreadonly\fP.Bash arithmetic allows the evaluation of an expression and thesubstitution of the result. Shell variables may be used as operands,and the result of an expression may be assigned to a variable.Nearly all of the operators from the C language are available,with the same precedence rules:.SE$ echo $((3 + 5 * 32))163.EE.LPFor interactive use, Bash implements ksh-style aliases and builtinssuch as.B fc(discussed below) and.B jobs .Bash aliases allow a string to be substituted for a command name.They can be used to create a mnemonic for a \s-1UNIX\s+1 commandname (\f(CRalias del=rm\fP), to expand a single word to a complex command(\f(CRalias news='xterm -g 80x45 -title trn -e trn -e -S1 -N &'\fP), or toensure that a command is invoked with a basic set of options(\f(CRalias ls="/bin/ls -F"\fP)..PPThe C shell (\fBcsh\fP)\(dg, originally written by Bill Joy while atBerkeley, is widely used and quite popular for its interactivefacilities. Bash includes a csh-compatible history expansionmechanism (\*Q! history\*U), brace expansion, access to a stackof directories via the.B pushd ,.B popd ,and.B dirsbuiltins, and tilde expansion, to generate users' home directories.Tilde expansion has also been adopted by both the Korn Shell andPOSIX.2..FS\(dgBill Joy, An Introduction to the C Shell, \fIUNIX User's SupplementaryDocuments\fP, University of California at Berkeley, 1986..FE.PPThere were certain areas in which POSIX.2 felt standardizationwas necessary, but no existing implementation provided the properbehavior. The working group invented and standardized functionalityin these areas, which Bash implements. The.B commandbuiltin was invented so that shell functions could be written toreplace builtins; it makes the capabilities of the builtinavailable to the function. The reserved word \*Q!\*U was addedto negate the return value of a command or pipeline; it was nearlyimpossible to express \*Qif not x\*U cleanly using the sh language.There exist multiple incompatible implementations of the .B testbuiltin, which tests files for type and other attributes and performsarithmetic and string comparisons.POSIX considered none of these correct, so the standardbehavior was specified in terms of the number of arguments to thecommand. POSIX.2 dictates exactly what will happen when four orfewer arguments are given to.B test ,and leaves the behavior undefined when more arguments are supplied.Bash uses the POSIX.2 algorithm, which was conceived by David Korn..NH 2Features not in the Bourne Shell.PPThere are a number of minor differences between Bash and theversion of sh present on most other versions of \s-1UNIX\s+1. The majorityof these are due to the POSIX standard, but some are the result ofBash adopting features from other shells. For instance, Bashincludes the new \*Q!\*U reserved word, the.B commandbuiltin, the ability of the.B readbuiltin to correctly return a line ending with a backslash, symbolicarguments to the.B umaskbuiltin, variable substring removal, a way to get the length of a variable,and the new algorithm for the.B testbuiltin from the POSIX.2 standard, none of which appear in sh..PPBash also implements the \*Q$(...)\*U command substitution syntax,which supersedes the sh `...` construct.The \*Q$(...)\*U construct expands to the output of the commandcontained within theparentheses, with trailing newlines removed. The sh syntax isaccepted for backwards compatibility, but the \*Q$(...)\*U formis preferred because its quoting rules are much simpler and itis easier to nest..PPThe Bourne shell does not provide such features as brace expansion,the abilityto define a variable and a function with the same name, local variablesin shell functions, the ability to enable and disable individualbuiltins or write a function to replace a builtin, or a means toexport a shell function to a child process..PPBash has closeda long-standing shell security hole by not using the.B $IFSvariable to split each word read by the shell, but splitting onlythe results of expansion (ksh and the 4.4 BSD sh have fixed thisas well). Useful behavior such as a means to abortexecution of a script read with the \*Q.\*U command using the\fBreturn\fP builtin or automaticallyexporting variables in the shell's environment to children is alsonot present in the Bourne shell. Bash provides a much more powerfulenvironment for both interactive use and programming..NH 1Bash-specific Features.PPThis section details a few of the features which make Bash unique.Most of them provide improved interactive use, but a few programmingimprovements are present as well. Full descriptions of thesefeatures can be found in the Bash documentation..NH 2Startup Files.PPBash executes startup files differently than other shells. The Bashbehavior is a compromise between the csh principle of startup fileswith fixed names executed for each shell and the sh\*Qminimalist\*U behavior. An interactive instance of Bash startedas a login shell reads and executes.I ~/.bash_profile(the file .bash_profile in the user's home directory), if it exists.An interactive non-login shell reads and executes.I ~/.bashrc .A non-interactive shell (one begun to execute a shell script, forexample) reads no fixed startup file, but uses the value of the variable.B $ENV ,if set, as the name of a startup file. The ksh practice of reading.B $ENVfor every shell, with the accompanying difficulty of defining theproper variables and functions for interactive and non-interactiveshells or having the file read only for interactive shells, wasconsidered too complex. Ease of use won out here. Interestingly,the next release of ksh will change to reading.B $ENVonly for interactive shells..NH 2New Builtin Commands.PPThere are a few builtins which are new or have been extended in Bash.The.B enablebuiltin allows builtin commands to be turned on and off arbitrarily.To use the version of.I echofound in a user's search path rather than the Bash builtin,\f(CRenable -n echo\fP suffices. The.B helpbuiltin providesquick synopses of the shell facilities without requiringaccess to a manual page..B Builtinis similar to.B commandin that it bypasses shell functions and directly executes builtincommands. Access to a csh-style stack of directories is providedvia the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -