📄 article.txt
字号:
word, the _c_o_m_m_a_n_d builtin, the ability of the _r_e_a_d builtinto correctly return a line ending with a backslash, symbolicarguments to the _u_m_a_s_k builtin, variable substring removal,a way to get the length of a variable, and the new algorithmfor the _t_e_s_t builtin from the POSIX.2 standard, none ofwhich appear in sh. Bash also implements the "$(...)" command substitutionsyntax, which supersedes the sh `...` construct. The"$(...)" construct expands to the output of the command con-tained within the parentheses, with trailing newlinesremoved. The sh syntax is accepted for backwards compati-bility, but the "$(...)" form is preferred because its quot-ing rules are much simpler and it is easier to nest. The Bourne shell does not provide such features asbrace expansion, the ability to define a variable and afunction with the same name, local variables in shell func-tions, the ability to enable and disable individual builtinsor write a function to replace a builtin, or a means toexport a shell function to a child process. Bash has closed a long-standing shell security hole bynot using the $_I_F_S variable to split each word read by theshell, but splitting only the results of expansion (ksh andthe 4.4 BSD sh have fixed this as well). Useful behaviorsuch as a means to abort execution of a script read with the"." command using the return builtin or automaticallyexporting variables in the shell's environment to childrenis also not present in the Bourne shell. Bash provides amuch more powerful environment for both interactive use andprogramming._4. _B_a_s_h-_s_p_e_c_i_f_i_c _F_e_a_t_u_r_e_s This section details a few of the features which makeBash unique. Most of them provide improved interactive use,but a few programming improvements are present as well.Full descriptions of these features can be found in the Bashdocumentation._4._1. _S_t_a_r_t_u_p _F_i_l_e_s Bash executes startup files differently than othershells. The Bash behavior is a compromise between the csh October 28, 1994 - 6 -principle of startup files with fixed names executed foreach shell and the sh "minimalist" behavior. An interactiveinstance of Bash started as a login shell reads and executes~/._b_a_s_h__p_r_o_f_i_l_e (the file .bash_profile in the user's homedirectory), if it exists. An interactive non-login shellreads and executes ~/._b_a_s_h_r_c. A non-interactive shell (onebegun to execute a shell script, for example) reads no fixedstartup file, but uses the value of the variable $_E_N_V, ifset, as the name of a startup file. The ksh practice ofreading $_E_N_V for every shell, with the accompanying diffi-culty of defining the proper variables and functions forinteractive and non-interactive shells or having the fileread only for interactive shells, was considered too com-plex. Ease of use won out here. Interestingly, the nextrelease of ksh will change to reading $_E_N_V only for interac-tive shells._4._2. _N_e_w _B_u_i_l_t_i_n _C_o_m_m_a_n_d_s There are a few builtins which are new or have beenextended in Bash. The _e_n_a_b_l_e builtin allows builtin com-mands to be turned on and off arbitrarily. To use the ver-sion of _e_c_h_o found in a user's search path rather than theBash builtin, enable -n echo suffices. The _h_e_l_p builtinprovides quick synopses of the shell facilities withoutrequiring access to a manual page. _B_u_i_l_t_i_n is similar to_c_o_m_m_a_n_d in that it bypasses shell functions and directlyexecutes builtin commands. Access to a csh-style stack ofdirectories is provided via the _p_u_s_h_d, _p_o_p_d, and _d_i_r_s buil-tins. _P_u_s_h_d and _p_o_p_d insert and remove directories from thestack, respectively, and _d_i_r_s lists the stack contents. Onsystems that allow fine-grained control of resources, the_u_l_i_m_i_t builtin can be used to tune these settings. _U_l_i_m_i_tallows a user to control, among other things, whether coredumps are to be generated, how much memory the shell or achild process is allowed to allocate, and how large a filecreated by a child process can grow. The _s_u_s_p_e_n_d commandwill stop the shell process when job control is active; mostother shells do not allow themselves to be stopped likethat. _T_y_p_e, the Bash answer to _w_h_i_c_h and _w_h_e_n_c_e, shows whatwill happen when a word is typed as a command:9 $ type export export is a shell builtin $ type -t export builtin $ type bash bash is /bin/bash $ type cd cd is a function cd () { builtin cd ${1+"$@"} && xtitle $HOST: $PWD }9 October 28, 1994 - 7 -Various modes tell what a command word is (reserved word,alias, function, builtin, or file) or which version of acommand will be executed based on a user's search path.Some of this functionality has been adopted by POSIX.2 andfolded into the _c_o_m_m_a_n_d utility._4._3. _E_d_i_t_i_n_g _a_n_d _C_o_m_p_l_e_t_i_o_n One area in which Bash shines is command line editing.Bash uses the _r_e_a_d_l_i_n_e library to read and edit lines wheninteractive. Readline is a powerful and flexible inputfacility that a user can configure to individual tastes. Itallows lines to be edited using either emacs or vi commands,where those commands are appropriate. The full capabilityof emacs is not present - there is no way to execute a namedcommand with M-x, for instance - but the existing commandsare more than adequate. The vi mode is compliant with thecommand line editing standardized by POSIX.2. Readline is fully customizable. In addition to thebasic commands and key bindings, the library allows users todefine additional key bindings using a startup file. The_i_n_p_u_t_r_c file, which defaults to the file ~/._i_n_p_u_t_r_c, is readeach time readline initializes, permitting users to maintaina consistent interface across a set of programs. Readlineincludes an extensible interface, so each program using thelibrary can add its own bindable commands and program-specific key bindings. Bash uses this facility to add bind-ings that perform history expansion or shell word expansionson the current input line. Readline interprets a number of variables which furthertune its behavior. Variables exist to control whether ornot eight-bit characters are directly read as input or con-verted to meta-prefixed key sequences (a meta-prefixed keysequence consists of the character with the eighth bitzeroed, preceded by the _m_e_t_a-_p_r_e_f_i_x character, usuallyescape, which selects an alternate keymap), to decidewhether to output characters with the eighth bit setdirectly or as a meta-prefixed key sequence, whether or notto wrap to a new screen line when a line being edited islonger than the screen width, the keymap to which subsequentkey bindings should apply, or even what happens when read-line wants to ring the terminal's bell. All of these vari-ables can be set in the inputrc file. The startup file understands a set of C preprocessor-like conditional constructs which allow variables or keybindings to be assigned based on the application using read-line, the terminal currently being used, or the editingmode. Users can add program-specific bindings to make theirlives easier: I have bindings that let me edit the value of$_P_A_T_H and double-quote the current or previous word:9 # Macros that are convenient for shell interaction9 October 28, 1994 - 8 - $if Bash # edit the path "\C-xp": "PATH=${PATH}\e\C-e\C-a\ef\C-f" # prepare to type a quoted word -- insert open and close double # quotes and move to just after the open quote "\C-x\"": "\"\"\C-b" # Quote the current or previous word "\C-xq": "\eb\"\ef\"" $endif9There is a readline command to re-read the file, so userscan edit the file, change some bindings, and begin to usethem almost immediately. Bash implements the _b_i_n_d builtin for more dyamic con-trol of readline than the startup file permits. _B_i_n_d isused in several ways. In _l_i_s_t mode, it can display thecurrent key bindings, list all the readline editing direc-tives available for binding, list which keys invoke a givendirective, or output the current set of key bindings in aformat that can be incorporated directly into an inputrcfile. In _b_a_t_c_h mode, it reads a series of key bindingsdirectly from a file and passes them to readline. In itsmost common usage, _b_i_n_d takes a single string and passes itdirectly to readline, which interprets the line as if it hadjust been read from the inputrc file. Both key bindings andvariable assignments may appear in the string given to _b_i_n_d. The readline library also provides an interface for_w_o_r_d _c_o_m_p_l_e_t_i_o_n. When the _c_o_m_p_l_e_t_i_o_n character (usuallyTAB) is typed, readline looks at the word currently beingentered and computes the set of filenames of which thecurrent word is a valid prefix. If there is only one possi-ble completion, the rest of the characters are inserteddirectly, otherwise the common prefix of the set offilenames is added to the current word. A second TAB char-acter entered immediately after a non-unique completioncauses readline to list the possible completions; there isan option to have the list displayed immediately. Readlineprovides hooks so that applications can provide specifictypes of completion before the default filename completionis attempted. This is quite flexible, though it is not com-pletely user-programmable. Bash, for example, can completefilenames, command names (including aliases, builtins, shellreserved words, shell functions, and executables found inthe file system), shell variables, usernames, and hostnames.It uses a set of heuristics that, while not perfect, is gen-erally quite good at determining what type of completion toattempt._4._4. _H_i_s_t_o_r_y Access to the list of commands previously entered (the_c_o_m_m_a_n_d _h_i_s_t_o_r_y) is provided jointly by Bash and the9 October 28, 1994 - 9 -readline library. Bash provides variables ($HISTFILE,$HISTSIZE, and $HISTCONTROL) and the _h_i_s_t_o_r_y and _f_c builtinsto manipulate the history list. The value of $_H_I_S_T_F_I_L_Especifes the file where Bash writes the command history onexit and reads it on startup. $_H_I_S_T_S_I_Z_E is used to limitthe number of commands saved in the history. $_H_I_S_T_C_O_N_T_R_O_Lprovides a crude form of control over which commands aresaved on the history list: a value of _i_g_n_o_r_e_s_p_a_c_e means tonot save commands which begin with a space; a value of_i_g_n_o_r_e_d_u_p_s means to not save commands identical to the lastcommand saved. $HISTCONTROL was named $history_control inearlier versions of Bash; the old name is still accepted forbackwards compatibility. The _h_i_s_t_o_r_y command can read orwrite files containing the history list and display thecurrent list contents. The _f_c builtin, adopted from POSIX.2and the Korn Shell, allows display and re-execution, withoptional editing, of commands from the history list. Thereadline library offers a set of commands to search the his-tory list for a portion of the current input line or astring typed by the user. Finally, the _h_i_s_t_o_r_y library,generally incorporated directly into the readline library,implements a facility for history recall, expansion, andre-execution of previous commands very similar to csh ("banghistory", so called because the exclamation point introducesa history substitution):9 $ echo a b c d e
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -