d191cb4e16b738085f5252e26cd1d85a4e853b30
[platform/upstream/bash.git] / doc / FAQ
1 This is the Bash FAQ, version 2.13, for Bash version 2.02.
2
3 This document contains a set of frequently-asked questions concerning
4 Bash, the GNU Bourne-Again Shell.  Bash is a freely-available command
5 interpreter with advanced features for both interactive use and shell
6 programming.
7
8 Another good source of basic information about shells is the collection
9 of FAQ articles periodically posted to comp.unix.shell.
10
11 Questions and comments concerning this document should be sent to
12 chet@po.cwru.edu.
13
14 This document is available for anonymous FTP with the URL
15
16 ftp://ftp.cwru.edu/pub/bash/FAQ
17
18 ----------
19 Contents:
20
21 Section A:  The Basics
22
23 1) What is it?
24 2) What's the latest version?
25 3) Where can I get it?
26 4) On what machines will bash run?
27 5) Will bash run on operating systems other than Unix?
28 6) How can I build bash with gcc?
29 7) How can I make bash my login shell?
30 8) I just changed my login shell to bash, and now I can't FTP into my
31    machine.  Why not?
32 9) What's the `POSIX 1003.2 standard'?
33 10) What is the bash `posix mode'?
34
35 Section B:  The latest version
36
37 11) What's new in version 2.02?
38 12) Are there any user-visible incompatibilities between bash-2.02 and
39     bash-1.14.7?
40
41 Section C:  Differences from other Unix shells
42
43 13) How does bash differ from sh, the Bourne shell?
44 14) How does bash differ from the Korn shell, version ksh88?
45 15) Which new features in ksh-93 are not in bash, and which are?
46
47 Section D:  Why does bash do some things differently than other Unix shells?
48
49 16) Why does bash run a different version of `command' than
50     `which command' says it will?
51 17) Why doesn't bash treat brace expansions exactly like csh?
52 18) Why doesn't bash have csh variable modifiers?
53 19) How can I make my csh aliases work when I convert to bash?
54 20) How can I pipe standard output and standard error from one command to
55     another, like csh does with `|&'?
56 21) Now that I've converted from ksh to bash, are there equivalents to
57     ksh features like autoloaded functions and the `whence' command?
58
59 Section E:  How can I get bash to do certain things, and why does bash do
60             things the way it does?
61
62 22) Why is the bash builtin `test' slightly different from /bin/test?
63 23) Why does bash sometimes say `Broken pipe'?
64 24) How can I get bash to read and display eight-bit characters?
65 25) How do I write a function `x' to replace builtin command `x', but
66     still invoke the command from within the function?
67 26) When I have terminal escape sequences in my prompt, why does bash
68     wrap lines at the wrong column?
69 27) How can I find the value of a shell variable whose name is the value
70     of another shell variable?
71 28) If I pipe the output of a command into `read variable', why doesn't
72     the output show up in $variable when the read command finishes?
73 29) I have a bunch of shell scripts that use backslash-escaped characters
74     in arguments to `echo'.  Bash doesn't interpret these characters.  Why
75     not, and how can I make it understand them?
76 30) Why doesn't a while or for loop get suspended when I type ^Z?
77 31) How can I make the bash `time' reserved word print timing output that
78     looks like the output from my system's /usr/bin/time?
79
80 Section F:  Things to watch out for on certain Unix versions
81
82 32) Why can't I use command line editing in my `cmdtool'?
83 33) I built bash on Solaris 2.  Why do globbing expansions and filename
84     completion chop off the first few characters of each filename?
85 34) Why does bash dump core after I interrupt username completion or
86     `~user' tilde expansion on a machine running NIS?
87 35) I'm running SVR4.2.  Why is the line erased every time I type `@'?
88 36) Why does bash report syntax errors when my C News scripts use a
89     redirection before a subshell command?
90
91 Section G:  Where do I go from here?
92
93 37) How do I report bugs in bash, and where should I look for fixes and
94     advice?
95 38) What kind of bash documentation is there?
96 39) What's coming in future versions?
97 40) What's on the bash `wish list'?
98 41) When will the next release appear?
99
100 ----------
101 Section A:  The Basics
102
103 1)  What is it?
104
105 Bash is a Unix command interpreter (shell).  It is an implementation of
106 the Posix 1003.2 shell standard, and resembles the Korn and System V
107 shells.
108
109 Bash contains a number of enhancements over those shells, both
110 for interactive use and shell programming.  Features geared
111 toward interactive use include command line editing, command
112 history, job control, aliases, and prompt expansion.  Programming
113 features include additional variable expansions, shell
114 arithmetic, and a number of variables and options to control
115 shell behavior.
116
117 Bash was originally written by Brian Fox of the Free Software
118 Foundation.  The current developer and maintainer is Chet Ramey
119 of Case Western Reserve University.
120
121 2)  What's the latest version?
122
123 The latest version is 2.02, first made available on Monday, 20 April, 1998.
124
125 3)  Where can I get it?
126
127 Bash is the GNU project's shell, and so is available from the
128 master GNU archive site, prep.ai.mit.edu, and its mirrors.  The
129 latest version is also available for FTP from ftp.cwru.edu.
130 The following URLs tell how to get version 2.02:
131
132 ftp://prep.ai.mit.edu/pub/gnu/bash-2.02.tar.gz
133 ftp://ftp.cwru.edu/pub/bash/bash-2.02.tar.gz
134
135 Formatted versions of the documentation are available with the URLs:
136
137 ftp://prep.ai.mit.edu/pub/gnu/bash-doc-2.02.tar.gz
138 ftp://ftp.cwru.edu/pub/bash/bash-doc-2.02.tar.gz
139
140 4)  On what machines will bash run?
141
142 Bash has been ported to nearly every version of UNIX.  All you
143 should have to do to build it on a machine for which a port
144 exists is to type `configure' and then `make'.  The build process
145 will attempt to discover the version of UNIX you have and tailor
146 itself accordingly, using a script created by GNU autoconf.
147
148 More information appears in the file `INSTALL' in the distribution.
149
150 5) Will bash run on operating systems other than Unix?
151
152 Configuration specifics for Unix-like systems such as QNX and
153 LynxOS are included in the distribution.  Bash-2.02 should 
154 compile and run on Minix 2.0 (patches were contributed), but I
155 don't believe anyone has built bash-2.x on earlier Minix versions
156 yet.
157
158 Bash has been ported to versions of Windows implementing the Win32
159 programming interface.  This includes Windows 95 and Windows NT.
160 The port was done by Cygnus Solutions as part of their GNU-Win32
161 project.  For more information about the project, look at the URL
162
163 http://www.cygnus.com/misc/gnu-win32
164
165 Cygnus has ported bash-1.14.7, and their port is part of the current
166 gnu-win32 release.  Cygnus has also done a port of bash-2.01 to the
167 GNU-Win32 environment, and it should be available as part of their next
168 release.
169
170 Bash-2.02 should require no local Cygnus changes to build and run under
171 GNU-WIN32.
172
173 The Cygnus port works only on Intel machines.  There is a port of bash
174 (I don't know which version) to the alpha/NT environment available from
175
176 ftp://ftp.gnustep.org//pub/win32/bash-alpha-nt-1.01.tar.gz
177
178 Softway Systems has ported bash-2.01.1 to their Interix (nee OpenNT)
179 system, a Unix subsystem for NT that replaces the Microsoft POSIX
180 subsystem.  Check out http://www.interix.com for more information.
181
182 D. J. Delorie has ported bash-1.14.7 to run under MS-DOS, as part of
183 the DJGPP project.  For more information on the project, see
184
185 http://www.delorie.com/djgpp/
186
187 I picked up a binary of bash-1.14.7 that is purported to work with
188 the DJGPP V2 environment from
189
190 ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh1147b.zip
191
192 The corresponding source is
193
194 ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh1147s.zip
195
196 Ports of bash-1.12 and bash-2.0 are available for OS/2 from
197
198 ftp://hobbes.nmsu.edu/pub/os2/util/shell/bash_112.zip
199 ftp://hobbes.nmsu.edu/pub/os2/util/shell/bash-2.0(253).zip
200
201 I haven't looked at either, but the second appears to be a binary-only
202 distribution.  Beware.
203
204 6) How can I build bash with gcc? 
205
206 Bash configures to use gcc by default if it is available.  Read the
207 file INSTALL in the distribution for more information.
208
209 7)  How can I make bash my login shell?
210
211 Some machines let you use `chsh' to change your login shell.  Other
212 systems use `passwd -s' or `passwd -e'.  If one of these works for
213 you, that's all you need.  Note that many systems require the full
214 pathname to a shell to appear in /etc/shells before you can make it
215 your login shell.  For this, you may need the assistance of your
216 friendly local system administrator. 
217
218 If you cannot do this, you can still use bash as your login shell, but
219 you need to perform some tricks.  The basic idea is to add a command
220 to your login shell's startup file to replace your login shell with
221 bash.
222
223 For example, if your login shell is csh or tcsh, and you have installed
224 bash in /usr/gnu/bin/bash, add the following line to ~/.login:
225
226         if ( -f /usr/gnu/bin/bash ) exec /usr/gnu/bin/bash --login
227
228 (the `--login' tells bash that it is a login shell).
229
230 It's not a good idea to put this command into ~/.cshrc, because every
231 csh you run without the `-f' option, even ones started to run csh scripts,
232 reads that file.  If you must put the command in ~/.cshrc, use something
233 like
234
235         if ( $?prompt ) exec /usr/gnu/bin/bash --login
236
237 to ensure that bash is exec'd only when the csh is interactive.
238
239 If your login shell is sh or ksh, you have to do two things.
240
241 First, create an empty file in your home directory named `.bash_profile'.
242 The existence of this file will prevent the exec'd bash from trying to
243 read ~/.profile, and re-execing itself over and over again.  ~/.bash_profile
244 is the first file bash tries to read initialization commands from when
245 it is invoked as a login shell.
246
247 Next, add a line similar to the above to ~/.profile:
248
249         [ -f /usr/gnu/bin/bash ] && exec /usr/gnu/bin/bash --login
250
251 This will cause login shells to replace themselves with bash running as
252 a login shell.  Once you have this working, you can copy your initialization
253 code from ~/.profile to ~/.bash_profile.
254
255 8) I just changed my login shell to bash, and now I can't FTP into my
256    machine.  Why not?
257
258 You must add the full pathname to bash to the file /etc/shells.  As
259 noted in the answer to the previous question, many systems require
260 this before you can make bash your login shell. 
261
262 Most versions of ftpd use this file to prohibit `special' users
263 such as `uucp' and `news' from using FTP. 
264
265 9)  What's the `POSIX 1003.2 standard'?
266
267 POSIX is a name originally coined by Richard Stallman for a
268 family of open system standards based on UNIX.  There are a
269 number of aspects of UNIX under consideration for
270 standardization, from the basic system services at the system
271 call and C library level to applications and tools to system
272 administration and management.  Each area of standardization is
273 assigned to a working group in the 1003 series. 
274
275 The POSIX Shell and Utilities standard has been developed by IEEE
276 Working Group 1003.2 (POSIX.2).  It concentrates on the command
277 interpreter interface and utility programs commonly executed from
278 the command line or by other programs.  An initial version of the
279 standard has been approved and published by the IEEE, and work is
280 currently underway to update it. 
281
282 Bash is concerned with the aspects of the shell's behavior
283 defined by POSIX.2.  The shell command language has of course
284 been standardized, including the basic flow control and program
285 execution constructs, I/O redirection and pipelining, argument
286 handling, variable expansion, and quoting. 
287
288 The `special' builtins, which must be implemented as part of the
289 shell to provide the desired functionality, are specified as
290 being part of the shell; examples of these are `eval' and
291 `export'.  Other utilities appear in the sections of POSIX.2 not
292 devoted to the shell which are commonly (and in some cases must
293 be) implemented as builtin commands, such as `read' and `test'. 
294 POSIX.2 also specifies aspects of the shell's interactive
295 behavior as part of the UPE, including job control and command
296 line editing.  Only vi-style line editing commands have been
297 standardized; emacs editing commands were left out due to
298 objections.
299
300 10)  What is the bash `posix mode'?
301
302 Although bash is an implementation of the POSIX.2 shell
303 specification, there are areas where the bash default behavior
304 differs from that spec.  The bash `posix mode' changes the bash
305 behavior in these areas so that it obeys the spec more closely. 
306
307 Posix mode is entered by starting bash with the --posix option or
308 executing `set -o posix' after bash is running.
309
310 The specific aspects of bash which change when posix mode is
311 active are listed in the file CWRU/POSIX.NOTES in the bash
312 distribution.  They are also listed in a section in the Bash
313 Reference Manual.
314
315 Section B:  The latest version
316
317 11) What's new in version 2.02?
318
319 Bash-2.02 has a number of new features.  Here's a short list:
320
321 a new version of malloc (based on the old GNU malloc code in previous
322         bash versions) that is more page-oriented, more conservative
323         with memory usage, does not `orphan' large blocks when they
324         are freed, is usable on 64-bit machines, and has allocation
325         checking turned on unconditionally
326 POSIX.2-style globbing character classes ([:alpha:], [:alnum:], etc.)
327 POSIX.2-style globbing equivalence classes
328 POSIX.2-style globbing collating symbols
329 the ksh [[...]] extended conditional command
330 the ksh egrep-style extended pattern matching operators
331 a new `printf' builtin
332 the ksh-like $(<filename) command substitution, which is equivalent to
333         $(cat filename)
334 new tilde prefixes that expand to directories from the directory stack
335 new `**' arithmetic operator to do exponentiation
336 case-insensitive globbing (filename expansion)
337 menu completion a la tcsh
338 `magic-space' history expansion function like tcsh
339 the readline inputrc `language' has a new file inclusion directive ($include)
340
341 Bash-2.01 contained only a few new features:
342
343 new `GROUPS' builtin array variable containing the user's group list
344 new bindable readline commands: history-and-alias-expand-line and
345         alias-expand-line
346
347 Bash-2.0 contained extensive changes and new features from bash-1.14.7.
348 Here's a short list:
349
350 new `time' reserved word to time pipelines, shell builtins, and
351         shell functions
352 one-dimensional arrays with a new compound assignment statement,
353         appropriate expansion constructs and modifications to some
354         of the builtins (read, declare, etc.) to use them
355 new quoting syntaxes for ANSI-C string expansion and locale-specific
356         string translation
357 new expansions to do substring extraction, pattern replacement, and
358         indirect variable expansion
359 new builtins: `disown' and `shopt'
360 new variables: HISTIGNORE, SHELLOPTS, PIPESTATUS, DIRSTACK, GLOBIGNORE,
361                MACHTYPE, BASH_VERSINFO
362 special handling of many unused or redundant variables removed
363         (e.g., $notify, $glob_dot_filenames, $no_exit_on_failed_exec)
364 dynamic loading of new builtin commands; many loadable examples provided
365 new prompt expansions: \a, \e, \n, \H, \T, \@, \v, \V
366 history and aliases available in shell scripts
367 new readline variables: enable-keypad, mark-directories, input-meta,
368         visible-stats, disable-completion, comment-begin
369 new readline commands to manipulate the mark and operate on the region
370 new readline emacs mode commands and bindings for ksh-88 compatibility
371 updated and extended builtins
372 new DEBUG trap
373 expanded (and now documented) restricted shell mode
374
375 implementation stuff:   
376 autoconf-based configuration
377 nearly all of the bugs reported since version 1.14 have been fixed
378 most builtins converted to use builtin `getopt' for consistency
379 most builtins use -p option to display output in a reusable form
380         (for consistency)
381 grammar tighter and smaller (66 reduce-reduce conflicts gone)
382 lots of code now smaller and faster
383 test suite greatly expanded
384
385 12) Are there any user-visible incompatibilities between bash-2.02 and
386     bash-1.14.7?
387
388 There are a few incompatibilities between version 1.14.7 and version 2.02.
389 They are detailed in the file COMPAT in the bash-2.02 distribution.
390
391 Section C:  Differences from other Unix shells
392
393 13) How does bash differ from sh, the Bourne shell?
394
395 This is a non-comprehensive list of features that differentiate bash
396 from the SVR4.2 shell.  The bash manual page explains these more
397 completely.
398
399 Things bash has that sh does not:
400         long invocation options
401         `!' reserved word to invert pipeline return value
402         `time' reserved word to time pipelines and shell builtins
403         the `function' reserved word
404         the select compound command and reserved word
405         new $'...' and $"..." quoting
406         the $(...) form of command substitution
407         the $(<filename) form of command substitution, equivalent to
408                 $(cat filename)
409         the ${#param} parameter value length operator
410         the ${!param} indirect parameter expansion operator
411         the ${param:length[:offset]} parameter substring operator
412         the ${param/pat[/string]} parameter pattern substitution operator
413         expansions to perform substring removal (${p%[%]w}, ${p#[#]w})
414         expansion of positional parameters beyond $9 with ${num}
415         variables: BASH, BASH_VERSION, BASH_VERSINFO, UID, EUID, REPLY,
416                    TIMEFORMAT, PPID, PWD, OLDPWD, SHLVL, RANDOM, SECONDS,
417                    LINENO, HISTCMD, HOSTTYPE, OSTYPE, MACHTYPE, HOSTNAME,
418                    ENV, PS3, PS4, DIRSTACK, PIPESTATUS, HISTSIZE, HISTFILE,
419                    HISTFILESIZE, HISTCONTROL, HISTIGNORE, GLOBIGNORE, GROUPS,
420                    PROMPT_COMMAND, FCEDIT, FIGNORE, IGNOREEOF, INPUTRC,
421                    SHELLOPTS, OPTERR, HOSTFILE, TMOUT, histchars, auto_resume
422         DEBUG trap
423         variable arrays with new compound assignment syntax
424         redirections: <>, &>, >|
425         prompt string special char translation and variable expansion
426         auto-export of modified values of variables in initial environment
427         command search finds functions before builtins
428         bash return builtin will exit a file sourced with `.'
429         builtins: cd -/-L/-P, exec -l/-c/-a, echo -e/-E, hash -p.
430                   export -n/-f/-p/name=value, pwd -L/-P, read -e/-p/-a,
431                   readonly -a/-f/name=value, trap -l, set +o,
432                   set -b/-m/-o option/-h/-p/-B/-C/-H/-P,
433                   unset -f/-v, ulimit -m/-p/-u,
434                   type -a/-p/-t, suspend -f, kill -n,
435                   test -o optname/s1 == s2/s1 < s2/s1 > s2/-nt/-ot/-ef/-O/-G/-S
436         bash reads ~/.bashrc for interactive shells, $ENV for non-interactive
437         bash restricted shell mode is more extensive
438         bash allows functions and variables with the same name
439         brace expansion
440         tilde expansion
441         arithmetic expansion with $((...)) and `let' builtin
442         the `[[...]]' extended conditional command
443         process substitution
444         aliases and alias/unalias builtins
445         local variables in functions and `local' builtin
446         readline and command-line editing
447         command history and history/fc builtins
448         csh-like history expansion
449         other new bash builtins: bind, command, builtin, declare/typeset,
450                                  dirs, enable, fc, help, history, logout,
451                                  popd, pushd, disown, shopt, printf
452         exported functions
453         filename generation when using output redirection (command >a*)
454         POSIX.2-style globbing character classes
455         POSIX.2-style globbing equivalence classes
456         POSIX.2-style globbing collating symbols
457         egrep-like extended pattern matching operators
458         case-insensitive pattern matching and globbing
459         variable assignments preceding commands affect only that command,
460                 even for builtins and functions
461         posix mode
462
463 Things sh has that bash does not:
464         uses variable SHACCT to do shell accounting
465         includes `stop' builtin (bash can use alias stop='kill -s STOP')
466         `newgrp' builtin
467         turns on job control if called as `jsh'
468         $TIMEOUT (like bash $TMOUT)
469         `^' is a synonym for `|'
470         new SVR4.2 sh builtins: mldmode, priv
471
472 Implementation differences:
473         redirection to/from compound commands causes sh to create a subshell
474         bash does not allow unbalanced quotes; sh silently inserts them at EOF
475         bash does not mess with signal 11
476         sh sets (euid, egid) to (uid, gid) if -p not supplied and uid < 100
477         bash splits only the results of expansions on IFS, using POSIX.2
478                 field splitting rules; sh splits all words on IFS
479         sh does not allow MAILCHECK to be unset (?)
480         sh does not allow traps on SIGALRM or SIGCHLD
481         bash allows multiple option arguments when invoked (e.g. -x -v);
482                 sh allows only a single option argument (`sh -x -v' attempts
483                 to open a file named `-v', and, on SunOS 4.1.4, dumps core.
484                 On Solaris 2, sh goes into an infinite loop.)
485         sh exits a script if any builtin fails; bash exits only if one of
486                 the POSIX.2 `special' builtins fails
487
488 14)  How does bash differ from the Korn shell, version ksh88?
489
490 Things bash has or uses that ksh88 does not:
491         long invocation options
492         `!' reserved word
493         posix mode and posix conformance
494         command hashing
495         tilde expansion for assignment statements that look like $PATH
496         process substitution with named pipes if /dev/fd is not available
497         the ${!param} indirect parameter expansion operator
498         the ${param:length[:offset]} parameter substring operator
499         the ${param/pat[/string]} parameter pattern substitution operator
500         variables: BASH, BASH_VERSION, BASH_VERSINFO, UID, EUID, SHLVL,
501                    TIMEFORMAT, HISTCMD, HOSTTYPE, OSTYPE, MACHTYPE,
502                    HISTFILESIZE, HISTIGNORE, HISTCONTROL, PROMPT_COMMAND,
503                    IGNOREEOF, FIGNORE, INPUTRC, HOSTFILE, DIRSTACK,
504                    PIPESTATUS, HOSTNAME, OPTERR, SHELLOPTS, GLOBIGNORE,
505                    GROUPS, histchars, auto_resume
506         prompt expansion with backslash escapes and command substitution
507         redirection: &> (stdout and stderr)
508         more extensive and extensible editing and completion
509         builtins: bind, builtin, command, declare, dirs, echo -e/-E, enable,
510                   exec -l/-c/-a, fc -s, export -n/-f/-p, hash, help, history,
511                   jobs -x/-r/-s, kill -s/-n/-l, local, logout, popd, pushd,
512                   read -e/-p/-a, readonly -a/-n/-f/-p, set -o braceexpand/
513                   -o histexpand/-o interactive-comments/-o notify/-o physical/
514                   -o posix/-o hashall/-o onecmd/-h/-B/-C/-b/-H/-P, set +o,
515                   suspend, trap -l, type, typeset -a/-F/-p, ulimit -u,
516                   umask -S, alias -p, shopt, disown, printf
517         `!' csh-style history expansion
518         POSIX.2-style globbing character classes
519         POSIX.2-style globbing equivalence classes
520         POSIX.2-style globbing collating symbols
521         egrep-like extended pattern matching operators
522         case-insensitive pattern matching and globbing
523         `**' arithmetic operator to do exponentiation
524
525 Things ksh88 has or uses that bash does not:
526         tracked aliases
527         variables: ERRNO, FPATH, COLUMNS, LINES, EDITOR, VISUAL
528         co-processes (|&, >&p, <&p)
529         weirdly-scoped functions
530         typeset +f to list all function names without definitions
531         text of command history kept in a file, not memory
532         builtins: alias -x, cd old new, fc -e -, newgrp, print,
533                   read -p/-s/-u/var?prompt, set -A/-o gmacs/
534                   -o bgnice/-o markdirs/-o nolog/-o trackall/-o viraw/-s,
535                   typeset -H/-L/-R/-A/-ft/-fu/-fx/-l/-u/-t, whence
536
537 Implementation differences:
538         ksh runs last command of a pipeline in parent shell context
539         bash has brace expansion by default (ksh88 compile-time option)
540         bash has fixed startup file for all interactive shells; ksh reads $ENV
541         bash has exported functions
542         bash command search finds functions before builtins
543
544 15)  Which new features in ksh-93 are not in bash, and which are?
545
546 New things in ksh-93 not in bash-2.02:
547         associative arrays
548         floating point arithmetic
549         ++, --, comma arithmetic operators
550         math library functions
551         ${!name[sub]} name of subscript for associative array
552         ${!prefix*} and {!prefix@} variable name prefix expansions
553         `.' is allowed in variable names to create a hierarchical namespace
554         more extensive compound assignment syntax
555         discipline functions
556         `sleep' and `getconf' builtins (bash has loadable versions)
557         typeset -n and `nameref' variables
558         KEYBD trap
559         variables: .sh.edchar, .sh.edmode, .sh.edcol, .sh.edtext, HISTEDIT,
560                    .sh.version, .sh.name, .sh.subscript, .sh.value
561         backreferences in pattern matching
562         print -f (bash has a loadable version of print and the printf builtin)
563         `fc' has been renamed to `hist'
564         read -t/-d
565         `.' can execute shell functions
566
567 New things in ksh-93 present in bash-2.02:
568         ?: arithmetic operator
569         expansions: ${!param}, ${param:offset[:len]}, ${param/pat[/str]}
570         compound array assignment
571         the `!' reserved word
572         loadable builtins -- but ksh uses `builtin' while bash uses `enable'
573         `command', `builtin', `disown' builtins
574         new $'...' and $"..." quoting
575         FIGNORE (but bash uses GLOBIGNORE), HISTCMD
576         set -o notify/-C
577         changes to kill builtin
578         read -A (bash uses read -a)
579         trap -p
580         exec -c/-a
581         `.' restores the positional parameters when it completes
582         POSIX.2 `test'
583         umask -S
584         unalias -a
585         command and arithmetic substitution performed on PS1, PS4, and ENV
586         command name completion
587         ENV processed only for interactive shells
588
589 Section D:  Why does bash do some things differently than other Unix shells?
590
591 16) Why does bash run a different version of `command' than
592     `which command' says it will?
593
594 `which' is actually a csh script that assumes you're running csh. 
595 It reads the csh startup files from your home directory and uses
596 those to determine which `command' will be invoked.  Since bash
597 doesn't use any of those startup files, there's a good chance
598 that your bash environment differs from your csh environment. 
599
600 17) Why doesn't bash treat brace expansions exactly like csh?
601
602 The only difference between bash and csh brace expansion is that
603 bash requires a brace expression to contain at least one unquoted
604 comma if it is to be expanded.  Any brace-surrounded word not
605 containing an unquoted comma is left unchanged by the brace
606 expansion code.  This affords the greatest degree of sh
607 compatibility. 
608
609 Bash, ksh, zsh, and pd-ksh all implement brace expansion this way. 
610
611 18) Why doesn't bash have csh variable modifiers?
612
613 Posix has specified a more powerful, albeit somewhat more cryptic,
614 mechanism cribbed from ksh, and bash implements it.
615
616 ${parameter%word}
617         Remove smallest suffix pattern.  The WORD is expanded to produce
618         a pattern.  It then expands to the value of PARAMETER, with the
619         smallest portion of the suffix matched by the pattern deleted.
620
621         x=file.c
622         echo ${x%.c}.o
623         -->file.o
624
625 ${parameter%%word}
626
627         Remove largest suffix pattern.  The WORD is expanded to produce
628         a pattern.  It then expands to the value of PARAMETER, with the
629         largest portion of the suffix matched by the pattern deleted.
630
631         x=posix/src/std
632         echo ${x%%/*}
633         -->posix
634
635 ${parameter#word}
636         Remove smallest prefix pattern.  The WORD is expanded to produce
637         a pattern.  It then expands to the value of PARAMETER, with the
638         smallest portion of the prefix matched by the pattern deleted.
639
640         x=$HOME/src/cmd
641         echo ${x#$HOME}
642         -->/src/cmd
643
644 ${parameter##word}
645         Remove largest prefix pattern.  The WORD is expanded to produce
646         a pattern.  It then expands to the value of PARAMETER, with the
647         largest portion of the prefix matched by the pattern deleted.
648
649         x=/one/two/three
650         echo ${x##*/}
651         -->three
652
653
654 Given
655         a=/a/b/c/d
656         b=b.xxx
657
658         csh                     bash            result
659         ---                     ----            ------
660         $a:h                    ${a%/*}            /a/b/c
661         $a:t                    ${a##*/}           d
662         $b:r                    ${b%.*}            b
663         $b:e                    ${b##*.}           xxx
664
665
666 19) How can I make my csh aliases work when I convert to bash?
667
668 Bash uses a different syntax to support aliases than csh does. 
669 The details can be found in the documentation.  We have provided
670 a shell script which does most of the work of conversion for you;
671 this script can be found in ./examples/misc/alias-conv.sh.  Here is
672 how you use it:
673   
674 Start csh in the normal way for you.  (e.g., `csh')
675   
676 Pipe the output of `alias' through `alias-conv.sh', saving the
677 results into `bash_aliases':
678   
679         alias | alias-conv.sh >bash_aliases
680   
681 Edit `bash_aliases', carefully reading through any created
682 functions.  You will need to change the names of some csh specific
683 variables to the bash equivalents.  The script converts $cwd to
684 $PWD, $term to $TERM, $home to $HOME, $user to $USER, and $prompt
685 to $PS1.  You may also have to add quotes to avoid unwanted
686 expansion.
687
688 For example, the csh alias:
689   
690         alias cd 'cd \!*; echo $cwd'
691   
692 is converted to the bash function:
693
694         cd () { command cd "$@"; echo $PWD ; }
695
696 The only thing that needs to be done is to quote $PWD:
697   
698         cd () { command cd "$@"; echo "$PWD" ; }
699   
700 Merge the edited file into your ~/.bashrc.
701
702 There is an additional, more ambitious, script in
703 examples/misc/cshtobash that attempts to convert your entire csh
704 environment to its bash equivalent.  This script can be run as
705 simply `cshtobash' to convert your normal interactive
706 environment, or as `cshtobash ~/.login' to convert your login
707 environment. 
708
709 20) How can I pipe standard output and standard error from one command to
710     another, like csh does with `|&'?
711
712 Use
713         command 2>&1 | command2
714
715 The key is to remember that piping is performed before redirection, so
716 file descriptor 1 points to the pipe when it is duplicated onto file
717 descriptor 2.
718
719 21) Now that I've converted from ksh to bash, are there equivalents to
720     ksh features like autoloaded functions and the `whence' command?
721
722 There are features in ksh-88 that do not have direct bash equivalents.
723 Most, however, can be emulated with very little trouble.
724
725 ksh-88 feature          Bash equivalent
726 --------------          ---------------
727 compiled-in aliases     set up aliases in .bashrc; some ksh aliases are
728                         bash builtins (hash, history, type)
729 coprocesses             named pipe pairs (one for read, one for write)
730 typeset +f              declare -F
731 cd, print, whence       function substitutes in examples/functions/kshenv
732 autoloaded functions    examples/functions/autoload is the same as typeset -fu
733 read var?prompt         read -p prompt var
734
735 Section E:  How can I get bash to do certain things, and why does bash do
736             things the way it does?
737
738 22) Why is the bash builtin `test' slightly different from /bin/test?
739
740 The specific example used here is [ ! x -o x ], which is false.
741
742 Bash's builtin `test' implements the Posix.2 spec, which can be
743 summarized as follows (the wording is due to David Korn):
744    
745 Here is the set of rules for processing test arguments.
746   
747     0 Args: False
748     1 Arg:  True iff argument is not null.
749     2 Args: If first arg is !, True iff second argument is null.
750             If first argument is unary, then true if unary test is true
751             Otherwise error.
752     3 Args: If second argument is a binary operator, do binary test of $1 $3
753             If first argument is !, negate two argument test of $2 $3
754             If first argument is `(' and third argument is `)', do the
755             one-argument test of the second argument.
756             Otherwise error.
757     4 Args: If first argument is !, negate three argument test of $2 $3 $4.
758             Otherwise unspecified
759     5 or more Args: unspecified.  (Historical shells would use their
760     current algorithm).
761    
762 The operators -a and -o are considered binary operators for the purpose
763 of the 3 Arg case.
764    
765 As you can see, the test becomes (not (x or x)), which is false.
766
767 23) Why does bash sometimes say `Broken pipe'?
768
769 If a sequence of commands appears in a pipeline, and one of the
770 reading commands finishes before the writer has finished, the
771 writer receives a SIGPIPE signal.  Many other shells special-case
772 SIGPIPE as an exit status in the pipeline and do not report it. 
773 For example, in:
774   
775       ps -aux | head
776   
777 `head' can finish before `ps' writes all of its output, and ps
778 will try to write on a pipe without a reader.  In that case, bash
779 will print `Broken pipe' to stderr when ps is killed by a
780 SIGPIPE. 
781
782 24) How can I get bash to read and display eight-bit characters?
783
784 This is a process requiring several steps.
785
786 First, you must ensure that the `physical' data path is a full eight
787 bits.  For xterms, for example, the `vt100' resources `eightBitInput'
788 and `eightBitOutput' should be set to `true'.
789
790 Once you have set up an eight-bit path, you must tell the kernel and
791 tty driver to leave the eighth bit of characters alone when processing
792 keyboard input.  Use `stty' to do this:
793
794         stty cs8 -istrip -parenb
795
796 For old BSD-style systems, you can use
797
798         stty pass8
799
800 You may also need
801
802         stty even odd
803
804 Finally, you need to tell readline that you will be inputting and
805 displaying eight-bit characters.  You use readline variables to do
806 this.  These variables can be set in your .inputrc or using the bash
807 `bind' builtin.  Here's an example using `bind':
808
809         bash$ bind 'set convert-meta off'
810         bash$ bind 'set meta-flag on'
811         bash$ bind 'set output-meta on'
812
813 The `set' commands between the single quotes may also be placed
814 in ~/.inputrc.
815
816 25) How do I write a function `x' to replace builtin command `x', but
817     still invoke the command from within the function?
818
819 This is why the `command' and `builtin' builtins exist.  The
820 `command' builtin executes the command supplied as its first
821 argument, skipping over any function defined with that name.  The
822 `builtin' builtin executes the builtin command given as its first
823 argument directly. 
824
825 For example, to write a function to replace `cd' that writes the
826 hostname and current directory to an xterm title bar, use
827 something like the following:
828
829         cd()
830         {
831                 builtin cd "$@" && xtitle "$HOST: $PWD"
832         }
833
834 This could also be written using `command' instead of `builtin';
835 the version above is marginally more efficient. 
836
837 26) When I have terminal escape sequences in my prompt, why does bash
838     wrap lines at the wrong column?
839
840 Readline, the line editing library that bash uses, does not know
841 that the terminal escape sequences do not take up space on the
842 screen.  The redisplay code assumes, unless told otherwise, that
843 each character in the prompt is a `printable' character that
844 takes up one character position on the screen. 
845
846 You can use the bash prompt expansion facility (see the PROMPTING
847 section in the manual page) to tell readline that sequences of
848 characters in the prompt strings take up no screen space. 
849
850 Use the \[ escape to begin a sequence of non-printing characters,
851 and the \] escape to signal the end of such a sequence. 
852
853 27) How can I find the value of a shell variable whose name is the value
854     of another shell variable?
855
856 Bash-2.02 supports this directly.  You can use 
857
858         ${!var}
859
860 For example, the following sequence of commands will echo `z':
861
862         var1=var2
863         var2=z
864         echo ${!var1}
865
866 For sh compatibility, use the `eval' builtin.  The important
867 thing to remember is that `eval' expands the arguments you give
868 it again, so you need to quote the parts of the arguments that
869 you want `eval' to act on. 
870
871 For example, this expression prints the value of the last positional
872 parameter:
873
874         eval echo \"\$\{$#\}\"
875
876 The expansion of the quoted portions of this expression will be
877 deferred until `eval' runs, while the `$#' will be expanded
878 before `eval' is executed.  In bash-2.02,
879
880         echo ${!#}
881
882 does the same thing.
883
884 28) If I pipe the output of a command into `read variable', why doesn't
885     the output show up in $variable when the read command finishes?
886
887 This has to do with the parent-child relationship between Unix
888 processes. 
889
890 Each element of a pipeline runs in a separate process, a child of
891 the shell running the pipeline.  A subprocess cannot affect its
892 parent's environment.  When the `read' command sets the variable
893 to the input, that variable is set only in the subshell, not the
894 parent shell.  When the subshell exits, the value of the variable
895 is lost. 
896
897 Many pipelines that end with `read variable' can be converted
898 into command substitutions, which will capture the output of
899 a specified command.  The output can then be assigned to a
900 variable:
901
902         grep ^gnu /usr/lib/news/active | wc -l | read ngroup
903
904 can be converted into
905
906         ngroup=$(grep ^gnu /usr/lib/news/active | wc -l)
907
908 This does not, unfortunately, work to split the text among
909 multiple variables, as read does when given multiple variable
910 arguments.  If you need to do this, you can either use the
911 command substitution above to read the output into a variable
912 and chop up the variable using the bash pattern removal
913 expansion operators or use some variant of the following
914 approach.
915
916 Say /usr/local/bin/ipaddr is the following shell script:
917
918 #! /bin/sh
919 host `hostname` | awk '/address/ {print $NF}'
920
921 Instead of using
922
923         /usr/local/bin/ipaddr | read A B C D
924
925 to break the local machine's IP address into separate octets, use
926
927         OIFS="$IFS"
928         IFS=.
929         set -- $(/usr/local/bin/ipaddr)
930         IFS="$OIFS"
931         A="$1" B="$2" C="$3" D="$4"
932
933 Beware, however, that this will change the shell's positional
934 parameters.  If you need them, you should save them before doing
935 this.
936
937 This is the general approach -- in most cases you will not need to
938 set $IFS to a different value.
939
940 29) I have a bunch of shell scripts that use backslash-escaped characters
941     in arguments to `echo'.  Bash doesn't interpret these characters.  Why
942     not, and how can I make it understand them?
943
944 This is the behavior of echo on most Unix System V machines.
945
946 The bash builtin `echo' is modelled after the 9th Edition
947 Research Unix version of `echo'.  It does not interpret
948 backslash-escaped characters in its argument strings by default;
949 it requires the use of the -e option to enable the
950 interpretation.  The System V echo provides no way to disable the
951 special characters; the bash echo has a -E option to disable
952 them. 
953
954 There is a configuration option that will make bash behave like
955 the System V echo and interpret things like `\t' by default.  Run
956 configure with the --enable-usg-echo-default option to turn this
957 on.  Be aware that this will cause some of the tests run when you
958 type `make tests' to fail.
959
960 30) Why doesn't a while or for loop get suspended when I type ^Z?
961
962 This is a consequence of how job control works on Unix.  The only
963 thing that can be suspended is the process group.  This is a single
964 command or pipeline of commands that the shell forks and executes.
965
966 When you run a while or for loop, the only thing that the shell forks
967 and executes are any commands in the while loop test and commands in
968 the loop bodies.  These, therefore, are the only things that can be
969 suspended when you type ^Z.
970
971 If you want to be able to stop the entire loop, you need to put it
972 within parentheses, which will force the loop into a subshell that
973 may be stopped (and subsequently restarted) as a single unit.
974
975 31) How can I make the bash `time' reserved word print timing output that
976     looks like the output from my system's /usr/bin/time?
977
978 The bash command timing code looks for a variable `TIMEFORMAT' and
979 uses its value as a format string to decide how to display the
980 timing statistics.
981
982 The value of TIMEFORMAT is a string with `%' escapes expanded in a
983 fashion similar in spirit to printf(3).  The manual page explains
984 the meanings of the escape sequences in the format string.
985
986 If TIMEFORMAT is not set, bash acts as if the following assignment had
987 been performed:
988
989         TIMEFORMAT=$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
990
991 The POSIX.2 default time format (used by `time -p command') is
992
993         TIMEFORMAT=$'real %2R\nuser %2U\nsys %2S'
994
995 The BSD /usr/bin/time format can be emulated with:
996
997         TIMEFORMAT=$'\t%1R real\t%1U user\t%1S sys'
998
999 The System V /usr/bin/time format can be emulated with:
1000
1001         TIMEFORMAT=$'\nreal\t%1R\nuser\t%1U\nsys\t%1S'
1002
1003 The ksh format can be emulated with:
1004
1005         TIMEFORMAT=$'\nreal\t%2lR\nuser\t%2lU\nsys\t%2lS'
1006
1007 Section F:  Things to watch out for on certain Unix versions
1008
1009 32) Why can't I use command line editing in my `cmdtool'?
1010
1011 The problem is `cmdtool' and bash fighting over the input.  When
1012 scrolling is enabled in a cmdtool window, cmdtool puts the tty in
1013 `raw mode' to permit command-line editing using the mouse for
1014 applications that cannot do it themselves.  As a result, bash and
1015 cmdtool each try to read keyboard input immediately, with neither
1016 getting enough of it to be useful.
1017
1018 This mode also causes cmdtool to not implement many of the
1019 terminal functions and control sequences appearing in the
1020 `sun-cmd' termcap entry.  For a more complete explanation, see
1021 that file examples/suncmd.termcap in the bash distribution. 
1022
1023 `xterm' is a better choice, and gets along with bash much more
1024 smoothly.
1025
1026 If you must use cmdtool, you can use the termcap description in
1027 examples/suncmd.termcap.  Set the TERMCAP variable to the terminal
1028 description contained in that file, i.e.
1029
1030 TERMCAP='Mu|sun-cmd:am:bs:km:pt:li#34:co#80:cl=^L:ce=\E[K:cd=\E[J:rs=\E[s:'
1031
1032 Then export TERMCAP and start a new cmdtool window from that shell.
1033 The bash command-line editing should behave better in the new
1034 cmdtool.  If this works, you can put the assignment to TERMCAP
1035 in your bashrc file.
1036
1037 33) I built bash on Solaris 2.  Why do globbing expansions and filename
1038     completion chop off the first few characters of each filename?
1039
1040 This is the consequence of building bash on SunOS 5 and linking
1041 with the libraries in /usr/ucblib, but using the definitions
1042 and structures from files in /usr/include. 
1043
1044 The actual conflict is between the dirent structure in
1045 /usr/include/dirent.h and the struct returned by the version of
1046 `readdir' in libucb.a (a 4.3-BSD style `struct direct'). 
1047
1048 Make sure you've got /usr/ccs/bin ahead of /usr/ucb in your $PATH
1049 when configuring and building bash.  This will ensure that you
1050 use /usr/ccs/bin/cc or acc instead of /usr/ucb/cc and that you
1051 link with libc before libucb. 
1052
1053 If you have installed the Sun C compiler, you may also need to
1054 put /usr/ccs/bin and /opt/SUNWspro/bin into your $PATH before
1055 /usr/ucb.
1056
1057 34) Why does bash dump core after I interrupt username completion or
1058     `~user' tilde expansion on a machine running NIS?
1059
1060 This is a famous and long-standing bug in the SunOS YP (sorry, NIS)
1061 client library, which is part of libc.
1062
1063 The YP library code keeps static state -- a pointer into the data
1064 returned from the server.  When YP initializes itself (setpwent),
1065 it looks at this pointer and calls free on it if it's non-null. 
1066 So far, so good. 
1067
1068 If one of the YP functions is interrupted during getpwent (the
1069 exact function is interpretwithsave()), and returns NULL, the
1070 pointer is freed without being reset to NULL, and the function
1071 returns.  The next time getpwent is called, it sees that this
1072 pointer is non-null, calls free, and the bash free() blows up
1073 because it's being asked to free freed memory. 
1074
1075 The traditional Unix mallocs allow memory to be freed multiple
1076 times; that's probably why this has never been fixed.  You can
1077 run configure with the `--without-gnu-malloc' option to use
1078 the C library malloc and avoid the problem.
1079
1080 35) I'm running SVR4.2.  Why is the line erased every time I type `@'?
1081
1082 The `@' character is the default `line kill' character in most
1083 versions of System V, including SVR4.2.  You can change this
1084 character to whatever you want using `stty'.  For example, to
1085 change the line kill character to control-u, type
1086
1087         stty kill ^U
1088
1089 where the `^' and `U' can be two separate characters.
1090
1091 36) Why does bash report syntax errors when my C News scripts use a
1092     redirection before a subshell command?
1093
1094 The actual command in question is something like
1095
1096         < file ( command )
1097
1098 According to the grammar given in the POSIX.2 standard, this construct
1099 is, in fact, a syntax error.  Redirections may only precede `simple
1100 commands'.  A subshell construct such as the above is one of the shell's
1101 `compound commands'.  A redirection may only follow a compound command.
1102
1103 The file CWRU/sh-redir-hack in the bash-2.02 distribution is an
1104 (unofficial) patch to parse.y that will modify the grammar to
1105 support this construct.  It will not apply with `patch'; you must
1106 modify parse.y by hand.  Note that if you apply this, you must
1107 recompile with -DREDIRECTION_HACK.  This introduces a large
1108 number of reduce/reduce conflicts into the shell grammar. 
1109
1110 Section G:  Where do I go from here?
1111
1112 37) How do I report bugs in bash, and where should I look for fixes and
1113     advice?
1114
1115 Use the `bashbug' script to report bugs.  It is built and
1116 installed at the same time as bash.  It provides a standard
1117 template for reporting a problem and automatically includes
1118 information about your configuration and build environment. 
1119
1120 `bashbug' sends its reports to bug-bash@prep.ai.mit.edu, which
1121 is a large mailing list gatewayed to the usenet newsgroup gnu.bash.bug. 
1122
1123 Bug fixes, answers to questions, and announcements of new releases
1124 are all posted to gnu.bash.bug.  Discussions concerning bash features
1125 and problems also take place there.
1126
1127 To reach the bash maintainers directly, send mail to
1128 bash-maintainers@prep.ai.mit.edu.
1129
1130 38) What kind of bash documentation is there?
1131
1132 First, look in the doc directory in the bash distribution.  It should
1133 contain at least the following files:
1134
1135 bash.1          an extensive, thorough Unix-style manual page
1136 builtins.1      a manual page covering just bash builtin commands
1137 bashref.texi    a reference manual in GNU info format
1138 bash.html       an HTML version of the manual page
1139 bashref.html    an HTML version of the reference manual
1140 FAQ             this file
1141 article.ms      text of an article written for The Linux Journal
1142 readline.3      a man page describing readline
1143
1144 Postscript files created from the above source are available in
1145 the documentation distribution.
1146
1147 There is additional documentation available for anonymous FTP from host
1148 ftp.cwru.edu in the `pub/bash' directory.
1149
1150 Cameron Newham and Bill Rosenblatt have written a book on bash, published
1151 by O'Reilly and Associates.  The book is based on Bill Rosenblatt's Korn
1152 Shell book.  The title is ``Learning the Bash Shell'', and the ISBN number
1153 is 1-56592-147-X.  Look for it in fine bookstores near you.  This book
1154 covers bash-1.14, but has an appendix describing some of the new features
1155 in bash-2.0.  
1156
1157 A second edition of this book is available, published in January, 1998.
1158 The ISBN number is 1-56592-347-2.  Look for it in the same fine bookstores
1159 or on the web.
1160
1161 39) What's coming in future versions?
1162
1163 These are features I plan to include in a future version of bash.
1164
1165 a bash debugger (a minimally-tested version is included with bash-2.02)
1166 Programmable completion a la zsh
1167
1168 40) What's on the bash `wish list' for future versions?
1169
1170 These are features that may or may not appear in a future version of bash.
1171
1172 associative arrays (not really all that hard)
1173 breaking some of the shell functionality into embeddable libraries
1174 better internationalization using GNU `gettext'
1175 an option to use external files for the long `help' text
1176 timeouts for the `read' builtin
1177 the ksh-93 ${!prefix*} and ${!prefix@} operators
1178 arithmetic ++ and -- prefix and postfix operators
1179 date-stamped command history
1180
1181 41) When will the next release appear?
1182
1183 The next version will appear sometime in 1998.  Never make
1184 predictions. 
1185
1186
1187 This document is Copyright 1995, 1996, 1998 by Chester Ramey.
1188
1189 Permission is hereby granted, without written agreement and
1190 without license or royalty fees, to use, copy, and distribute
1191 this document for any purpose, provided that the above copyright
1192 notice appears in all copies of this document and that the
1193 contents of this document remain unaltered.