| [3228] | 1 | .de SE \" start example
|
|---|
| 2 | .sp .5
|
|---|
| 3 | .RS
|
|---|
| 4 | .ft CR
|
|---|
| 5 | .nf
|
|---|
| 6 | ..
|
|---|
| 7 | .de EE \" end example
|
|---|
| 8 | .fi
|
|---|
| 9 | .sp .5
|
|---|
| 10 | .RE
|
|---|
| 11 | .ft R
|
|---|
| 12 | ..
|
|---|
| 13 | .TL
|
|---|
| 14 | Bash \- The GNU shell*
|
|---|
| 15 | .AU
|
|---|
| 16 | Chet Ramey
|
|---|
| 17 | Case Western Reserve University
|
|---|
| 18 | chet@po.cwru.edu
|
|---|
| 19 | .FS
|
|---|
| 20 | *An earlier version of this article appeared in The Linux Journal.
|
|---|
| 21 | .FE
|
|---|
| 22 | .NH 1
|
|---|
| 23 | Introduction
|
|---|
| 24 | .PP
|
|---|
| 25 | .B Bash
|
|---|
| 26 | is the shell, or command language interpreter,
|
|---|
| 27 | that will appear in the GNU operating system.
|
|---|
| 28 | The name is an acronym for
|
|---|
| 29 | the \*QBourne-Again SHell\*U, a pun on Steve Bourne, the author
|
|---|
| 30 | of the direct ancestor of the current
|
|---|
| 31 | .UX
|
|---|
| 32 | shell \fI/bin/sh\fP,
|
|---|
| 33 | which appeared in the Seventh Edition Bell Labs Research version
|
|---|
| 34 | of \s-1UNIX\s+1.
|
|---|
| 35 | .PP
|
|---|
| 36 | Bash is an \fBsh\fP\-compatible shell that incorporates useful
|
|---|
| 37 | features from the Korn shell (\fBksh\fP) and the C shell (\fBcsh\fP),
|
|---|
| 38 | described later in this article. It is ultimately intended to be a
|
|---|
| 39 | conformant implementation of the IEEE POSIX Shell and Utilities
|
|---|
| 40 | specification (IEEE Working Group 1003.2). It offers functional
|
|---|
| 41 | improvements over sh for both interactive and programming use.
|
|---|
| 42 | .PP
|
|---|
| 43 | While the GNU operating system will most likely include a version
|
|---|
| 44 | of the Berkeley shell csh, Bash will be the default shell.
|
|---|
| 45 | Like other GNU software, Bash is quite portable. It currently runs
|
|---|
| 46 | on nearly every version of
|
|---|
| 47 | .UX
|
|---|
| 48 | and a few other operating systems \- an independently-supported
|
|---|
| 49 | port exists for OS/2, and there are rumors of ports to DOS and
|
|---|
| 50 | Windows NT. Ports to \s-1UNIX\s+1-like systems such as QNX and Minix
|
|---|
| 51 | are part of the distribution.
|
|---|
| 52 | .PP
|
|---|
| 53 | The original author of Bash
|
|---|
| 54 | was Brian Fox, an employee of the Free Software Foundation. The
|
|---|
| 55 | current developer and maintainer is Chet Ramey, a volunteer who
|
|---|
| 56 | works at Case Western Reserve University.
|
|---|
| 57 | .NH 1
|
|---|
| 58 | What's POSIX, anyway?
|
|---|
| 59 | .PP
|
|---|
| 60 | .I POSIX
|
|---|
| 61 | is a name originally coined by Richard Stallman for a family of open
|
|---|
| 62 | system standards based on \s-1UNIX\s+1. There are a number of aspects of \s-1UNIX\s+1
|
|---|
| 63 | under consideration for standardization, from the basic system services
|
|---|
| 64 | at the system call and C library level to applications and tools to system
|
|---|
| 65 | administration and management. Each area of standardization is
|
|---|
| 66 | assigned to a working group in the 1003 series.
|
|---|
| 67 | .PP
|
|---|
| 68 | The POSIX Shell and Utilities standard has been developed by IEEE Working
|
|---|
| 69 | Group 1003.2 (POSIX.2).\(dd
|
|---|
| 70 | .FS
|
|---|
| 71 | \(ddIEEE, \fIIEEE Standard for Information Technology -- Portable
|
|---|
| 72 | Operating System Interface (POSIX) Part 2: Shell and Utilities\fP,
|
|---|
| 73 | 1992.
|
|---|
| 74 | .FE
|
|---|
| 75 | It concentrates on the command interpreter
|
|---|
| 76 | interface and utility programs
|
|---|
| 77 | commonly executed from the command line or by other programs.
|
|---|
| 78 | An initial version of the standard has been
|
|---|
| 79 | approved and published by the IEEE, and work is currently underway to
|
|---|
| 80 | update it.
|
|---|
| 81 | There are four primary areas of work in the 1003.2 standard:
|
|---|
| 82 | .IP \(bu
|
|---|
| 83 | Aspects of the shell's syntax and command language.
|
|---|
| 84 | A number of special builtins such as
|
|---|
| 85 | .B cd
|
|---|
| 86 | and
|
|---|
| 87 | .B exec
|
|---|
| 88 | are being specified as part of the shell, since their
|
|---|
| 89 | functionality usually cannot be implemented by a separate executable;
|
|---|
| 90 | .IP \(bu
|
|---|
| 91 | A set of utilities to be called by shell scripts and applications.
|
|---|
| 92 | Examples are programs like
|
|---|
| 93 | .I sed,
|
|---|
| 94 | .I tr,
|
|---|
| 95 | and
|
|---|
| 96 | .I awk.
|
|---|
| 97 | Utilities commonly implemented as shell builtins
|
|---|
| 98 | are described in this section, such as
|
|---|
| 99 | .B test
|
|---|
| 100 | and
|
|---|
| 101 | .B kill .
|
|---|
| 102 | An expansion of this section's scope, termed the User Portability
|
|---|
| 103 | Extension, or UPE, has standardized interactive programs such as
|
|---|
| 104 | .I vi
|
|---|
| 105 | and
|
|---|
| 106 | .I mailx;
|
|---|
| 107 | .IP \(bu
|
|---|
| 108 | A group of functional interfaces to services provided by the
|
|---|
| 109 | shell, such as the traditional \f(CRsystem()\fP
|
|---|
| 110 | C library function. There are functions to perform shell word
|
|---|
| 111 | expansions, perform filename expansion (\fIglobbing\fP), obtain values
|
|---|
| 112 | of POSIX.2 system configuration variables, retrieve values of
|
|---|
| 113 | environment variables (\f(CRgetenv()\fP\^), and other services;
|
|---|
| 114 | .IP \(bu
|
|---|
| 115 | A suite of \*Qdevelopment\*U utilities such as
|
|---|
| 116 | .I c89
|
|---|
| 117 | (the POSIX.2 version of \fIcc\fP),
|
|---|
| 118 | and
|
|---|
| 119 | .I yacc.
|
|---|
| 120 | .PP
|
|---|
| 121 | Bash is concerned with the aspects of the shell's behavior
|
|---|
| 122 | defined by POSIX.2. The shell command language has of
|
|---|
| 123 | course been standardized, including the basic flow control
|
|---|
| 124 | and program execution constructs, I/O redirection and
|
|---|
| 125 | pipelining, argument handling, variable expansion, and quoting.
|
|---|
| 126 | The
|
|---|
| 127 | .I special
|
|---|
| 128 | builtins, which must be implemented as part of the shell to
|
|---|
| 129 | provide the desired functionality, are specified as being
|
|---|
| 130 | part of the shell; examples of these are
|
|---|
| 131 | .B eval
|
|---|
| 132 | and
|
|---|
| 133 | .B export .
|
|---|
| 134 | Other utilities appear in the sections of POSIX.2 not
|
|---|
| 135 | devoted to the shell which are commonly (and in some
|
|---|
| 136 | cases must be) implemented as builtin commands, such as
|
|---|
| 137 | .B read
|
|---|
| 138 | and
|
|---|
| 139 | .B test .
|
|---|
| 140 | POSIX.2 also specifies aspects of the shell's
|
|---|
| 141 | interactive behavior as part of
|
|---|
| 142 | the UPE, including job control and command line editing.
|
|---|
| 143 | Interestingly enough, only \fIvi\fP-style line editing commands
|
|---|
| 144 | have been standardized; \fIemacs\fP editing commands were left
|
|---|
| 145 | out due to objections.
|
|---|
| 146 | .PP
|
|---|
| 147 | While POSIX.2 includes much of what the shell has traditionally
|
|---|
| 148 | provided, some important things have been omitted as being
|
|---|
| 149 | \*Qbeyond its scope.\*U There is, for instance, no mention of
|
|---|
| 150 | a difference between a
|
|---|
| 151 | .I login
|
|---|
| 152 | shell and any other interactive shell (since POSIX.2 does not
|
|---|
| 153 | specify a login program). No fixed startup files are defined,
|
|---|
| 154 | either \- the standard does not mention
|
|---|
| 155 | .I .profile .
|
|---|
| 156 | .NH 1
|
|---|
| 157 | Basic Bash features
|
|---|
| 158 | .PP
|
|---|
| 159 | Since the Bourne shell
|
|---|
| 160 | provides Bash with most of its philosophical underpinnings,
|
|---|
| 161 | Bash inherits most of its features and functionality from sh.
|
|---|
| 162 | Bash implements all of the traditional sh flow
|
|---|
| 163 | control constructs (\fIfor\fP, \fIif\fP, \fIwhile\fP, etc.).
|
|---|
| 164 | All of the Bourne shell builtins, including those not specified in
|
|---|
| 165 | the POSIX.2 standard, appear in Bash. Shell \fIfunctions\fP,
|
|---|
| 166 | introduced in the SVR2 version of the Bourne shell,
|
|---|
| 167 | are similar to shell scripts, but are defined using a special
|
|---|
| 168 | syntax and are executed in the same process as the calling shell.
|
|---|
| 169 | Bash has shell functions
|
|---|
| 170 | which behave in a fashion upward-compatible with sh functions.
|
|---|
| 171 | There are certain shell
|
|---|
| 172 | variables that Bash interprets in the same way as sh, such as
|
|---|
| 173 | .B PS1 ,
|
|---|
| 174 | .B IFS ,
|
|---|
| 175 | and
|
|---|
| 176 | .B PATH .
|
|---|
| 177 | Bash implements essentially the same grammar, parameter and
|
|---|
| 178 | variable expansion semantics, redirection, and quoting as the
|
|---|
| 179 | Bourne shell. Where differences appear between the POSIX.2
|
|---|
| 180 | standard and traditional sh behavior, Bash follows POSIX.
|
|---|
| 181 | .PP
|
|---|
| 182 | The Korn Shell (\fBksh\fP) is a descendent of the Bourne shell written
|
|---|
| 183 | at AT&T Bell Laboratories by David Korn\(dg. It provides a number of
|
|---|
| 184 | useful features that POSIX and Bash have adopted. Many of the
|
|---|
| 185 | interactive facilities in POSIX.2 have their roots in the ksh:
|
|---|
| 186 | for example, the POSIX and ksh job control facilities are nearly
|
|---|
| 187 | identical. Bash includes features from the Korn Shell for both
|
|---|
| 188 | interactive use and shell programming. For programming, Bash provides
|
|---|
| 189 | variables such as
|
|---|
| 190 | .B RANDOM
|
|---|
| 191 | and
|
|---|
| 192 | .B REPLY ,
|
|---|
| 193 | the
|
|---|
| 194 | .B typeset
|
|---|
| 195 | builtin,
|
|---|
| 196 | the ability to remove substrings from variables based on patterns,
|
|---|
| 197 | and shell arithmetic.
|
|---|
| 198 | .FS
|
|---|
| 199 | \(dgMorris Bolsky and David Korn, \fIThe KornShell Command and
|
|---|
| 200 | Programming Language\fP, Prentice Hall, 1989.
|
|---|
| 201 | .FE
|
|---|
| 202 | .B RANDOM
|
|---|
| 203 | expands to a random number each time it is referenced; assigning a
|
|---|
| 204 | value to
|
|---|
| 205 | .B RANDOM
|
|---|
| 206 | seeds the random number generator.
|
|---|
| 207 | .B REPLY
|
|---|
| 208 | is the default variable used by the
|
|---|
| 209 | .B read
|
|---|
| 210 | builtin when no variable names are supplied as arguments.
|
|---|
| 211 | The
|
|---|
| 212 | .B typeset
|
|---|
| 213 | builtin is used to define variables and give them attributes
|
|---|
| 214 | such as \fBreadonly\fP.
|
|---|
| 215 | Bash arithmetic allows the evaluation of an expression and the
|
|---|
| 216 | substitution of the result. Shell variables may be used as operands,
|
|---|
| 217 | and the result of an expression may be assigned to a variable.
|
|---|
| 218 | Nearly all of the operators from the C language are available,
|
|---|
| 219 | with the same precedence rules:
|
|---|
| 220 | .SE
|
|---|
| 221 | $ echo $((3 + 5 * 32))
|
|---|
| 222 | 163
|
|---|
| 223 | .EE
|
|---|
| 224 | .LP
|
|---|
| 225 | For interactive use, Bash implements ksh-style aliases and builtins
|
|---|
| 226 | such as
|
|---|
| 227 | .B fc
|
|---|
| 228 | (discussed below) and
|
|---|
| 229 | .B jobs .
|
|---|
| 230 | Bash aliases allow a string to be substituted for a command name.
|
|---|
| 231 | They can be used to create a mnemonic for a \s-1UNIX\s+1 command
|
|---|
| 232 | name (\f(CRalias del=rm\fP), to expand a single word to a complex command
|
|---|
| 233 | (\f(CRalias news='xterm -g 80x45 -title trn -e trn -e -S1 -N &'\fP), or to
|
|---|
| 234 | ensure that a command is invoked with a basic set of options
|
|---|
| 235 | (\f(CRalias ls="/bin/ls -F"\fP).
|
|---|
| 236 | .PP
|
|---|
| 237 | The C shell (\fBcsh\fP)\(dg, originally written by Bill Joy while at
|
|---|
| 238 | Berkeley, is widely used and quite popular for its interactive
|
|---|
| 239 | facilities. Bash includes a csh-compatible history expansion
|
|---|
| 240 | mechanism (\*Q! history\*U), brace expansion, access to a stack
|
|---|
| 241 | of directories via the
|
|---|
| 242 | .B pushd ,
|
|---|
| 243 | .B popd ,
|
|---|
| 244 | and
|
|---|
| 245 | .B dirs
|
|---|
| 246 | builtins, and tilde expansion, to generate users' home directories.
|
|---|
| 247 | Tilde expansion has also been adopted by both the Korn Shell and
|
|---|
| 248 | POSIX.2.
|
|---|
| 249 | .FS
|
|---|
| 250 | \(dgBill Joy, An Introduction to the C Shell, \fIUNIX User's Supplementary
|
|---|
| 251 | Documents\fP, University of California at Berkeley, 1986.
|
|---|
| 252 | .FE
|
|---|
| 253 | .PP
|
|---|
| 254 | There were certain areas in which POSIX.2 felt standardization
|
|---|
| 255 | was necessary, but no existing implementation provided the proper
|
|---|
| 256 | behavior. The working group invented and standardized functionality
|
|---|
| 257 | in these areas, which Bash implements. The
|
|---|
| 258 | .B command
|
|---|
| 259 | builtin was invented so that shell functions could be written to
|
|---|
| 260 | replace builtins; it makes the capabilities of the builtin
|
|---|
| 261 | available to the function. The reserved word \*Q!\*U was added
|
|---|
| 262 | to negate the return value of a command or pipeline; it was nearly
|
|---|
| 263 | impossible to express \*Qif not x\*U cleanly using the sh language.
|
|---|
| 264 | There exist multiple incompatible implementations of the
|
|---|
| 265 | .B test
|
|---|
| 266 | builtin, which tests files for type and other attributes and performs
|
|---|
| 267 | arithmetic and string comparisons.
|
|---|
| 268 | POSIX considered none of these correct, so the standard
|
|---|
| 269 | behavior was specified in terms of the number of arguments to the
|
|---|
| 270 | command. POSIX.2 dictates exactly what will happen when four or
|
|---|
| 271 | fewer arguments are given to
|
|---|
| 272 | .B test ,
|
|---|
| 273 | and leaves the behavior undefined when more arguments are supplied.
|
|---|
| 274 | Bash uses the POSIX.2 algorithm, which was conceived by David Korn.
|
|---|
| 275 | .NH 2
|
|---|
| 276 | Features not in the Bourne Shell
|
|---|
| 277 | .PP
|
|---|
| 278 | There are a number of minor differences between Bash and the
|
|---|
| 279 | version of sh present on most other versions of \s-1UNIX\s+1. The majority
|
|---|
| 280 | of these are due to the POSIX standard, but some are the result of
|
|---|
| 281 | Bash adopting features from other shells. For instance, Bash
|
|---|
| 282 | includes the new \*Q!\*U reserved word, the
|
|---|
| 283 | .B command
|
|---|
| 284 | builtin, the ability of the
|
|---|
| 285 | .B read
|
|---|
| 286 | builtin to correctly return a line ending with a backslash, symbolic
|
|---|
| 287 | arguments to the
|
|---|
| 288 | .B umask
|
|---|
| 289 | builtin, variable substring removal, a way to get the length of a variable,
|
|---|
| 290 | and the new algorithm for the
|
|---|
| 291 | .B test
|
|---|
| 292 | builtin from the POSIX.2 standard, none of which appear in sh.
|
|---|
| 293 | .PP
|
|---|
| 294 | Bash also implements the \*Q$(...)\*U command substitution syntax,
|
|---|
| 295 | which supersedes the sh `...` construct.
|
|---|
| 296 | The \*Q$(...)\*U construct expands to the output of the command
|
|---|
| 297 | contained within the
|
|---|
| 298 | parentheses, with trailing newlines removed. The sh syntax is
|
|---|
| 299 | accepted for backwards compatibility, but the \*Q$(...)\*U form
|
|---|
| 300 | is preferred because its quoting rules are much simpler and it
|
|---|
| 301 | is easier to nest.
|
|---|
| 302 | .PP
|
|---|
| 303 | The Bourne shell does not provide such features as brace expansion,
|
|---|
| 304 | the ability
|
|---|
| 305 | to define a variable and a function with the same name, local variables
|
|---|
| 306 | in shell functions, the ability to enable and disable individual
|
|---|
| 307 | builtins or write a function to replace a builtin, or a means to
|
|---|
| 308 | export a shell function to a child process.
|
|---|
| 309 | .PP
|
|---|
| 310 | Bash has closed
|
|---|
| 311 | a long-standing shell security hole by not using the
|
|---|
| 312 | .B $IFS
|
|---|
| 313 | variable to split each word read by the shell, but splitting only
|
|---|
| 314 | the results of expansion (ksh and the 4.4 BSD sh have fixed this
|
|---|
| 315 | as well). Useful behavior such as a means to abort
|
|---|
| 316 | execution of a script read with the \*Q.\*U command using the
|
|---|
| 317 | \fBreturn\fP builtin or automatically
|
|---|
| 318 | exporting variables in the shell's environment to children is also
|
|---|
| 319 | not present in the Bourne shell. Bash provides a much more powerful
|
|---|
| 320 | environment for both interactive use and programming.
|
|---|
| 321 | .NH 1
|
|---|
| 322 | Bash-specific Features
|
|---|
| 323 | .PP
|
|---|
| 324 | This section details a few of the features which make Bash unique.
|
|---|
| 325 | Most of them provide improved interactive use, but a few programming
|
|---|
| 326 | improvements are present as well. Full descriptions of these
|
|---|
| 327 | features can be found in the Bash documentation.
|
|---|
| 328 | .NH 2
|
|---|
| 329 | Startup Files
|
|---|
| 330 | .PP
|
|---|
| 331 | Bash executes startup files differently than other shells. The Bash
|
|---|
| 332 | behavior is a compromise between the csh principle of startup files
|
|---|
| 333 | with fixed names executed for each shell and the sh
|
|---|
| 334 | \*Qminimalist\*U behavior. An interactive instance of Bash started
|
|---|
| 335 | as a login shell reads and executes
|
|---|
| 336 | .I ~/.bash_profile
|
|---|
| 337 | (the file .bash_profile in the user's home directory), if it exists.
|
|---|
| 338 | An interactive non-login shell reads and executes
|
|---|
| 339 | .I ~/.bashrc .
|
|---|
| 340 | A non-interactive shell (one begun to execute a shell script, for
|
|---|
| 341 | example) reads no fixed startup file, but uses the value of the variable
|
|---|
| 342 | .B $ENV ,
|
|---|
| 343 | if set, as the name of a startup file. The ksh practice of reading
|
|---|
| 344 | .B $ENV
|
|---|
| 345 | for every shell, with the accompanying difficulty of defining the
|
|---|
| 346 | proper variables and functions for interactive and non-interactive
|
|---|
| 347 | shells or having the file read only for interactive shells, was
|
|---|
| 348 | considered too complex. Ease of use won out here. Interestingly,
|
|---|
| 349 | the next release of ksh will change to reading
|
|---|
| 350 | .B $ENV
|
|---|
| 351 | only for interactive shells.
|
|---|
| 352 | .NH 2
|
|---|
| 353 | New Builtin Commands
|
|---|
| 354 | .PP
|
|---|
| 355 | There are a few builtins which are new or have been extended in Bash.
|
|---|
| 356 | The
|
|---|
| 357 | .B enable
|
|---|
| 358 | builtin allows builtin commands to be turned on and off arbitrarily.
|
|---|
| 359 | To use the version of
|
|---|
| 360 | .I echo
|
|---|
| 361 | found in a user's search path rather than the Bash builtin,
|
|---|
| 362 | \f(CRenable -n echo\fP suffices. The
|
|---|
| 363 | .B help
|
|---|
| 364 | builtin provides
|
|---|
| 365 | quick synopses of the shell facilities without requiring
|
|---|
| 366 | access to a manual page.
|
|---|
| 367 | .B Builtin
|
|---|
| 368 | is similar to
|
|---|
| 369 | .B command
|
|---|
| 370 | in that it bypasses shell functions and directly executes builtin
|
|---|
| 371 | commands. Access to a csh-style stack of directories is provided
|
|---|
| 372 | via the
|
|---|
| 373 | .B pushd ,
|
|---|
| 374 | .B popd ,
|
|---|
| 375 | and
|
|---|
| 376 | .B dirs
|
|---|
| 377 | builtins.
|
|---|
| 378 | .B Pushd
|
|---|
| 379 | and
|
|---|
| 380 | .B popd
|
|---|
| 381 | insert and remove directories from the stack, respectively, and
|
|---|
| 382 | .B dirs
|
|---|
| 383 | lists the stack contents. On systems that allow fine-grained control
|
|---|
| 384 | of resources, the
|
|---|
| 385 | .B ulimit
|
|---|
| 386 | builtin can be used to tune these settings.
|
|---|
| 387 | .B Ulimit
|
|---|
| 388 | allows a user to control,
|
|---|
| 389 | among other things, whether core dumps are to be generated,
|
|---|
| 390 | how much memory the shell or a child process is allowed to allocate,
|
|---|
| 391 | and how large a file created by a child process can grow. The
|
|---|
| 392 | .B suspend
|
|---|
| 393 | command will stop the shell process when job control is active; most
|
|---|
| 394 | other shells do not allow themselves to be stopped like that.
|
|---|
| 395 | .B Type,
|
|---|
| 396 | the Bash answer to
|
|---|
| 397 | .B which
|
|---|
| 398 | and
|
|---|
| 399 | .B whence,
|
|---|
| 400 | shows what will happen when a word is typed as a command:
|
|---|
| 401 | .SE
|
|---|
| 402 | $ type export
|
|---|
| 403 | export is a shell builtin
|
|---|
| 404 | $ type -t export
|
|---|
| 405 | builtin
|
|---|
| 406 | $ type bash
|
|---|
| 407 | bash is /bin/bash
|
|---|
| 408 | $ type cd
|
|---|
| 409 | cd is a function
|
|---|
| 410 | cd ()
|
|---|
| 411 | {
|
|---|
| 412 | builtin cd ${1+"$@"} && xtitle $HOST: $PWD
|
|---|
| 413 | }
|
|---|
| 414 | .EE
|
|---|
| 415 | .LP
|
|---|
| 416 | Various
|
|---|
| 417 | modes tell what a command word is (reserved word, alias, function, builtin,
|
|---|
| 418 | or file) or which version of a command will be executed based on
|
|---|
| 419 | a user's search path. Some of this functionality has been adopted
|
|---|
| 420 | by POSIX.2 and folded into the
|
|---|
| 421 | .B command
|
|---|
| 422 | utility.
|
|---|
| 423 | .NH 2
|
|---|
| 424 | Editing and Completion
|
|---|
| 425 | .PP
|
|---|
| 426 | One area in which Bash shines is command line editing. Bash uses the
|
|---|
| 427 | .I readline
|
|---|
| 428 | library to read and edit lines when interactive. Readline is a
|
|---|
| 429 | powerful and flexible input facility that a user can configure to
|
|---|
| 430 | individual tastes. It allows lines to be edited using either emacs
|
|---|
| 431 | or vi commands, where those commands are appropriate. The full
|
|---|
| 432 | capability of emacs is not present \- there is no way to execute
|
|---|
| 433 | a named command with M-x, for instance \- but the existing commands
|
|---|
| 434 | are more than adequate. The vi mode is compliant with
|
|---|
| 435 | the command line editing standardized by POSIX.2.
|
|---|
| 436 | .PP
|
|---|
| 437 | Readline is fully customizable. In addition to the basic commands
|
|---|
| 438 | and key bindings, the library allows users to define additional
|
|---|
| 439 | key bindings using a startup file. The
|
|---|
| 440 | .I inputrc
|
|---|
| 441 | file, which defaults to the file
|
|---|
| 442 | .I ~/.inputrc ,
|
|---|
| 443 | is read each time readline initializes, permitting users to
|
|---|
| 444 | maintain a consistent interface across a set of programs. Readline
|
|---|
| 445 | includes an extensible interface, so each program using the
|
|---|
| 446 | library can add its own bindable commands and program-specific
|
|---|
| 447 | key bindings. Bash uses this facility to add bindings
|
|---|
| 448 | that perform history expansion or shell word expansions on the current
|
|---|
| 449 | input line.
|
|---|
| 450 | .PP
|
|---|
| 451 | Readline interprets a number of
|
|---|
| 452 | variables which further tune its behavior. Variables
|
|---|
| 453 | exist to control whether or not eight-bit characters are directly
|
|---|
| 454 | read as input or converted to meta-prefixed key sequences (a
|
|---|
| 455 | meta-prefixed key sequence consists of the character with the
|
|---|
| 456 | eighth bit zeroed, preceded by the
|
|---|
| 457 | .I meta-prefix
|
|---|
| 458 | character, usually escape, which selects an alternate keymap), to
|
|---|
| 459 | decide whether to output characters with the eighth bit set
|
|---|
| 460 | directly or as a meta-prefixed key sequence, whether or not to
|
|---|
| 461 | wrap to a new screen line when a line being edited is longer than
|
|---|
| 462 | the screen width, the keymap to which subsequent key bindings should
|
|---|
| 463 | apply, or even what happens when readline wants to
|
|---|
| 464 | ring the terminal's bell. All of these variables can be set in
|
|---|
| 465 | the inputrc file.
|
|---|
| 466 | .PP
|
|---|
| 467 | The startup file understands a set of C
|
|---|
| 468 | preprocessor-like conditional constructs which allow variables or
|
|---|
| 469 | key bindings to be assigned based on the application using readline,
|
|---|
| 470 | the terminal currently being used, or the editing mode. Users can
|
|---|
| 471 | add program-specific bindings to make their lives easier: I have
|
|---|
| 472 | bindings that let me edit the value of
|
|---|
| 473 | .B $PATH
|
|---|
| 474 | and double-quote the current or previous word:
|
|---|
| 475 | .SE
|
|---|
| 476 | # Macros that are convenient for shell interaction
|
|---|
| 477 | $if Bash
|
|---|
| 478 | # edit the path
|
|---|
| 479 | "\eC-xp": "PATH=${PATH}\ee\eC-e\eC-a\eef\eC-f"
|
|---|
| 480 | # prepare to type a quoted word -- insert open and close double
|
|---|
| 481 | # quotes and move to just after the open quote
|
|---|
| 482 | "\eC-x\e"": "\e"\e"\eC-b"
|
|---|
| 483 | # Quote the current or previous word
|
|---|
| 484 | "\eC-xq": "\eeb\e"\eef\e""
|
|---|
| 485 | $endif
|
|---|
| 486 | .EE
|
|---|
| 487 | .LP
|
|---|
| 488 | There is a readline
|
|---|
| 489 | command to re-read the file, so users can edit the file, change
|
|---|
| 490 | some bindings, and begin to use them almost immediately.
|
|---|
| 491 | .PP
|
|---|
| 492 | Bash implements the
|
|---|
| 493 | .B bind
|
|---|
| 494 | builtin for more dyamic control of readline than the startup file
|
|---|
| 495 | permits.
|
|---|
| 496 | .B Bind
|
|---|
| 497 | is used in several ways. In
|
|---|
| 498 | .I list
|
|---|
| 499 | mode, it can display the current key bindings, list all the
|
|---|
| 500 | readline editing directives available for binding, list which keys
|
|---|
| 501 | invoke a given directive, or output the current set of key
|
|---|
| 502 | bindings in a format that can be incorporated directly into an inputrc
|
|---|
| 503 | file. In
|
|---|
| 504 | .I batch
|
|---|
| 505 | mode, it reads a series of key bindings directly from a file and
|
|---|
| 506 | passes them to readline. In its most common usage,
|
|---|
| 507 | .B bind
|
|---|
| 508 | takes a single string and passes it directly to readline, which
|
|---|
| 509 | interprets the line as if it had just been read from the inputrc file.
|
|---|
| 510 | Both key bindings and variable assignments may appear in the
|
|---|
| 511 | string given to
|
|---|
| 512 | .B bind .
|
|---|
| 513 | .PP
|
|---|
| 514 | The readline library also provides an interface for \fIword completion\fP.
|
|---|
| 515 | When the
|
|---|
| 516 | .I completion
|
|---|
| 517 | character (usually TAB) is typed, readline looks at the word currently
|
|---|
| 518 | being entered and computes the set of filenames of which the current
|
|---|
| 519 | word is a valid prefix.
|
|---|
| 520 | If there is only one possible completion, the
|
|---|
| 521 | rest of the characters are inserted directly, otherwise the
|
|---|
| 522 | common prefix of the set of filenames is added to the current word.
|
|---|
| 523 | A second TAB character entered immediately after a non-unique
|
|---|
| 524 | completion causes readline to list the possible completions; there is
|
|---|
| 525 | an option to have the list displayed immediately.
|
|---|
| 526 | Readline provides hooks so that applications can provide specific types
|
|---|
| 527 | of completion before the default filename completion is attempted.
|
|---|
| 528 | This is quite flexible, though it is not completely user-programmable.
|
|---|
| 529 | Bash, for example, can complete filenames, command names (including aliases,
|
|---|
| 530 | builtins, shell reserved words, shell functions, and executables found
|
|---|
| 531 | in the file system), shell variables, usernames, and hostnames. It
|
|---|
| 532 | uses a set of heuristics that, while not perfect, is generally quite
|
|---|
| 533 | good at determining what type of completion to attempt.
|
|---|
| 534 | .NH 2
|
|---|
| 535 | History
|
|---|
| 536 | .PP
|
|---|
| 537 | Access to the list of commands previously entered (the \fIcommand history\fP)
|
|---|
| 538 | is provided jointly by Bash and the readline library. Bash provides
|
|---|
| 539 | variables (\fB$HISTFILE\fP, \fB$HISTSIZE\fP, and \fB$HISTCONTROL\fP)
|
|---|
| 540 | and the
|
|---|
| 541 | .B history
|
|---|
| 542 | and
|
|---|
| 543 | .B fc
|
|---|
| 544 | builtins to manipulate the history list.
|
|---|
| 545 | The value of
|
|---|
| 546 | .B $HISTFILE
|
|---|
| 547 | specifes the file where Bash writes the command history on exit and
|
|---|
| 548 | reads it on startup.
|
|---|
| 549 | .B $HISTSIZE
|
|---|
| 550 | is used to limit the number of commands saved in the history.
|
|---|
| 551 | .B $HISTCONTROL
|
|---|
| 552 | provides a crude form of control over which commands are saved on
|
|---|
| 553 | the history list: a value of
|
|---|
| 554 | .I ignorespace
|
|---|
| 555 | means to not save commands which begin with a space; a value of
|
|---|
| 556 | .I ignoredups
|
|---|
| 557 | means to not save commands identical to the last command saved.
|
|---|
| 558 | \fB$HISTCONTROL\fP was named \fB$history_control\fP in earlier
|
|---|
| 559 | versions of Bash; the old name is still accepted for backwards
|
|---|
| 560 | compatibility. The
|
|---|
| 561 | .B history
|
|---|
| 562 | command can read or write files containing the history list
|
|---|
| 563 | and display the current list contents. The
|
|---|
| 564 | .B fc
|
|---|
| 565 | builtin, adopted from POSIX.2 and the Korn Shell, allows display
|
|---|
| 566 | and re-execution, with optional editing,
|
|---|
| 567 | of commands from the history list. The readline
|
|---|
| 568 | library offers a set of commands to search the history list for
|
|---|
| 569 | a portion of the current input line or a string typed by the user.
|
|---|
| 570 | Finally, the
|
|---|
| 571 | .I history
|
|---|
| 572 | library, generally incorporated directly into the readline library,
|
|---|
| 573 | implements a facility for history recall, expansion, and re-execution
|
|---|
| 574 | of previous commands very similar to csh
|
|---|
| 575 | (\*Qbang history\*U, so called because the exclamation point
|
|---|
| 576 | introduces a history substitution):
|
|---|
| 577 | .SE
|
|---|
| 578 | $ echo a b c d e
|
|---|
| 579 | a b c d e
|
|---|
| 580 | $ !! f g h i
|
|---|
| 581 | echo a b c d e f g h i
|
|---|
| 582 | a b c d e f g h i
|
|---|
| 583 | $ !-2
|
|---|
| 584 | echo a b c d e
|
|---|
| 585 | a b c d e
|
|---|
| 586 | $ echo !-2:1-4
|
|---|
| 587 | echo a b c d
|
|---|
| 588 | a b c d
|
|---|
| 589 | .EE
|
|---|
| 590 | .LP
|
|---|
| 591 | The command history is only
|
|---|
| 592 | saved when the shell is interactive, so it is not available for use
|
|---|
| 593 | by shell scripts.
|
|---|
| 594 | .NH 2
|
|---|
| 595 | New Shell Variables
|
|---|
| 596 | .PP
|
|---|
| 597 | There are a number of convenience variables that Bash interprets
|
|---|
| 598 | to make life easier. These include
|
|---|
| 599 | .B FIGNORE ,
|
|---|
| 600 | which is a set of filename suffixes identifying files to exclude when
|
|---|
| 601 | completing filenames;
|
|---|
| 602 | .B HOSTTYPE ,
|
|---|
| 603 | which is automatically set to a string describing the type of
|
|---|
| 604 | hardware on which Bash is currently executing;
|
|---|
| 605 | .B command_oriented_history ,
|
|---|
| 606 | which directs Bash to save all lines of a multiple-line
|
|---|
| 607 | command such as a \fIwhile\fP or \fIfor\fP loop in a single
|
|---|
| 608 | history entry, allowing easy re-editing; and
|
|---|
| 609 | .B IGNOREEOF ,
|
|---|
| 610 | whose value indicates the number of consecutive EOF characters that
|
|---|
| 611 | an interactive shell will read before exiting \- an easy way to keep
|
|---|
| 612 | yourself from being logged out accidentally. The
|
|---|
| 613 | .B auto_resume
|
|---|
| 614 | variable alters the way the shell treats simple command names:
|
|---|
| 615 | if job control is active, and this variable is set, single-word
|
|---|
| 616 | simple commands without redirections cause the shell to first
|
|---|
| 617 | look for and restart a suspended job with that name before
|
|---|
| 618 | starting a new process.
|
|---|
| 619 | .NH 2
|
|---|
| 620 | Brace Expansion
|
|---|
| 621 | .PP
|
|---|
| 622 | Since sh offers no convenient way to generate arbitrary strings that
|
|---|
| 623 | share a common prefix or suffix (filename expansion requires that
|
|---|
| 624 | the filenames exist), Bash implements \fIbrace expansion\fP, a
|
|---|
| 625 | capability picked up from csh.
|
|---|
| 626 | Brace expansion is similar to filename expansion, but the strings
|
|---|
| 627 | generated need not correspond to existing files. A brace expression
|
|---|
| 628 | consists of an optional
|
|---|
| 629 | .I preamble ,
|
|---|
| 630 | followed by a pair of braces enclosing a series of comma-separated
|
|---|
| 631 | strings, and an optional
|
|---|
| 632 | .I postamble .
|
|---|
| 633 | The preamble is prepended to each string within the braces, and the
|
|---|
| 634 | postamble is then appended to each resulting string:
|
|---|
| 635 | .SE
|
|---|
| 636 | $ echo a{d,c,b}e
|
|---|
| 637 | ade ace abe
|
|---|
| 638 | .EE
|
|---|
| 639 | .LP
|
|---|
| 640 | As this example demonstrates, the results of brace expansion are not
|
|---|
| 641 | sorted, as they are by filename expansion.
|
|---|
| 642 | .NH 2
|
|---|
| 643 | Process Substitution
|
|---|
| 644 | .PP
|
|---|
| 645 | On systems that can support it, Bash provides a facility known as
|
|---|
| 646 | \fIprocess substitution\fP. Process substitution is similar to command
|
|---|
| 647 | substitution in that its specification includes a command to execute,
|
|---|
| 648 | but the shell does not collect the command's output and insert it into
|
|---|
| 649 | the command line. Rather, Bash opens a pipe to the command, which
|
|---|
| 650 | is run in the background. The shell uses named pipes (FIFOs) or the
|
|---|
| 651 | .I /dev/fd
|
|---|
| 652 | method of naming open files to expand the process
|
|---|
| 653 | substitution to a filename which connects to the pipe when opened.
|
|---|
| 654 | This filename becomes the result of the expansion. Process substitution
|
|---|
| 655 | can be used to compare the outputs of two different versions of an
|
|---|
| 656 | application as part of a regression test:
|
|---|
| 657 | .SE
|
|---|
| 658 | $ cmp <(old_prog) <(new_prog)
|
|---|
| 659 | .EE
|
|---|
| 660 | .NH 2
|
|---|
| 661 | Prompt Customization
|
|---|
| 662 | .PP
|
|---|
| 663 | One of the more popular interactive features that Bash provides is
|
|---|
| 664 | the ability to customize the prompt. Both
|
|---|
| 665 | .B $PS1
|
|---|
| 666 | and
|
|---|
| 667 | .B $PS2,
|
|---|
| 668 | the primary and secondary prompts, are expanded before being
|
|---|
| 669 | displayed. Parameter and variable expansion is performed when
|
|---|
| 670 | the prompt string is expanded, so any shell variable can be
|
|---|
| 671 | put into the prompt (e.g.,
|
|---|
| 672 | .B $SHLVL ,
|
|---|
| 673 | which indicates how deeply the current shell is nested).
|
|---|
| 674 | Bash specially interprets characters in the prompt string
|
|---|
| 675 | preceded by a backslash. Some of these backslash escapes are
|
|---|
| 676 | replaced with
|
|---|
| 677 | the current time, the date, the current working directory,
|
|---|
| 678 | the username, and the command number or history number of the command
|
|---|
| 679 | being entered. There is even a backslash escape to cause the shell
|
|---|
| 680 | to change its prompt when running as root after an \fIsu\fP.
|
|---|
| 681 | Before printing each primary prompt, Bash expands the variable
|
|---|
| 682 | .B $PROMPT_COMMAND
|
|---|
| 683 | and, if it has a value, executes the expanded value as a command,
|
|---|
| 684 | allowing additional prompt customization. For example, this assignment
|
|---|
| 685 | causes the current user, the current host, the time, the last
|
|---|
| 686 | component of the current working directory, the level of shell
|
|---|
| 687 | nesting, and the history number of the current command to be embedded
|
|---|
| 688 | into the primary prompt:
|
|---|
| 689 | .SE
|
|---|
| 690 | $ PS1='\eu@\eh [\et] \eW($SHLVL:\e!)\e$ '
|
|---|
| 691 | chet@odin [21:03:44] documentation(2:636)$ cd ..
|
|---|
| 692 | chet@odin [21:03:54] src(2:637)$
|
|---|
| 693 | .EE
|
|---|
| 694 | .LP
|
|---|
| 695 | The string being assigned is surrounded by single quotes so that if
|
|---|
| 696 | it is exported, the value of
|
|---|
| 697 | .B $SHLVL
|
|---|
| 698 | will be updated by a child shell:
|
|---|
| 699 | .SE
|
|---|
| 700 | chet@odin [21:17:35] src(2:638)$ export PS1
|
|---|
| 701 | chet@odin [21:17:40] src(2:639)$ bash
|
|---|
| 702 | chet@odin [21:17:46] src(3:696)$
|
|---|
| 703 | .EE
|
|---|
| 704 | .LP
|
|---|
| 705 | The \fP\e$\fP escape is displayed
|
|---|
| 706 | as \*Q\fB$\fP\*U when running as a normal user, but as \*Q\fB#\fP\*U when
|
|---|
| 707 | running as root.
|
|---|
| 708 | .NH 2
|
|---|
| 709 | File System Views
|
|---|
| 710 | .PP
|
|---|
| 711 | Since Berkeley introduced symbolic links in 4.2 BSD, one of their most
|
|---|
| 712 | annoying properties has been the \*Qwarping\*U to a completely
|
|---|
| 713 | different area of the file system when using
|
|---|
| 714 | .B cd ,
|
|---|
| 715 | and the resultant non-intuitive behavior of \*Q\fBcd ..\fP\*U.
|
|---|
| 716 | The \s-1UNIX\s+1 kernel treats symbolic links
|
|---|
| 717 | .I physically .
|
|---|
| 718 | When the kernel is translating a pathname
|
|---|
| 719 | in which one component is a symbolic link, it replaces all or part
|
|---|
| 720 | of the pathname while processing the link. If the contents of the symbolic
|
|---|
| 721 | link begin with a slash, the kernel replaces the
|
|---|
| 722 | pathname entirely; if not, the link contents replace
|
|---|
| 723 | the current component. In either case, the symbolic link
|
|---|
| 724 | is visible. If the link value is an absolute pathname,
|
|---|
| 725 | the user finds himself in a completely different part of the file
|
|---|
| 726 | system.
|
|---|
| 727 | .PP
|
|---|
| 728 | Bash provides a
|
|---|
| 729 | .I logical
|
|---|
| 730 | view of the file system. In this default mode, command and filename
|
|---|
| 731 | completion and builtin commands such as
|
|---|
| 732 | .B cd
|
|---|
| 733 | and
|
|---|
| 734 | .B pushd
|
|---|
| 735 | which change the current working directory transparently follow
|
|---|
| 736 | symbolic links as if they were directories.
|
|---|
| 737 | The
|
|---|
| 738 | .B $PWD
|
|---|
| 739 | variable, which holds the shell's idea of the current working directory,
|
|---|
| 740 | depends on the path used to reach the directory rather than its
|
|---|
| 741 | physical location in the local file system hierarchy. For example:
|
|---|
| 742 | .SE
|
|---|
| 743 | $ cd /usr/local/bin
|
|---|
| 744 | $ echo $PWD
|
|---|
| 745 | /usr/local/bin
|
|---|
| 746 | $ pwd
|
|---|
| 747 | /usr/local/bin
|
|---|
| 748 | $ /bin/pwd
|
|---|
| 749 | /net/share/sun4/local/bin
|
|---|
| 750 | $ cd ..
|
|---|
| 751 | $ pwd
|
|---|
| 752 | /usr/local
|
|---|
| 753 | $ /bin/pwd
|
|---|
| 754 | /net/share/sun4/local
|
|---|
| 755 | $ cd ..
|
|---|
| 756 | $ pwd
|
|---|
| 757 | /usr
|
|---|
| 758 | $ /bin/pwd
|
|---|
| 759 | /usr
|
|---|
| 760 | .EE
|
|---|
| 761 | .LP
|
|---|
| 762 | One problem with this, of
|
|---|
| 763 | course, arises when programs that do not understand the shell's logical
|
|---|
| 764 | notion of the file system interpret \*Q..\*U differently. This generally
|
|---|
| 765 | happens when Bash completes filenames containing \*Q..\*U according to a
|
|---|
| 766 | logical hierarchy which does not correspond to their physical location.
|
|---|
| 767 | For users who find this troublesome, a corresponding
|
|---|
| 768 | .I physical
|
|---|
| 769 | view of the file system is available:
|
|---|
| 770 | .SE
|
|---|
| 771 | $ cd /usr/local/bin
|
|---|
| 772 | $ pwd
|
|---|
| 773 | /usr/local/bin
|
|---|
| 774 | $ set -o physical
|
|---|
| 775 | $ pwd
|
|---|
| 776 | /net/share/sun4/local/bin
|
|---|
| 777 | .EE
|
|---|
| 778 | .NH 2
|
|---|
| 779 | Internationalization
|
|---|
| 780 | .PP
|
|---|
| 781 | One of the most significant improvements in version 1.13 of Bash was the
|
|---|
| 782 | change to \*Qeight-bit cleanliness\*U. Previous versions used the
|
|---|
| 783 | eighth bit of characters to mark whether or not they were
|
|---|
| 784 | quoted when performing word expansions. While this did not affect
|
|---|
| 785 | the majority of users, most of whom used only seven-bit ASCII characters,
|
|---|
| 786 | some found it confining. Beginning with version 1.13, Bash
|
|---|
| 787 | implemented a different quoting mechanism that did not alter the
|
|---|
| 788 | eighth bit of characters. This allowed Bash
|
|---|
| 789 | to manipulate files with \*Qodd\*U characters in their names, but
|
|---|
| 790 | did nothing to help users enter those names, so
|
|---|
| 791 | version 1.13 introduced changes to readline that
|
|---|
| 792 | made it eight-bit clean as well. Options exist that force readline to
|
|---|
| 793 | attach no special significance to characters with the eighth bit set
|
|---|
| 794 | (the default behavior is to convert these characters to meta-prefixed
|
|---|
| 795 | key sequences) and to output these characters without conversion to
|
|---|
| 796 | meta-prefixed sequences. These changes, along with the expansion of
|
|---|
| 797 | keymaps to a full eight bits, enable readline to work with most of the
|
|---|
| 798 | ISO-8859 family of character sets, used by many European countries.
|
|---|
| 799 | .NH 2
|
|---|
| 800 | POSIX Mode
|
|---|
| 801 | .PP
|
|---|
| 802 | Although Bash is intended to be POSIX.2 conformant, there are areas in
|
|---|
| 803 | which the default behavior is not compatible with the standard. For
|
|---|
| 804 | users who wish to operate in a strict POSIX.2 environment, Bash
|
|---|
| 805 | implements a \fIPOSIX mode\fP. When this mode is active, Bash modifies
|
|---|
| 806 | its default operation where it differs from POSIX.2 to match the
|
|---|
| 807 | standard. POSIX mode is entered when Bash is started with the
|
|---|
| 808 | .B -posix
|
|---|
| 809 | option. This feature is also available as an option to the
|
|---|
| 810 | \fBset\fP builtin, \fBset -o posix\fP.
|
|---|
| 811 | For compatibility with other GNU software that attempts to be POSIX.2
|
|---|
| 812 | compliant, Bash also enters POSIX mode if the variable
|
|---|
| 813 | .B $POSIXLY_CORRECT
|
|---|
| 814 | is set when Bash is started or assigned a value during execution.
|
|---|
| 815 | .B $POSIX_PEDANTIC
|
|---|
| 816 | is accepted as well, to be compatible with some older GNU utilities.
|
|---|
| 817 | When Bash is started in POSIX mode, for example, it sources the
|
|---|
| 818 | file named by the value of
|
|---|
| 819 | .B $ENV
|
|---|
| 820 | rather than the \*Qnormal\*U startup files, and does not allow
|
|---|
| 821 | reserved words to be aliased.
|
|---|
| 822 | .NH 1
|
|---|
| 823 | New Features and Future Plans
|
|---|
| 824 | .PP
|
|---|
| 825 | There are several features introduced in the current
|
|---|
| 826 | version of Bash, version 1.14, and a number under consideration
|
|---|
| 827 | for future releases. This section will briefly detail the new
|
|---|
| 828 | features in version 1.14 and describe several features
|
|---|
| 829 | that may appear in later versions.
|
|---|
| 830 | .NH 2
|
|---|
| 831 | New Features in Bash-1.14
|
|---|
| 832 | .PP
|
|---|
| 833 | The new features available in Bash-1.14 answer several of
|
|---|
| 834 | the most common requests for enhancements. Most notably, there
|
|---|
| 835 | is a mechanism
|
|---|
| 836 | for including non-visible character sequences in prompts, such as
|
|---|
| 837 | those which cause a terminal to print characters in different
|
|---|
| 838 | colors or in standout mode. There was nothing preventing the use
|
|---|
| 839 | of these sequences in earlier
|
|---|
| 840 | versions, but the readline redisplay algorithm assumed each
|
|---|
| 841 | character occupied physical screen space and would wrap lines
|
|---|
| 842 | prematurely.
|
|---|
| 843 | .PP
|
|---|
| 844 | Readline has a few new
|
|---|
| 845 | variables, several new bindable commands, and some additional
|
|---|
| 846 | emacs mode default key bindings. A new history search
|
|---|
| 847 | mode has been implemented: in this mode, readline searches the
|
|---|
| 848 | history for lines beginning with the characters between the
|
|---|
| 849 | beginning of the current line and the cursor. The existing readline
|
|---|
| 850 | incremental search commands no longer match identical lines more
|
|---|
| 851 | than once.
|
|---|
| 852 | Filename completion now expands variables in directory names.
|
|---|
| 853 | The history expansion facilities are now nearly
|
|---|
| 854 | completely csh-compatible: missing modifiers have been added and
|
|---|
| 855 | history substitution has been extended.
|
|---|
| 856 | .PP
|
|---|
| 857 | Several of the features described earlier, such as
|
|---|
| 858 | .B "set -o posix"
|
|---|
| 859 | and
|
|---|
| 860 | .B $POSIX_PEDANTIC ,
|
|---|
| 861 | are new in version 1.14.
|
|---|
| 862 | There is a new shell variable,
|
|---|
| 863 | .B OSTYPE ,
|
|---|
| 864 | to which Bash assigns a value that identifies the
|
|---|
| 865 | version of \s-1UNIX\s+1 it's
|
|---|
| 866 | running on (great for putting architecture-specific binary directories
|
|---|
| 867 | into the \fB$PATH\fP).
|
|---|
| 868 | Two variables have been renamed:
|
|---|
| 869 | .B $HISTCONTROL
|
|---|
| 870 | replaces
|
|---|
| 871 | .B $history_control ,
|
|---|
| 872 | and
|
|---|
| 873 | .B $HOSTFILE
|
|---|
| 874 | replaces
|
|---|
| 875 | .B $hostname_completion_file .
|
|---|
| 876 | In both cases, the old names are accepted for backwards
|
|---|
| 877 | compatibility. The ksh
|
|---|
| 878 | .I select
|
|---|
| 879 | construct, which allows the generation of simple menus,
|
|---|
| 880 | has been implemented. New capabilities have been added
|
|---|
| 881 | to existing variables:
|
|---|
| 882 | .B $auto_resume
|
|---|
| 883 | can now take values of
|
|---|
| 884 | .I exact
|
|---|
| 885 | or
|
|---|
| 886 | .I substring ,
|
|---|
| 887 | and
|
|---|
| 888 | .B $HISTCONTROL
|
|---|
| 889 | understands the value
|
|---|
| 890 | .I ignoreboth ,
|
|---|
| 891 | which combines the two previously acceptable values. The
|
|---|
| 892 | .B dirs
|
|---|
| 893 | builtin has acquired options to print out specific members of the
|
|---|
| 894 | directory stack. The
|
|---|
| 895 | .B $nolinks
|
|---|
| 896 | variable, which forces a physical view of the file system,
|
|---|
| 897 | has been superseded by the
|
|---|
| 898 | .B \-P
|
|---|
| 899 | option to the
|
|---|
| 900 | .B set
|
|---|
| 901 | builtin (equivalent to \fBset -o physical\fP); the variable is retained
|
|---|
| 902 | for backwards compatibility. The version string contained in
|
|---|
| 903 | .B $BASH_VERSION
|
|---|
| 904 | now includes an indication of the patch level as well as the
|
|---|
| 905 | \*Qbuild version\*U.
|
|---|
| 906 | Some little-used features have
|
|---|
| 907 | been removed: the
|
|---|
| 908 | .B bye
|
|---|
| 909 | synonym for
|
|---|
| 910 | .B exit
|
|---|
| 911 | and the
|
|---|
| 912 | .B $NO_PROMPT_VARS
|
|---|
| 913 | variable are gone. There is now an organized test suite that can be
|
|---|
| 914 | run as a regression test when building a new version of Bash.
|
|---|
| 915 | .PP
|
|---|
| 916 | The documentation has been thoroughly overhauled:
|
|---|
| 917 | there is a new manual page on the readline library and the \fIinfo\fP
|
|---|
| 918 | file has been updated to reflect the current version.
|
|---|
| 919 | As always, as many bugs as possible have been fixed, although some
|
|---|
| 920 | surely remain.
|
|---|
| 921 | .NH 2
|
|---|
| 922 | Other Features
|
|---|
| 923 | .PP
|
|---|
| 924 | There are a few features that I hope to include in later Bash releases.
|
|---|
| 925 | Some are based on work already done in other shells.
|
|---|
| 926 | .PP
|
|---|
| 927 | In addition to simple variables, a future release of Bash will include
|
|---|
| 928 | one-dimensional arrays, using the ksh
|
|---|
| 929 | implementation of arrays as a model. Additions to the ksh syntax,
|
|---|
| 930 | such as \fIvarname\fP=( ... ) to assign a list of words directly to
|
|---|
| 931 | an array and a mechanism to allow
|
|---|
| 932 | the
|
|---|
| 933 | .B read
|
|---|
| 934 | builtin to read a list of values directly into an array, would be
|
|---|
| 935 | desirable. Given those extensions, the ksh
|
|---|
| 936 | .B "set \-A"
|
|---|
| 937 | syntax may not be worth supporting (the
|
|---|
| 938 | .B \-A
|
|---|
| 939 | option assigns a list of values to an array, but is a rather
|
|---|
| 940 | peculiar special case).
|
|---|
| 941 | .PP
|
|---|
| 942 | Some shells include a means of \fIprogrammable\fP word
|
|---|
| 943 | completion, where the user specifies on a per-command basis how the
|
|---|
| 944 | arguments of the command are to be treated when completion is attempted:
|
|---|
| 945 | as filenames, hostnames, executable files, and so on. The other
|
|---|
| 946 | aspects of the current Bash implementation could remain as-is; the
|
|---|
| 947 | existing heuristics would still be valid. Only when completing the
|
|---|
| 948 | arguments to a simple command would the programmable completion be
|
|---|
| 949 | in effect.
|
|---|
| 950 | .PP
|
|---|
| 951 | It would also be nice to give the user finer-grained
|
|---|
| 952 | control over which commands are saved onto the history list. One
|
|---|
| 953 | proposal is for a variable, tentatively named
|
|---|
| 954 | .B HISTIGNORE ,
|
|---|
| 955 | which would contain a colon-separated list of commands. Lines beginning
|
|---|
| 956 | with these commands, after the restrictions of
|
|---|
| 957 | .B $HISTCONTROL
|
|---|
| 958 | have been applied, would not be placed onto the history list. The
|
|---|
| 959 | shell pattern-matching capabilities could also be available when
|
|---|
| 960 | specifying the contents of
|
|---|
| 961 | .B $HISTIGNORE .
|
|---|
| 962 | .PP
|
|---|
| 963 | One thing that newer shells such as
|
|---|
| 964 | .B wksh
|
|---|
| 965 | (also known as
|
|---|
| 966 | .B dtksh )
|
|---|
| 967 | provide is a command to dynamically load code
|
|---|
| 968 | implementing additional builtin commands into a running shell.
|
|---|
| 969 | This new builtin would take an object file or shared library
|
|---|
| 970 | implementing the \*Qbody\*U of the
|
|---|
| 971 | builtin (\fIxxx_builtin()\fP for those familiar with Bash internals)
|
|---|
| 972 | and a structure containing the name of the new command, the function
|
|---|
| 973 | to call when the new builtin is invoked (presumably defined in the
|
|---|
| 974 | shared object specified as an argument), and the documentation to be
|
|---|
| 975 | printed by the
|
|---|
| 976 | .B help
|
|---|
| 977 | command (possibly present in the shared object as well). It would
|
|---|
| 978 | manage the details of extending the internal table of builtins.
|
|---|
| 979 | .PP
|
|---|
| 980 | A few other builtins would also be desirable: two are the POSIX.2
|
|---|
| 981 | .B getconf
|
|---|
| 982 | command, which prints the values of system configuration variables
|
|---|
| 983 | defined by POSIX.2, and a
|
|---|
| 984 | .B disown
|
|---|
| 985 | builtin, which causes a shell running
|
|---|
| 986 | with job control active to \*Qforget about\*U one or more
|
|---|
| 987 | background jobs in its internal jobs table. Using
|
|---|
| 988 | .B getconf ,
|
|---|
| 989 | for example, a user could retrieve a value for
|
|---|
| 990 | .B $PATH
|
|---|
| 991 | guaranteed to find all of the POSIX standard utilities, or
|
|---|
| 992 | find out how long filenames may be in the file system containing
|
|---|
| 993 | a specified directory.
|
|---|
| 994 | .PP
|
|---|
| 995 | There are no implementation timetables for any of these features, nor
|
|---|
| 996 | are there concrete plans to include them. If anyone has comments on
|
|---|
| 997 | these proposals, feel free to send me electronic mail.
|
|---|
| 998 | .NH 1
|
|---|
| 999 | Reflections and Lessons Learned
|
|---|
| 1000 | .PP
|
|---|
| 1001 | The lesson that has been repeated most often during Bash
|
|---|
| 1002 | development is that there are dark corners in the Bourne shell,
|
|---|
| 1003 | and people use all of them. In the original description of the
|
|---|
| 1004 | Bourne shell, quoting and the shell grammar are both poorly
|
|---|
| 1005 | specified and incomplete; subsequent descriptions have not helped
|
|---|
| 1006 | much. The grammar presented in Bourne's paper describing
|
|---|
| 1007 | the shell distributed with the Seventh Edition of \s-1UNIX\s+1\(dg
|
|---|
| 1008 | is so far off that it does not allow the command \f(CWwho|wc\fP.
|
|---|
| 1009 | In fact, as Tom Duff states:
|
|---|
| 1010 | .QP
|
|---|
| 1011 | Nobody really knows what the
|
|---|
| 1012 | Bourne shell's grammar is. Even examination of the source code is
|
|---|
| 1013 | little help.\(dd
|
|---|
| 1014 | .FS
|
|---|
| 1015 | \(dgS. R. Bourne, \*QUNIX Time-Sharing System: The UNIX Shell\*U,
|
|---|
| 1016 | \fIBell System Technical Journal\fP, 57(6), July-August, 1978, pp. 1971-1990.
|
|---|
| 1017 | .FE
|
|---|
| 1018 | .FS
|
|---|
| 1019 | \(ddTom Duff, \*QRc \- A Shell for Plan 9 and \s-1UNIX\s+1 systems\*U,
|
|---|
| 1020 | \fIProc. of the Summer 1990 EUUG Conference\fP, London, July, 1990,
|
|---|
| 1021 | pp. 21-33.
|
|---|
| 1022 | .FE
|
|---|
| 1023 | .LP
|
|---|
| 1024 | The POSIX.2 standard includes a \fIyacc\fP grammar that comes close
|
|---|
| 1025 | to capturing the Bourne shell's behavior, but it disallows some
|
|---|
| 1026 | constructs which sh accepts without complaint \- and there are
|
|---|
| 1027 | scripts out there that use them. It took a few versions and
|
|---|
| 1028 | several bug reports before Bash implemented sh-compatible quoting,
|
|---|
| 1029 | and there are still some \*Qlegal\*U sh constructs which Bash flags as
|
|---|
| 1030 | syntax errors. Complete sh compatibility is a tough nut.
|
|---|
| 1031 | .PP
|
|---|
| 1032 | The shell is bigger and slower than I would like, though the current
|
|---|
| 1033 | version is substantially faster than previously. The readline library
|
|---|
| 1034 | could stand a substantial rewrite. A hand-written parser to replace
|
|---|
| 1035 | the current \fIyacc\fP-generated one would probably result in a speedup,
|
|---|
| 1036 | and would solve one glaring problem: the shell could parse
|
|---|
| 1037 | commands in \*Q$(...)\*U constructs
|
|---|
| 1038 | as they are entered, rather than reporting errors when the construct
|
|---|
| 1039 | is expanded.
|
|---|
| 1040 | .PP
|
|---|
| 1041 | As always, there is some chaff to go with the wheat.
|
|---|
| 1042 | Areas of duplicated functionality need to be cleaned
|
|---|
| 1043 | up. There are several cases where Bash treats a variable specially to
|
|---|
| 1044 | enable functionality available another way (\fB$notify\fP vs.
|
|---|
| 1045 | \fBset -o notify\fP and \fB$nolinks\fP vs. \fBset -o physical\fP, for
|
|---|
| 1046 | instance); the special treatment of the variable name should probably
|
|---|
| 1047 | be removed. A few more things could stand removal; the
|
|---|
| 1048 | .B $allow_null_glob_expansion
|
|---|
| 1049 | and
|
|---|
| 1050 | .B $glob_dot_filenames
|
|---|
| 1051 | variables are of particularly questionable value.
|
|---|
| 1052 | The \fB$[...]\fP arithmetic evaluation syntax is redundant now that
|
|---|
| 1053 | the POSIX-mandated \fB$((...))\fP construct has been implemented,
|
|---|
| 1054 | and could be deleted.
|
|---|
| 1055 | It would be nice if the text output by the
|
|---|
| 1056 | .B help
|
|---|
| 1057 | builtin were external to the shell rather than compiled into it.
|
|---|
| 1058 | The behavior enabled by
|
|---|
| 1059 | .B $command_oriented_history ,
|
|---|
| 1060 | which causes the shell to attempt to save all lines of a multi-line
|
|---|
| 1061 | command in a single history entry, should be made the default and
|
|---|
| 1062 | the variable removed.
|
|---|
| 1063 | .NH 1
|
|---|
| 1064 | Availability
|
|---|
| 1065 | .PP
|
|---|
| 1066 | As with all other
|
|---|
| 1067 | GNU software, Bash is available for anonymous FTP from
|
|---|
| 1068 | .I prep.ai.mit.edu:/pub/gnu
|
|---|
| 1069 | and from other GNU software mirror sites. The current version is in
|
|---|
| 1070 | .I bash-1.14.1.tar.gz
|
|---|
| 1071 | in that directory. Use
|
|---|
| 1072 | .I archie
|
|---|
| 1073 | to find the nearest archive site. The
|
|---|
| 1074 | latest version is always available for FTP from
|
|---|
| 1075 | .I bash.CWRU.Edu:/pub/dist.
|
|---|
| 1076 | Bash documentation is available for FTP from
|
|---|
| 1077 | .I bash.CWRU.Edu:/pub/bash.
|
|---|
| 1078 | .PP
|
|---|
| 1079 | The Free Software Foundation sells tapes and CD-ROMs
|
|---|
| 1080 | containing Bash; send electronic mail to
|
|---|
| 1081 | \f(CRgnu@prep.ai.mit.edu\fP or call \f(CR+1-617-876-3296\fP
|
|---|
| 1082 | for more information.
|
|---|
| 1083 | .PP
|
|---|
| 1084 | Bash is also distributed with several versions of \s-1UNIX\s+1-compatible
|
|---|
| 1085 | systems. It is included as /bin/sh and /bin/bash on several Linux
|
|---|
| 1086 | distributions (more about the difference in a moment), and as contributed
|
|---|
| 1087 | software in BSDI's BSD/386* and FreeBSD.
|
|---|
| 1088 | .FS
|
|---|
| 1089 | *BSD/386 is a trademark of Berkeley Software Design, Inc.
|
|---|
| 1090 | .FE
|
|---|
| 1091 | .PP
|
|---|
| 1092 | The Linux distribution deserves special mention. There are two
|
|---|
| 1093 | configurations included in the standard Bash distribution: a
|
|---|
| 1094 | \*Qnormal\*U configuration, in which all of the standard features
|
|---|
| 1095 | are included, and a \*Qminimal\*U configuration, which omits job
|
|---|
| 1096 | control, aliases, history and command line editing, the directory
|
|---|
| 1097 | stack and
|
|---|
| 1098 | .B pushd/popd/dirs,
|
|---|
| 1099 | process substitution, prompt string special character decoding, and the
|
|---|
| 1100 | .I select
|
|---|
| 1101 | construct. This minimal version is designed to be a drop-in replacement
|
|---|
| 1102 | for the traditional \s-1UNIX\s+1 /bin/sh, and is included as the Linux
|
|---|
| 1103 | /bin/sh in several packagings.
|
|---|
| 1104 | .NH 1
|
|---|
| 1105 | Conclusion
|
|---|
| 1106 | .PP
|
|---|
| 1107 | Bash is a worthy successor to sh.
|
|---|
| 1108 | It is sufficiently portable
|
|---|
| 1109 | to run on nearly every version of \s-1UNIX\s+1 from
|
|---|
| 1110 | 4.3 BSD to SVR4.2, and several \s-1UNIX\s+1 workalikes.
|
|---|
| 1111 | It is robust enough to replace sh on most of those systems,
|
|---|
| 1112 | and provides more functionality. It has several thousand regular users,
|
|---|
| 1113 | and their feedback has helped to make it as good as it is today \- a
|
|---|
| 1114 | testament to the benefits of free software.
|
|---|