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