📄 article.txt
字号:
Bash - The GNU shell* Chet Ramey Case Western Reserve University chet@po.cwru.edu_1. _I_n_t_r_o_d_u_c_t_i_o_n _B_a_s_h is the shell, or command language interpreter,that will appear in the GNU operating system. The name isan acronym for the "Bourne-Again SHell", a pun on SteveBourne, the author of the direct ancestor of the currentUNIX|- shell /_b_i_n/_s_h, which appeared in the Seventh EditionBell Labs Research version of UNIX. Bash is an sh-compatible shell that incorporates usefulfeatures from the Korn shell (ksh) and the C shell (csh),described later in this article. It is ultimately intendedto be a conformant implementation of the IEEE POSIX Shelland Utilities specification (IEEE Working Group 1003.2). Itoffers functional improvements over sh for both interactiveand programming use. While the GNU operating system will most likely includea version of the Berkeley shell csh, Bash will be thedefault shell. Like other GNU software, Bash is quite port-able. It currently runs on nearly every version of UNIX anda few other operating systems - an independently-supportedport exists for OS/2, and there are rumors of ports to DOSand Windows NT. Ports to UNIX-like systems such as QNX andMinix are part of the distribution. The original author of Bash was Brian Fox, an employeeof the Free Software Foundation. The current developer andmaintainer is Chet Ramey, a volunteer who works at CaseWestern Reserve University._2. _W_h_a_t'_s _P_O_S_I_X, _a_n_y_w_a_y? _P_O_S_I_X is a name originally coined by Richard Stallman_________________________*An earlier version of this article appeared in TheLinux Journal.|- UNIX is a trademark of Bell Laboratories. October 28, 1994 - 2 -for a family of open system standards based on UNIX. Thereare a number of aspects of UNIX under consideration forstandardization, from the basic system services at the sys-tem call and C library level to applications and tools tosystem administration and management. Each area of stan-dardization is assigned to a working group in the 1003series. The POSIX Shell and Utilities standard has beendeveloped by IEEE Working Group 1003.2 (POSIX.2).|= It con-centrates on the command interpreter interface and utilityprograms commonly executed from the command line or by otherprograms. An initial version of the standard has beenapproved and published by the IEEE, and work is currentlyunderway to update it. There are four primary areas of workin the 1003.2 standard:o+ Aspects of the shell's syntax and command language. A number of special builtins such as _c_d and _e_x_e_c are being specified as part of the shell, since their func- tionality usually cannot be implemented by a separate executable;o+ A set of utilities to be called by shell scripts and applications. Examples are programs like _s_e_d, _t_r, and _a_w_k. Utilities commonly implemented as shell builtins are described in this section, such as _t_e_s_t and _k_i_l_l. An expansion of this section's scope, termed the User Portability Extension, or UPE, has standardized interactive programs such as _v_i and _m_a_i_l_x;o+ A group of functional interfaces to services provided by the shell, such as the traditional system() C library function. There are functions to perform shell word expansions, perform filename expansion (_g_l_o_b_b_i_n_g), obtain values of POSIX.2 system configuration vari- ables, retrieve values of environment variables (getenv()), _a_n_d _o_t_h_e_r _s_e_r_v_i_c_e_s;o+ A suite of "development" utilities such as _c_8_9 (the POSIX.2 version of _c_c), and _y_a_c_c. Bash is concerned with the aspects of the shell'sbehavior defined by POSIX.2. The shell command language hasof course been standardized, including the basic flow con-trol and program execution constructs, I/O redirection andpipelining, argument handling, variable expansion, and quot-ing. The _s_p_e_c_i_a_l builtins, which must be implemented aspart of the shell to provide the desired functionality, are_________________________|=IEEE, _I_E_E_E _S_t_a_n_d_a_r_d _f_o_r _I_n_f_o_r_m_a_t_i_o_n _T_e_c_h_n_o_l_o_g_y --_P_o_r_t_a_b_l_e _O_p_e_r_a_t_i_n_g _S_y_s_t_e_m _I_n_t_e_r_f_a_c_e (_P_O_S_I_X) _P_a_r_t _2:_S_h_e_l_l _a_n_d _U_t_i_l_i_t_i_e_s, 1992. October 28, 1994 - 3 -specified as being part of the shell; examples of these are_e_v_a_l and _e_x_p_o_r_t. Other utilities appear in the sections ofPOSIX.2 not devoted to the shell which are commonly (and insome cases must be) implemented as builtin commands, such as_r_e_a_d and _t_e_s_t. POSIX.2 also specifies aspects of theshell's interactive behavior as part of the UPE, includingjob control and command line editing. Interestingly enough,only _v_i-style line editing commands have been standardized;_e_m_a_c_s editing commands were left out due to objections. While POSIX.2 includes much of what the shell has trad-itionally provided, some important things have been omittedas being "beyond its scope." There is, for instance, nomention of a difference between a _l_o_g_i_n shell and any otherinteractive shell (since POSIX.2 does not specify a loginprogram). No fixed startup files are defined, either - thestandard does not mention ._p_r_o_f_i_l_e._3. _B_a_s_i_c _B_a_s_h _f_e_a_t_u_r_e_s Since the Bourne shell provides Bash with most of itsphilosophical underpinnings, Bash inherits most of itsfeatures and functionality from sh. Bash implements all ofthe traditional sh flow control constructs (_f_o_r, _i_f, _w_h_i_l_e,etc.). All of the Bourne shell builtins, including thosenot specified in the POSIX.2 standard, appear in Bash.Shell _f_u_n_c_t_i_o_n_s, introduced in the SVR2 version of theBourne shell, are similar to shell scripts, but are definedusing a special syntax and are executed in the same processas the calling shell. Bash has shell functions which behavein a fashion upward-compatible with sh functions. There arecertain shell variables that Bash interprets in the same wayas sh, such as _P_S_1, _I_F_S, and _P_A_T_H. Bash implements essen-tially the same grammar, parameter and variable expansionsemantics, redirection, and quoting as the Bourne shell.Where differences appear between the POSIX.2 standard andtraditional sh behavior, Bash follows POSIX. The Korn Shell (ksh) is a descendent of the Bourneshell written at AT&T Bell Laboratories by David Korn|-. Itprovides a number of useful features that POSIX and Bashhave adopted. Many of the interactive facilities in POSIX.2have their roots in the ksh: for example, the POSIX and kshjob control facilities are nearly identical. Bash includesfeatures from the Korn Shell for both interactive use andshell programming. For programming, Bash provides variablessuch as _R_A_N_D_O_M and _R_E_P_L_Y, the _t_y_p_e_s_e_t builtin, the abilityto remove substrings from variables based on patterns, andshell arithmetic. _R_A_N_D_O_M expands to a random number eachtime it is referenced; assigning a value to _R_A_N_D_O_M seeds the_________________________|-Morris Bolsky and David Korn, _T_h_e _K_o_r_n_S_h_e_l_l _C_o_m_m_a_n_d_a_n_d _P_r_o_g_r_a_m_m_i_n_g _L_a_n_g_u_a_g_e, Prentice Hall, 1989. October 28, 1994 - 4 -random number generator. _R_E_P_L_Y is the default variable usedby the _r_e_a_d builtin when no variable names are supplied asarguments. The _t_y_p_e_s_e_t builtin is used to define variablesand give them attributes such as readonly. Bash arithmeticallows the evaluation of an expression and the substitutionof the result. Shell variables may be used as operands, andthe result of an expression may be assigned to a variable.Nearly all of the operators from the C language are avail-able, with the same precedence rules:9 $ echo $((3 + 5 * 32)) 1639For interactive use, Bash implements ksh-style aliases andbuiltins such as _f_c (discussed below) and _j_o_b_s. Bashaliases allow a string to be substituted for a command name.They can be used to create a mnemonic for a UNIX commandname (alias del=rm), to expand a single word to a complexcommand (alias news='xterm -g 80x45 -title trn -e trn -e -S1-N &'), or to ensure that a command is invoked with a basicset of options (alias ls="/bin/ls -F"). The C shell (csh)|-, originally written by Bill Joywhile at Berkeley, is widely used and quite popular for itsinteractive facilities. Bash includes a csh-compatible his-tory expansion mechanism ("! history"), brace expansion,access to a stack of directories via the _p_u_s_h_d, _p_o_p_d, and_d_i_r_s builtins, and tilde expansion, to generate users' homedirectories. Tilde expansion has also been adopted by boththe Korn Shell and POSIX.2. There were certain areas in which POSIX.2 felt stan-dardization was necessary, but no existing implementationprovided the proper behavior. The working group inventedand standardized functionality in these areas, which Bashimplements. The _c_o_m_m_a_n_d builtin was invented so that shellfunctions could be written to replace builtins; it makes thecapabilities of the builtin available to the function. Thereserved word "!" was added to negate the return value of acommand or pipeline; it was nearly impossible to express "ifnot x" cleanly using the sh language. There exist multipleincompatible implementations of the _t_e_s_t builtin, whichtests files for type and other attributes and performsarithmetic and string comparisons. POSIX considered none ofthese correct, so the standard behavior was specified interms of the number of arguments to the command. POSIX.2dictates exactly what will happen when four or fewer argu-ments are given to _t_e_s_t, and leaves the behavior undefinedwhen more arguments are supplied. Bash uses the POSIX.2_________________________|-Bill Joy, An Introduction to the C Shell, _U_N_I_X _U_s_e_r'_s_S_u_p_p_l_e_m_e_n_t_a_r_y _D_o_c_u_m_e_n_t_s, University of California atBerkeley, 1986. October 28, 1994 - 5 -algorithm, which was conceived by David Korn._3._1. _F_e_a_t_u_r_e_s _n_o_t _i_n _t_h_e _B_o_u_r_n_e _S_h_e_l_l There are a number of minor differences between Bashand the version of sh present on most other versions ofUNIX. The majority of these are due to the POSIX standard,but some are the result of Bash adopting features from othershells. For instance, Bash includes the new "!" reserved
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -