Imported from ../bash-2.05.tar.gz.
[platform/upstream/bash.git] / doc / FAQ
1 This is the Bash FAQ, version 3.11, for Bash version 2.05.
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 The Bash home page is http://cnswww.cns.cwru.edu/~chet/bash/bashtop.html
19
20 ----------
21 Contents:
22
23 Section A:  The Basics
24
25 A1) What is it?
26 A2) What's the latest version?
27 A3) Where can I get it?
28 A4) On what machines will bash run?
29 A5) Will bash run on operating systems other than Unix?
30 A6) How can I build bash with gcc?
31 A7) How can I make bash my login shell?
32 A8) I just changed my login shell to bash, and now I can't FTP into my
33     machine.  Why not?
34 A9) What's the `POSIX 1003.2 standard'?
35 A10) What is the bash `posix mode'?
36
37 Section B:  The latest version
38
39 B1) What's new in version 2.05?
40 B2) Are there any user-visible incompatibilities between bash-2.05 and
41     bash-1.14.7?
42
43 Section C:  Differences from other Unix shells
44
45 C1) How does bash differ from sh, the Bourne shell?
46 C2) How does bash differ from the Korn shell, version ksh88?
47 C3) Which new features in ksh-93 are not in bash, and which are?
48
49 Section D:  Why does bash do some things differently than other Unix shells?
50
51 D1) Why does bash run a different version of `command' than
52     `which command' says it will?
53 D2) Why doesn't bash treat brace expansions exactly like csh?
54 D3) Why doesn't bash have csh variable modifiers?
55 D4) How can I make my csh aliases work when I convert to bash?
56 D5) How can I pipe standard output and standard error from one command to
57     another, like csh does with `|&'?
58 D6) Now that I've converted from ksh to bash, are there equivalents to
59     ksh features like autoloaded functions and the `whence' command?
60
61 Section E:  Why does bash do certain things the way it does?
62
63 E1) Why is the bash builtin `test' slightly different from /bin/test?
64 E2) Why does bash sometimes say `Broken pipe'?
65 E3) When I have terminal escape sequences in my prompt, why does bash
66     wrap lines at the wrong column?
67 E4) If I pipe the output of a command into `read variable', why doesn't
68     the output show up in $variable when the read command finishes?
69 E5) I have a bunch of shell scripts that use backslash-escaped characters
70     in arguments to `echo'.  Bash doesn't interpret these characters.  Why
71     not, and how can I make it understand them?
72 E6) Why doesn't a while or for loop get suspended when I type ^Z?
73 E7) What about empty for loops in Makefiles?
74 E8) Why does the arithmetic evaluation code complain about `08'?
75 E9) Why does the pattern matching expression [A-Z]* match files beginning
76     with every letter except `z'?
77
78 Section F:  Things to watch out for on certain Unix versions
79
80 F1) Why can't I use command line editing in my `cmdtool'?
81 F2) I built bash on Solaris 2.  Why do globbing expansions and filename
82     completion chop off the first few characters of each filename?
83 F3) Why does bash dump core after I interrupt username completion or
84     `~user' tilde expansion on a machine running NIS?
85 F4) I'm running SVR4.2.  Why is the line erased every time I type `@'?
86 F5) Why does bash report syntax errors when my C News scripts use a
87     redirection before a subshell command?
88 F6) Why can't I use vi-mode editing on Red Hat Linux 6.1?
89
90 Section G:  How can I get bash to do certain common things?
91
92 G1) How can I get bash to read and display eight-bit characters?
93 G2) How do I write a function `x' to replace builtin command `x', but
94     still invoke the command from within the function?
95 G3) How can I find the value of a shell variable whose name is the value
96     of another shell variable?
97 G4) How can I make the bash `time' reserved word print timing output that
98     looks like the output from my system's /usr/bin/time?
99 G5) How do I get the current directory into my prompt?
100 G6) How can I rename "*.foo" to "*.bar"?
101 G7) How can I translate a filename from uppercase to lowercase?
102 G8) How can I write a filename expansion (globbing) pattern that will match
103     all files in the current directory except "." and ".."?
104
105 Section H:  Where do I go from here?
106
107 H1) How do I report bugs in bash, and where should I look for fixes and
108     advice?
109 H2) What kind of bash documentation is there?
110 H3) What's coming in future versions?
111 H4) What's on the bash `wish list'?
112 H5) When will the next release appear?
113
114 ----------
115 Section A:  The Basics
116
117 A1)  What is it?
118
119 Bash is a Unix command interpreter (shell).  It is an implementation of
120 the Posix 1003.2 shell standard, and resembles the Korn and System V
121 shells.
122
123 Bash contains a number of enhancements over those shells, both
124 for interactive use and shell programming.  Features geared
125 toward interactive use include command line editing, command
126 history, job control, aliases, and prompt expansion.  Programming
127 features include additional variable expansions, shell
128 arithmetic, and a number of variables and options to control
129 shell behavior.
130
131 Bash was originally written by Brian Fox of the Free Software
132 Foundation.  The current developer and maintainer is Chet Ramey
133 of Case Western Reserve University.
134
135 A2)  What's the latest version?
136
137 The latest version is 2.05, first made available on Monday, 9 April 2001.
138
139 A3)  Where can I get it?
140
141 Bash is the GNU project's shell, and so is available from the
142 master GNU archive site, ftp.gnu.org, and its mirrors.  The
143 latest version is also available for FTP from ftp.cwru.edu.
144 The following URLs tell how to get version 2.05:
145
146 ftp://ftp.gnu.org/pub/gnu/bash/bash-2.05.tar.gz
147 ftp://ftp.cwru.edu/pub/bash/bash-2.05.tar.gz
148
149 Formatted versions of the documentation are available with the URLs:
150
151 ftp://ftp.gnu.org/pub/gnu/bash/bash-doc-2.05.tar.gz
152 ftp://ftp.cwru.edu/pub/bash/bash-doc-2.05.tar.gz
153
154 A4)  On what machines will bash run?
155
156 Bash has been ported to nearly every version of UNIX.  All you
157 should have to do to build it on a machine for which a port
158 exists is to type `configure' and then `make'.  The build process
159 will attempt to discover the version of UNIX you have and tailor
160 itself accordingly, using a script created by GNU autoconf.
161
162 More information appears in the file `INSTALL' in the distribution.
163
164 A5) Will bash run on operating systems other than Unix?
165
166 Configuration specifics for Unix-like systems such as QNX and
167 LynxOS are included in the distribution.  Bash-2.05 should 
168 compile and run on Minix 2.0 (patches were contributed), but I
169 don't believe anyone has built bash-2.x on earlier Minix versions
170 yet.
171
172 Bash has been ported to versions of Windows implementing the Win32
173 programming interface.  This includes Windows 95 and Windows NT.
174 The port was done by Cygnus Solutions as part of their CYGWIN
175 project.  For more information about the project, look at the URLs
176
177 http://www.cygwin.com/
178 http://sourceware.cygnus.com/cygwin
179
180 Cygnus originally ported bash-1.14.7, and that port was part of their
181 early GNU-Win32 (the original name) releases.  Cygnus has also done a
182 port of bash-2.04 to the CYGWIN environment, and it is available as
183 part of their current release.
184
185 Bash-2.05 should require no local Cygnus changes to build and run under
186 CYGWIN.
187
188 The Cygnus port works only on Intel machines.  There is a port of bash
189 (I don't know which version) to the alpha/NT environment available from
190
191 ftp://ftp.gnustep.org//pub/win32/bash-alpha-nt-1.01.tar.gz
192
193 DJ Delorie has a port of bash-1.14.7 which runs under MS-DOS, as part
194 of the DJGPP project.  For more information on the project, see
195
196 http://www.delorie.com/djgpp/
197
198 I have been told that the original DJGPP port was done by Daisuke Aoyama.
199
200 I picked up a binary of bash-1.14.7 that is purported to work with
201 the DJGPP V2 environment from
202
203 ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh1147b.zip
204
205 The corresponding source is
206
207 ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh1147s.zip
208
209 Mark Elbrecht <snowball3@bigfoot.com> has sent me notice that bash-2.03
210 has become available for DJGPP V2.  The files are available as:
211
212 ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh203b.zip  binary
213 ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh203d.zip  documentation
214 ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh203s.zip  source
215
216 Mark has begun to work with bash-2.04.
217
218 Ports of bash-1.12 and bash-2.0 are available for OS/2 from
219
220 ftp://hobbes.nmsu.edu/pub/os2/util/shell/bash_112.zip
221 ftp://hobbes.nmsu.edu/pub/os2/util/shell/bash-2.0(253).zip
222
223 I haven't looked at either, but the second appears to be a binary-only
224 distribution.  Beware.
225
226 I have received word that Bash (I'm not sure which version, but I
227 believe that it's at least bash-2.02.1) is the standard shell on
228 BeOS.
229
230 A6) How can I build bash with gcc? 
231
232 Bash configures to use gcc by default if it is available.  Read the
233 file INSTALL in the distribution for more information.
234
235 A7)  How can I make bash my login shell?
236
237 Some machines let you use `chsh' to change your login shell.  Other
238 systems use `passwd -s' or `passwd -e'.  If one of these works for
239 you, that's all you need.  Note that many systems require the full
240 pathname to a shell to appear in /etc/shells before you can make it
241 your login shell.  For this, you may need the assistance of your
242 friendly local system administrator. 
243
244 If you cannot do this, you can still use bash as your login shell, but
245 you need to perform some tricks.  The basic idea is to add a command
246 to your login shell's startup file to replace your login shell with
247 bash.
248
249 For example, if your login shell is csh or tcsh, and you have installed
250 bash in /usr/gnu/bin/bash, add the following line to ~/.login:
251
252         if ( -f /usr/gnu/bin/bash ) exec /usr/gnu/bin/bash --login
253
254 (the `--login' tells bash that it is a login shell).
255
256 It's not a good idea to put this command into ~/.cshrc, because every
257 csh you run without the `-f' option, even ones started to run csh scripts,
258 reads that file.  If you must put the command in ~/.cshrc, use something
259 like
260
261         if ( $?prompt ) exec /usr/gnu/bin/bash --login
262
263 to ensure that bash is exec'd only when the csh is interactive.
264
265 If your login shell is sh or ksh, you have to do two things.
266
267 First, create an empty file in your home directory named `.bash_profile'.
268 The existence of this file will prevent the exec'd bash from trying to
269 read ~/.profile, and re-execing itself over and over again.  ~/.bash_profile
270 is the first file bash tries to read initialization commands from when
271 it is invoked as a login shell.
272
273 Next, add a line similar to the above to ~/.profile:
274
275         [ -f /usr/gnu/bin/bash ] && exec /usr/gnu/bin/bash --login
276
277 This will cause login shells to replace themselves with bash running as
278 a login shell.  Once you have this working, you can copy your initialization
279 code from ~/.profile to ~/.bash_profile.
280
281 I have received word that the recipe supplied above is insufficient for
282 machines running CDE.  CDE has a maze of twisty little startup files, all
283 slightly different.
284
285 If you cannot change your login shell in the password file to bash, you
286 will have to (apparently) live with CDE using the shell in the password
287 file to run its startup scripts.  If you have changed your shell to bash,
288 there is code in the CDE startup files (on Solaris, at least) to do the
289 right thing. 
290
291 `dtterm' claims to use $SHELL as the default program to start, so if you
292 can change $SHELL in the CDE startup files, you should be able to use bash
293 in your terminal windows.
294
295 Setting DTSOURCEPROFILE in ~/.dtprofile will cause the `Xsession' program
296 to read your login shell's startup files.  You may be able to use bash for
297 the rest of the CDE programs by setting SHELL to bash in ~/.dtprofile as
298 well, but I have not tried this.
299
300 You can use the above `exec' recipe to start bash when not logging in with
301 CDE by testing the value of the DT variable:
302
303         if [ -n "$DT" ]; then
304                 [ -f /usr/gnu/bin/bash ] && exec /usr/gnu/bin/bash --login
305         fi
306
307
308 A8) I just changed my login shell to bash, and now I can't FTP into my
309    machine.  Why not?
310
311 You must add the full pathname to bash to the file /etc/shells.  As
312 noted in the answer to the previous question, many systems require
313 this before you can make bash your login shell. 
314
315 Most versions of ftpd use this file to prohibit `special' users
316 such as `uucp' and `news' from using FTP. 
317
318 A9)  What's the `POSIX 1003.2 standard'?
319
320 POSIX is a name originally coined by Richard Stallman for a
321 family of open system standards based on UNIX.  There are a
322 number of aspects of UNIX under consideration for
323 standardization, from the basic system services at the system
324 call and C library level to applications and tools to system
325 administration and management.  Each area of standardization is
326 assigned to a working group in the 1003 series. 
327
328 The POSIX Shell and Utilities standard has been developed by IEEE
329 Working Group 1003.2 (POSIX.2).  It concentrates on the command
330 interpreter interface and utility programs commonly executed from
331 the command line or by other programs.  An initial version of the
332 standard has been approved and published by the IEEE, and work is
333 currently underway to update it. 
334
335 Bash is concerned with the aspects of the shell's behavior
336 defined by POSIX.2.  The shell command language has of course
337 been standardized, including the basic flow control and program
338 execution constructs, I/O redirection and pipelining, argument
339 handling, variable expansion, and quoting. 
340
341 The `special' builtins, which must be implemented as part of the
342 shell to provide the desired functionality, are specified as
343 being part of the shell; examples of these are `eval' and
344 `export'.  Other utilities appear in the sections of POSIX.2 not
345 devoted to the shell which are commonly (and in some cases must
346 be) implemented as builtin commands, such as `read' and `test'. 
347 POSIX.2 also specifies aspects of the shell's interactive
348 behavior as part of the UPE, including job control and command
349 line editing.  Only vi-style line editing commands have been
350 standardized; emacs editing commands were left out due to
351 objections.
352
353 A10)  What is the bash `posix mode'?
354
355 Although bash is an implementation of the POSIX.2 shell
356 specification, there are areas where the bash default behavior
357 differs from that spec.  The bash `posix mode' changes the bash
358 behavior in these areas so that it obeys the spec more closely. 
359
360 Posix mode is entered by starting bash with the --posix option or
361 executing `set -o posix' after bash is running.
362
363 The specific aspects of bash which change when posix mode is
364 active are listed in the file CWRU/POSIX.NOTES in the bash
365 distribution.  They are also listed in a section in the Bash
366 Reference Manual.
367
368 Section B:  The latest version
369
370 B1) What's new in version 2.05?
371
372 Bash-2.05 contains the following new features (see the manual page for
373 complete descriptions and the CHANGES and NEWS files in the bash-2.05
374 distribution):
375
376 o This version has once again reverted to using locales and strcoll(3) when
377   processing pattern matching bracket expressions, as POSIX requires. 
378 o Added a new `--init-file' invocation argument as a synonym for `--rcfile',
379   per the new GNU coding standards.
380 o The /dev/tcp and /dev/udp redirections now accept service names as well as
381   port numbers.
382 o `complete' and `compgen' now take a `-o value' option, which controls some
383    of the aspects of that compspec.  Valid values are:
384
385         default - perform bash default completion if programmable
386                   completion produces no matches
387         dirnames - perform directory name completion if programmable
388                    completion produces no matches
389         filenames - tell readline that the compspec produces filenames,
390                     so it can do things like append slashes to
391                     directory names and suppress trailing spaces
392 o A new loadable builtin, realpath, which canonicalizes and expands symlinks
393   in pathname arguments.
394 o When `set' is called without options, it prints function defintions in a
395   way that allows them to be reused as input.  This affects `declare' and 
396   `declare -p' as well.  This only happens when the shell is not in POSIX
397    mode, since POSIX.2 forbids this behavior.
398
399 A short feature history dating from bash-2.0:
400
401 Bash-2.04 introduced the following new features:
402
403 o Programmable word completion with the new `complete' and `compgen' builtins;
404   examples are provided in examples/complete/complete-examples
405 o `history' has a new `-d' option to delete a history entry
406 o `bind' has a new `-x' option to bind key sequences to shell commands
407 o The prompt expansion code has new `\j' and `\l' escape sequences
408 o The `no_empty_cmd_completion' shell option, if enabled, inhibits
409   command completion when TAB is typed on an empty line
410 o `help' has a new `-s' option to print a usage synopsis
411 o New arithmetic operators: var++, var--, ++var, --var, expr1,expr2 (comma)
412 o New ksh93-style arithmetic for command:
413         for ((expr1 ; expr2; expr3 )); do list; done
414 o `read' has new options: `-t', `-n', `-d', `-s'
415 o The redirection code handles several filenames specially:  /dev/fd/N,
416   /dev/stdin, /dev/stdout, /dev/stderr
417 o The redirection code now recognizes /dev/tcp/HOST/PORT and
418   /dev/udp/HOST/PORT and tries to open a TCP or UDP socket, respectively,
419   to the specified port on the specified host
420 o The ${!prefix*} expansion has been implemented
421 o A new FUNCNAME variable, which expands to the name of a currently-executing
422   function
423 o The GROUPS variable is no longer readonly
424 o A new shopt `xpg_echo' variable, to control the behavior of echo with
425   respect to backslash-escape sequences at runtime
426 o The NON_INTERACTIVE_LOGIN_SHELLS #define has returned
427
428 The version of Readline released with Bash-2.04, Readline-4.1, had several
429 new features as well:
430
431 o Parentheses matching is always compiled into readline, and controllable
432   with the new `blink-matching-paren' variable
433 o The history-search-forward and history-search-backward functions now leave
434   point at the end of the line when the search string is empty, like
435   reverse-search-history, and forward-search-history
436 o A new function for applications:  rl_on_new_line_with_prompt()
437 o New variables for applications:  rl_already_prompted, and rl_gnu_readline_p
438
439
440 Bash-2.03 had very few new features, in keeping with the convention
441 that odd-numbered releases provide mainly bug fixes.  A number of new
442 features were added to Readline, mostly at the request of the Cygnus
443 folks.
444
445 A new shopt option, `restricted_shell', so that startup files can test
446         whether or not the shell was started in restricted mode
447 Filename generation is now performed on the words between ( and ) in
448         compound array assignments (this is really a bug fix)
449 OLDPWD is now auto-exported, as POSIX.2 requires
450 ENV and BASH_ENV are read-only variables in a restricted shell
451 Bash may now be linked against an already-installed Readline library,
452         as long as the Readline library is version 4 or newer
453 All shells begun with the `--login' option will source the login shell
454         startup files, even if the shell is not interactive
455
456 There were lots of changes to the version of the Readline library released
457 along with Bash-2.03.  For a complete list of the changes, read the file
458 CHANGES in the Bash-2.03 distribution.
459
460 Bash-2.02 contained the following new features:
461
462 a new version of malloc (based on the old GNU malloc code in previous
463         bash versions) that is more page-oriented, more conservative
464         with memory usage, does not `orphan' large blocks when they
465         are freed, is usable on 64-bit machines, and has allocation
466         checking turned on unconditionally
467 POSIX.2-style globbing character classes ([:alpha:], [:alnum:], etc.)
468 POSIX.2-style globbing equivalence classes
469 POSIX.2-style globbing collating symbols
470 the ksh [[...]] extended conditional command
471 the ksh egrep-style extended pattern matching operators
472 a new `printf' builtin
473 the ksh-like $(<filename) command substitution, which is equivalent to
474         $(cat filename)
475 new tilde prefixes that expand to directories from the directory stack
476 new `**' arithmetic operator to do exponentiation
477 case-insensitive globbing (filename expansion)
478 menu completion a la tcsh
479 `magic-space' history expansion function like tcsh
480 the readline inputrc `language' has a new file inclusion directive ($include)
481
482 Bash-2.01 contained only a few new features:
483
484 new `GROUPS' builtin array variable containing the user's group list
485 new bindable readline commands: history-and-alias-expand-line and
486         alias-expand-line
487
488 Bash-2.0 contained extensive changes and new features from bash-1.14.7.
489 Here's a short list:
490
491 new `time' reserved word to time pipelines, shell builtins, and
492         shell functions
493 one-dimensional arrays with a new compound assignment statement,
494         appropriate expansion constructs and modifications to some
495         of the builtins (read, declare, etc.) to use them
496 new quoting syntaxes for ANSI-C string expansion and locale-specific
497         string translation
498 new expansions to do substring extraction, pattern replacement, and
499         indirect variable expansion
500 new builtins: `disown' and `shopt'
501 new variables: HISTIGNORE, SHELLOPTS, PIPESTATUS, DIRSTACK, GLOBIGNORE,
502                MACHTYPE, BASH_VERSINFO
503 special handling of many unused or redundant variables removed
504         (e.g., $notify, $glob_dot_filenames, $no_exit_on_failed_exec)
505 dynamic loading of new builtin commands; many loadable examples provided
506 new prompt expansions: \a, \e, \n, \H, \T, \@, \v, \V
507 history and aliases available in shell scripts
508 new readline variables: enable-keypad, mark-directories, input-meta,
509         visible-stats, disable-completion, comment-begin
510 new readline commands to manipulate the mark and operate on the region
511 new readline emacs mode commands and bindings for ksh-88 compatibility
512 updated and extended builtins
513 new DEBUG trap
514 expanded (and now documented) restricted shell mode
515
516 implementation stuff:   
517 autoconf-based configuration
518 nearly all of the bugs reported since version 1.14 have been fixed
519 most builtins converted to use builtin `getopt' for consistency
520 most builtins use -p option to display output in a reusable form
521         (for consistency)
522 grammar tighter and smaller (66 reduce-reduce conflicts gone)
523 lots of code now smaller and faster
524 test suite greatly expanded
525
526 B2) Are there any user-visible incompatibilities between bash-2.05 and
527     bash-1.14.7?
528
529 There are a few incompatibilities between version 1.14.7 and version 2.05.
530 They are detailed in the file COMPAT in the bash-2.05 distribution.
531
532 Section C:  Differences from other Unix shells
533
534 C1) How does bash differ from sh, the Bourne shell?
535
536 This is a non-comprehensive list of features that differentiate bash
537 from the SVR4.2 shell.  The bash manual page explains these more
538 completely.
539
540 Things bash has that sh does not:
541         long invocation options
542         `!' reserved word to invert pipeline return value
543         `time' reserved word to time pipelines and shell builtins
544         the `function' reserved word
545         the `select' compound command and reserved word
546         arithmetic for command: for ((expr1 ; expr2; expr3 )); do list; done
547         new $'...' and $"..." quoting
548         the $(...) form of command substitution
549         the $(<filename) form of command substitution, equivalent to
550                 $(cat filename)
551         the ${#param} parameter value length operator
552         the ${!param} indirect parameter expansion operator
553         the ${!param*} prefix expansion operator
554         the ${param:length[:offset]} parameter substring operator
555         the ${param/pat[/string]} parameter pattern substitution operator
556         expansions to perform substring removal (${p%[%]w}, ${p#[#]w})
557         expansion of positional parameters beyond $9 with ${num}
558         variables: BASH, BASH_VERSION, BASH_VERSINFO, UID, EUID, REPLY,
559                    TIMEFORMAT, PPID, PWD, OLDPWD, SHLVL, RANDOM, SECONDS,
560                    LINENO, HISTCMD, HOSTTYPE, OSTYPE, MACHTYPE, HOSTNAME,
561                    ENV, PS3, PS4, DIRSTACK, PIPESTATUS, HISTSIZE, HISTFILE,
562                    HISTFILESIZE, HISTCONTROL, HISTIGNORE, GLOBIGNORE, GROUPS,
563                    PROMPT_COMMAND, FCEDIT, FIGNORE, IGNOREEOF, INPUTRC,
564                    SHELLOPTS, OPTERR, HOSTFILE, TMOUT, FUNCNAME, histchars,
565                    auto_resume
566         DEBUG trap
567         variable arrays with new compound assignment syntax
568         redirections: <>, &>, >|
569         prompt string special char translation and variable expansion
570         auto-export of variables in initial environment
571         command search finds functions before builtins
572         bash return builtin will exit a file sourced with `.'
573         builtins: cd -/-L/-P, exec -l/-c/-a, echo -e/-E, hash -p.
574                   export -n/-f/-p/name=value, pwd -L/-P,
575                   read -e/-p/-a/-t/-n/-d/-s,
576                   readonly -a/-f/name=value, trap -l, set +o,
577                   set -b/-m/-o option/-h/-p/-B/-C/-H/-P,
578                   unset -f/-v, ulimit -m/-p/-u,
579                   type -a/-p/-t, suspend -f, kill -n,
580                   test -o optname/s1 == s2/s1 < s2/s1 > s2/-nt/-ot/-ef/-O/-G/-S
581         bash reads ~/.bashrc for interactive shells, $ENV for non-interactive
582         bash restricted shell mode is more extensive
583         bash allows functions and variables with the same name
584         brace expansion
585         tilde expansion
586         arithmetic expansion with $((...)) and `let' builtin
587         the `[[...]]' extended conditional command
588         process substitution
589         aliases and alias/unalias builtins
590         local variables in functions and `local' builtin
591         readline and command-line editing with programmable completion
592         command history and history/fc builtins
593         csh-like history expansion
594         other new bash builtins: bind, command, compgen, complete, builtin,
595                                  declare/typeset, dirs, enable, fc, help,
596                                  history, logout, popd, pushd, disown, shopt,
597                                  printf
598         exported functions
599         filename generation when using output redirection (command >a*)
600         POSIX.2-style globbing character classes
601         POSIX.2-style globbing equivalence classes
602         POSIX.2-style globbing collating symbols
603         egrep-like extended pattern matching operators
604         case-insensitive pattern matching and globbing
605         variable assignments preceding commands affect only that command,
606                 even for builtins and functions
607         posix mode
608         redirection to /dev/fd/N, /dev/stdin, /dev/stdout, /dev/stderr,
609                 /dev/tcp/host/port, /dev/udp/host/port
610
611 Things sh has that bash does not:
612         uses variable SHACCT to do shell accounting
613         includes `stop' builtin (bash can use alias stop='kill -s STOP')
614         `newgrp' builtin
615         turns on job control if called as `jsh'
616         $TIMEOUT (like bash $TMOUT)
617         `^' is a synonym for `|'
618         new SVR4.2 sh builtins: mldmode, priv
619
620 Implementation differences:
621         redirection to/from compound commands causes sh to create a subshell
622         bash does not allow unbalanced quotes; sh silently inserts them at EOF
623         bash does not mess with signal 11
624         sh sets (euid, egid) to (uid, gid) if -p not supplied and uid < 100
625         bash splits only the results of expansions on IFS, using POSIX.2
626                 field splitting rules; sh splits all words on IFS
627         sh does not allow MAILCHECK to be unset (?)
628         sh does not allow traps on SIGALRM or SIGCHLD
629         bash allows multiple option arguments when invoked (e.g. -x -v);
630                 sh allows only a single option argument (`sh -x -v' attempts
631                 to open a file named `-v', and, on SunOS 4.1.4, dumps core.
632                 On Solaris 2.4 and earlier versions, sh goes into an infinite
633                 loop.)
634         sh exits a script if any builtin fails; bash exits only if one of
635                 the POSIX.2 `special' builtins fails
636
637 C2)  How does bash differ from the Korn shell, version ksh88?
638
639 Things bash has or uses that ksh88 does not:
640         long invocation options
641         `!' reserved word
642         arithmetic for command: for ((expr1 ; expr2; expr3 )); do list; done
643         posix mode and posix conformance
644         command hashing
645         tilde expansion for assignment statements that look like $PATH
646         process substitution with named pipes if /dev/fd is not available
647         the ${!param} indirect parameter expansion operator
648         the ${!param*} prefix expansion operator
649         the ${param:length[:offset]} parameter substring operator
650         the ${param/pat[/string]} parameter pattern substitution operator
651         variables: BASH, BASH_VERSION, BASH_VERSINFO, UID, EUID, SHLVL,
652                    TIMEFORMAT, HISTCMD, HOSTTYPE, OSTYPE, MACHTYPE,
653                    HISTFILESIZE, HISTIGNORE, HISTCONTROL, PROMPT_COMMAND,
654                    IGNOREEOF, FIGNORE, INPUTRC, HOSTFILE, DIRSTACK,
655                    PIPESTATUS, HOSTNAME, OPTERR, SHELLOPTS, GLOBIGNORE,
656                    GROUPS, FUNCNAME, histchars, auto_resume
657         prompt expansion with backslash escapes and command substitution
658         redirection: &> (stdout and stderr)
659         more extensive and extensible editing and programmable completion
660         builtins: bind, builtin, command, declare, dirs, echo -e/-E, enable,
661                   exec -l/-c/-a, fc -s, export -n/-f/-p, hash, help, history,
662                   jobs -x/-r/-s, kill -s/-n/-l, local, logout, popd, pushd,
663                   read -e/-p/-a/-t/-n/-d/-s, readonly -a/-n/-f/-p,
664                   set -o braceexpand/-o histexpand/-o interactive-comments/
665                   -o notify/-o physical/-o posix/-o hashall/-o onecmd/
666                   -h/-B/-C/-b/-H/-P, set +o, suspend, trap -l, type,
667                   typeset -a/-F/-p, ulimit -u, umask -S, alias -p, shopt,
668                   disown, printf, complete, compgen
669         `!' csh-style history expansion
670         POSIX.2-style globbing character classes
671         POSIX.2-style globbing equivalence classes
672         POSIX.2-style globbing collating symbols
673         egrep-like extended pattern matching operators
674         case-insensitive pattern matching and globbing
675         `**' arithmetic operator to do exponentiation
676         redirection to /dev/fd/N, /dev/stdin, /dev/stdout, /dev/stderr
677
678 Things ksh88 has or uses that bash does not:
679         tracked aliases
680         variables: ERRNO, FPATH, EDITOR, VISUAL
681         trap on ERR
682         co-processes (|&, >&p, <&p)
683         weirdly-scoped functions
684         typeset +f to list all function names without definitions
685         text of command history kept in a file, not memory
686         builtins: alias -x, cd old new, fc -e -, newgrp, print,
687                   read -p/-s/-u/var?prompt, set -A/-o gmacs/
688                   -o bgnice/-o markdirs/-o nolog/-o trackall/-o viraw/-s,
689                   typeset -H/-L/-R/-A/-ft/-fu/-fx/-l/-u/-t, whence
690
691 Implementation differences:
692         ksh runs last command of a pipeline in parent shell context
693         bash has brace expansion by default (ksh88 compile-time option)
694         bash has fixed startup file for all interactive shells; ksh reads $ENV
695         bash has exported functions
696         bash command search finds functions before builtins
697
698 C3)  Which new features in ksh-93 are not in bash, and which are?
699
700 New things in ksh-93 not in bash-2.05:
701         associative arrays
702         floating point arithmetic
703         math library functions
704         ${!name[sub]} name of subscript for associative array
705         `.' is allowed in variable names to create a hierarchical namespace
706         more extensive compound assignment syntax
707         discipline functions
708         `sleep' and `getconf' builtins (bash has loadable versions)
709         typeset -n and `nameref' variables
710         KEYBD trap
711         variables: .sh.edchar, .sh.edmode, .sh.edcol, .sh.edtext, .sh.version,
712                    .sh.name, .sh.subscript, .sh.value, HISTEDIT
713         backreferences in pattern matching
714         print -f (bash uses printf)
715         `fc' has been renamed to `hist'
716         `.' can execute shell functions
717
718 New things in ksh-93 present in bash-2.04:
719         for (( expr1; expr2; expr3 )) ; do list; done - arithmetic for command
720         ?:, ++, --, `expr1 , expr2' arithmetic operators
721         expansions: ${!param}, ${param:offset[:len]}, ${param/pat[/str]},
722                     ${!param*}
723         compound array assignment
724         the `!' reserved word
725         loadable builtins -- but ksh uses `builtin' while bash uses `enable'
726         `command', `builtin', `disown' builtins
727         new $'...' and $"..." quoting
728         FIGNORE (but bash uses GLOBIGNORE), HISTCMD
729         set -o notify/-C
730         changes to kill builtin
731         read -A (bash uses read -a)
732         read -t/-d
733         trap -p
734         exec -c/-a
735         `.' restores the positional parameters when it completes
736         POSIX.2 `test'
737         umask -S
738         unalias -a
739         command and arithmetic substitution performed on PS1, PS4, and ENV
740         command name completion
741         ENV processed only for interactive shells
742
743 Section D:  Why does bash do some things differently than other Unix shells?
744
745 D1) Why does bash run a different version of `command' than
746     `which command' says it will?
747
748 On many systems, `which' is actually a csh script that assumes
749 you're running csh.  In tcsh, `which' and its cousin `where'
750 are builtins.  On other Unix systems, `which' is a perl script
751 that uses the PATH environment variable.
752
753 The csh script version reads the csh startup files from your
754 home directory and uses those to determine which `command' will
755 be invoked.  Since bash doesn't use any of those startup files,
756 there's a good chance that your bash environment differs from
757 your csh environment.  The bash `type' builtin does everything
758 `which' does, and will report correct results for the running
759 shell.  If you're really wedded to the name `which', try adding
760 the following function definition to your .bashrc:
761
762         which()
763         {
764                 builtin type "$@"
765         }
766
767 If you're moving from tcsh and would like to bring `where' along
768 as well, use this function:
769
770         where()
771         {
772                 builtin type -a "$@"
773         }
774
775 D2) Why doesn't bash treat brace expansions exactly like csh?
776
777 The only difference between bash and csh brace expansion is that
778 bash requires a brace expression to contain at least one unquoted
779 comma if it is to be expanded.  Any brace-surrounded word not
780 containing an unquoted comma is left unchanged by the brace
781 expansion code.  This affords the greatest degree of sh
782 compatibility. 
783
784 Bash, ksh, zsh, and pd-ksh all implement brace expansion this way. 
785
786 D3) Why doesn't bash have csh variable modifiers?
787
788 Posix has specified a more powerful, albeit somewhat more cryptic,
789 mechanism cribbed from ksh, and bash implements it.
790
791 ${parameter%word}
792         Remove smallest suffix pattern.  The WORD is expanded to produce
793         a pattern.  It then expands to the value of PARAMETER, with the
794         smallest portion of the suffix matched by the pattern deleted.
795
796         x=file.c
797         echo ${x%.c}.o
798         -->file.o
799
800 ${parameter%%word}
801
802         Remove largest suffix pattern.  The WORD is expanded to produce
803         a pattern.  It then expands to the value of PARAMETER, with the
804         largest portion of the suffix matched by the pattern deleted.
805
806         x=posix/src/std
807         echo ${x%%/*}
808         -->posix
809
810 ${parameter#word}
811         Remove smallest prefix pattern.  The WORD is expanded to produce
812         a pattern.  It then expands to the value of PARAMETER, with the
813         smallest portion of the prefix matched by the pattern deleted.
814
815         x=$HOME/src/cmd
816         echo ${x#$HOME}
817         -->/src/cmd
818
819 ${parameter##word}
820         Remove largest prefix pattern.  The WORD is expanded to produce
821         a pattern.  It then expands to the value of PARAMETER, with the
822         largest portion of the prefix matched by the pattern deleted.
823
824         x=/one/two/three
825         echo ${x##*/}
826         -->three
827
828
829 Given
830         a=/a/b/c/d
831         b=b.xxx
832
833         csh                     bash            result
834         ---                     ----            ------
835         $a:h                    ${a%/*}            /a/b/c
836         $a:t                    ${a##*/}           d
837         $b:r                    ${b%.*}            b
838         $b:e                    ${b##*.}           xxx
839
840
841 D4) How can I make my csh aliases work when I convert to bash?
842
843 Bash uses a different syntax to support aliases than csh does. 
844 The details can be found in the documentation.  We have provided
845 a shell script which does most of the work of conversion for you;
846 this script can be found in ./examples/misc/aliasconv.sh.  Here is
847 how you use it:
848   
849 Start csh in the normal way for you.  (e.g., `csh')
850   
851 Pipe the output of `alias' through `aliasconv.sh', saving the
852 results into `bash_aliases':
853   
854         alias | bash aliasconv.sh >bash_aliases
855   
856 Edit `bash_aliases', carefully reading through any created
857 functions.  You will need to change the names of some csh specific
858 variables to the bash equivalents.  The script converts $cwd to
859 $PWD, $term to $TERM, $home to $HOME, $user to $USER, and $prompt
860 to $PS1.  You may also have to add quotes to avoid unwanted
861 expansion.
862
863 For example, the csh alias:
864   
865         alias cd 'cd \!*; echo $cwd'
866   
867 is converted to the bash function:
868
869         cd () { command cd "$@"; echo $PWD ; }
870
871 The only thing that needs to be done is to quote $PWD:
872   
873         cd () { command cd "$@"; echo "$PWD" ; }
874   
875 Merge the edited file into your ~/.bashrc.
876
877 There is an additional, more ambitious, script in
878 examples/misc/cshtobash that attempts to convert your entire csh
879 environment to its bash equivalent.  This script can be run as
880 simply `cshtobash' to convert your normal interactive
881 environment, or as `cshtobash ~/.login' to convert your login
882 environment. 
883
884 D5) How can I pipe standard output and standard error from one command to
885     another, like csh does with `|&'?
886
887 Use
888         command 2>&1 | command2
889
890 The key is to remember that piping is performed before redirection, so
891 file descriptor 1 points to the pipe when it is duplicated onto file
892 descriptor 2.
893
894 D6) Now that I've converted from ksh to bash, are there equivalents to
895     ksh features like autoloaded functions and the `whence' command?
896
897 There are features in ksh-88 and ksh-93 that do not have direct bash
898 equivalents.  Most, however, can be emulated with very little trouble.
899
900 ksh-88 feature          Bash equivalent
901 --------------          ---------------
902 compiled-in aliases     set up aliases in .bashrc; some ksh aliases are
903                         bash builtins (hash, history, type)
904 coprocesses             named pipe pairs (one for read, one for write)
905 typeset +f              declare -F
906 cd, print, whence       function substitutes in examples/functions/kshenv
907 autoloaded functions    examples/functions/autoload is the same as typeset -fu
908 read var?prompt         read -p prompt var
909
910 ksh-93 feature          Bash equivalent
911 --------------          ---------------
912 sleep, getconf          Bash has loadable versions in examples/loadables
913 ${.sh.version}          $BASH_VERSION
914 print -f                printf
915 hist                    alias fc=hist
916 $HISTEDIT               $FCEDIT
917
918 Section E:  How can I get bash to do certain things, and why does bash do
919             things the way it does?
920
921 E1) Why is the bash builtin `test' slightly different from /bin/test?
922
923 The specific example used here is [ ! x -o x ], which is false.
924
925 Bash's builtin `test' implements the Posix.2 spec, which can be
926 summarized as follows (the wording is due to David Korn):
927    
928 Here is the set of rules for processing test arguments.
929   
930     0 Args: False
931     1 Arg:  True iff argument is not null.
932     2 Args: If first arg is !, True iff second argument is null.
933             If first argument is unary, then true if unary test is true
934             Otherwise error.
935     3 Args: If second argument is a binary operator, do binary test of $1 $3
936             If first argument is !, negate two argument test of $2 $3
937             If first argument is `(' and third argument is `)', do the
938             one-argument test of the second argument.
939             Otherwise error.
940     4 Args: If first argument is !, negate three argument test of $2 $3 $4.
941             Otherwise unspecified
942     5 or more Args: unspecified.  (Historical shells would use their
943     current algorithm).
944    
945 The operators -a and -o are considered binary operators for the purpose
946 of the 3 Arg case.
947    
948 As you can see, the test becomes (not (x or x)), which is false.
949
950 E2) Why does bash sometimes say `Broken pipe'?
951
952 If a sequence of commands appears in a pipeline, and one of the
953 reading commands finishes before the writer has finished, the
954 writer receives a SIGPIPE signal.  Many other shells special-case
955 SIGPIPE as an exit status in the pipeline and do not report it. 
956 For example, in:
957   
958       ps -aux | head
959   
960 `head' can finish before `ps' writes all of its output, and ps
961 will try to write on a pipe without a reader.  In that case, bash
962 will print `Broken pipe' to stderr when ps is killed by a
963 SIGPIPE. 
964
965 You can build a version of bash that will not report SIGPIPE errors
966 by uncommenting the definition of DONT_REPORT_SIGPIPE in the file
967 config-top.h.
968
969 E3) When I have terminal escape sequences in my prompt, why does bash
970     wrap lines at the wrong column?
971
972 Readline, the line editing library that bash uses, does not know
973 that the terminal escape sequences do not take up space on the
974 screen.  The redisplay code assumes, unless told otherwise, that
975 each character in the prompt is a `printable' character that
976 takes up one character position on the screen. 
977
978 You can use the bash prompt expansion facility (see the PROMPTING
979 section in the manual page) to tell readline that sequences of
980 characters in the prompt strings take up no screen space. 
981
982 Use the \[ escape to begin a sequence of non-printing characters,
983 and the \] escape to signal the end of such a sequence. 
984
985 E4) If I pipe the output of a command into `read variable', why doesn't
986     the output show up in $variable when the read command finishes?
987
988 This has to do with the parent-child relationship between Unix
989 processes.  It affects all commands run in pipelines, not just
990 simple calls to `read'.  For example, piping a command's output
991 into a `while' loop that repeatedly calls `read' will result in
992 the same behavior.
993
994 Each element of a pipeline runs in a separate process, a child of
995 the shell running the pipeline.  A subprocess cannot affect its
996 parent's environment.  When the `read' command sets the variable
997 to the input, that variable is set only in the subshell, not the
998 parent shell.  When the subshell exits, the value of the variable
999 is lost. 
1000
1001 Many pipelines that end with `read variable' can be converted
1002 into command substitutions, which will capture the output of
1003 a specified command.  The output can then be assigned to a
1004 variable:
1005
1006         grep ^gnu /usr/lib/news/active | wc -l | read ngroup
1007
1008 can be converted into
1009
1010         ngroup=$(grep ^gnu /usr/lib/news/active | wc -l)
1011
1012 This does not, unfortunately, work to split the text among
1013 multiple variables, as read does when given multiple variable
1014 arguments.  If you need to do this, you can either use the
1015 command substitution above to read the output into a variable
1016 and chop up the variable using the bash pattern removal
1017 expansion operators or use some variant of the following
1018 approach.
1019
1020 Say /usr/local/bin/ipaddr is the following shell script:
1021
1022 #! /bin/sh
1023 host `hostname` | awk '/address/ {print $NF}'
1024
1025 Instead of using
1026
1027         /usr/local/bin/ipaddr | read A B C D
1028
1029 to break the local machine's IP address into separate octets, use
1030
1031         OIFS="$IFS"
1032         IFS=.
1033         set -- $(/usr/local/bin/ipaddr)
1034         IFS="$OIFS"
1035         A="$1" B="$2" C="$3" D="$4"
1036
1037 Beware, however, that this will change the shell's positional
1038 parameters.  If you need them, you should save them before doing
1039 this.
1040
1041 This is the general approach -- in most cases you will not need to
1042 set $IFS to a different value.
1043
1044 E5) I have a bunch of shell scripts that use backslash-escaped characters
1045     in arguments to `echo'.  Bash doesn't interpret these characters.  Why
1046     not, and how can I make it understand them?
1047
1048 This is the behavior of echo on most Unix System V machines.
1049
1050 The bash builtin `echo' is modeled after the 9th Edition
1051 Research Unix version of `echo'.  It does not interpret
1052 backslash-escaped characters in its argument strings by default;
1053 it requires the use of the -e option to enable the
1054 interpretation.  The System V echo provides no way to disable the
1055 special characters; the bash echo has a -E option to disable
1056 them. 
1057
1058 There is a configuration option that will make bash behave like
1059 the System V echo and interpret things like `\t' by default.  Run
1060 configure with the --enable-usg-echo-default option to turn this
1061 on.  Be aware that this will cause some of the tests run when you
1062 type `make tests' to fail.
1063
1064 There is a shell option, `xpg_echo', settable with `shopt' that will
1065 change the behavior of echo at runtime.  Enabling this option turns
1066 on expansion of backslash-escape sequences.
1067
1068 E6) Why doesn't a while or for loop get suspended when I type ^Z?
1069
1070 This is a consequence of how job control works on Unix.  The only
1071 thing that can be suspended is the process group.  This is a single
1072 command or pipeline of commands that the shell forks and executes.
1073
1074 When you run a while or for loop, the only thing that the shell forks
1075 and executes are any commands in the while loop test and commands in
1076 the loop bodies.  These, therefore, are the only things that can be
1077 suspended when you type ^Z.
1078
1079 If you want to be able to stop the entire loop, you need to put it
1080 within parentheses, which will force the loop into a subshell that
1081 may be stopped (and subsequently restarted) as a single unit.
1082
1083 E7) What about empty for loops in Makefiles?
1084
1085 It's fairly common to see constructs like this in automatically-generated
1086 Makefiles:
1087
1088 SUBDIRS = @SUBDIRS@
1089
1090         ...
1091
1092 subdirs-clean:
1093         for d in ${SUBDIRS}; do \
1094                 ( cd $$d && ${MAKE} ${MFLAGS} clean ) \
1095         done
1096
1097 When SUBDIRS is empty, this results in a command like this being passed to
1098 bash:
1099
1100         for d in ; do
1101                 ( cd $d && ${MAKE} ${MFLAGS} clean )
1102         done
1103
1104 This is a syntax error.  If the reserved word `in' is present, a word must
1105 follow it before the semicolon or newline.  The language in the manual page
1106 referring to the list of words being empty refers to the list after it is
1107 expanded.  There must be at least one word following the `in' when the
1108 construct is parsed.
1109
1110 The idiomatic Makefile solution is something like:
1111
1112 SUBDIRS = @SUBDIRS@
1113
1114 subdirs-clean:
1115         subdirs=$SUBDIRS ; for d in $$subdirs; do \
1116                 ( cd $$d && ${MAKE} ${MFLAGS} clean ) \
1117         done
1118
1119
1120 The POSIX.2 interpretation committee has considered this issue and declared
1121 that the bash implemenation is correct, according to the standard:
1122
1123 http://www.pasc.org/interps/unofficial/db/p1003.2/pasc-1003.2-169.html
1124
1125 E8) Why does the arithmetic evaluation code complain about `08'?
1126
1127 The bash arithmetic evaluation code (used for `let', $(()), (()), and in
1128 other places), interprets a leading `0' in numeric constants as denoting
1129 an octal number, and a leading `0x' as denoting hexadecimal.  This is
1130 in accordance with the POSIX.2 spec, section 2.9.2.1, which states that
1131 arithmetic constants should be handled as signed long integers as defined
1132 by the ANSI/ISO C standard.
1133
1134 The POSIX.2 interpretation committee has confirmed this:
1135
1136 http://www.pasc.org/interps/unofficial/db/p1003.2/pasc-1003.2-173.html
1137
1138 E9) Why does the pattern matching expression [A-Z]* match files beginning
1139     with every letter except `z'?
1140
1141 Bash-2.05 and later versions have reverted to the bash-2.03 behavior of
1142 honoring the current locale setting when processing ranges within pattern
1143 matching bracket expressions ([A-Z]).  This is what POSIX.2 and SUSv2/XPG5
1144 specify. 
1145
1146 The behavior of the matcher in bash-2.05 depends on the current LC_COLLATE
1147 setting.  Setting this variable to `C' or `POSIX' will result in the
1148 traditional behavior ([A-Z] matches all uppercase ASCII characters).
1149 Many other locales, including the en_US locale (the default on many US
1150 versions of Linux) collate the upper and lower case letters like this:
1151
1152         AaBb...Zz
1153
1154 which means that [A-Z] matches every letter except `z'.
1155
1156 The portable way to specify upper case letters is [:upper:] instead of
1157 A-Z; lower case may be specified as [:lower:] instead of a-z.
1158
1159 Look at the manual pages for setlocale(3), strcoll(3), and, if it is
1160 present, locale(1).  If you have locale(1), you can use it to find
1161 your current locale information even if you do not have any of the
1162 LC_ variables set.
1163
1164 My advice is to put
1165
1166         export LC_COLLATE=C
1167
1168 into /etc/profile and inspect any shell scripts run from cron for
1169 constructs like [A-Z].  This will prevent things like
1170
1171         rm [A-Z]*
1172
1173 from removing every file in the current directory except those beginning
1174 with `z' and still allow individual users to change the collation order.
1175 Users may put the above command into their own profiles as well, of course.
1176
1177 Section F:  Things to watch out for on certain Unix versions
1178
1179 F1) Why can't I use command line editing in my `cmdtool'?
1180
1181 The problem is `cmdtool' and bash fighting over the input.  When
1182 scrolling is enabled in a cmdtool window, cmdtool puts the tty in
1183 `raw mode' to permit command-line editing using the mouse for
1184 applications that cannot do it themselves.  As a result, bash and
1185 cmdtool each try to read keyboard input immediately, with neither
1186 getting enough of it to be useful.
1187
1188 This mode also causes cmdtool to not implement many of the
1189 terminal functions and control sequences appearing in the
1190 `sun-cmd' termcap entry.  For a more complete explanation, see
1191 that file examples/suncmd.termcap in the bash distribution. 
1192
1193 `xterm' is a better choice, and gets along with bash much more
1194 smoothly.
1195
1196 If you must use cmdtool, you can use the termcap description in
1197 examples/suncmd.termcap.  Set the TERMCAP variable to the terminal
1198 description contained in that file, i.e.
1199
1200 TERMCAP='Mu|sun-cmd:am:bs:km:pt:li#34:co#80:cl=^L:ce=\E[K:cd=\E[J:rs=\E[s:'
1201
1202 Then export TERMCAP and start a new cmdtool window from that shell.
1203 The bash command-line editing should behave better in the new
1204 cmdtool.  If this works, you can put the assignment to TERMCAP
1205 in your bashrc file.
1206
1207 F2) I built bash on Solaris 2.  Why do globbing expansions and filename
1208     completion chop off the first few characters of each filename?
1209
1210 This is the consequence of building bash on SunOS 5 and linking
1211 with the libraries in /usr/ucblib, but using the definitions
1212 and structures from files in /usr/include. 
1213
1214 The actual conflict is between the dirent structure in
1215 /usr/include/dirent.h and the struct returned by the version of
1216 `readdir' in libucb.a (a 4.3-BSD style `struct direct'). 
1217
1218 Make sure you've got /usr/ccs/bin ahead of /usr/ucb in your $PATH
1219 when configuring and building bash.  This will ensure that you
1220 use /usr/ccs/bin/cc or acc instead of /usr/ucb/cc and that you
1221 link with libc before libucb. 
1222
1223 If you have installed the Sun C compiler, you may also need to
1224 put /usr/ccs/bin and /opt/SUNWspro/bin into your $PATH before
1225 /usr/ucb.
1226
1227 F3) Why does bash dump core after I interrupt username completion or
1228     `~user' tilde expansion on a machine running NIS?
1229
1230 This is a famous and long-standing bug in the SunOS YP (sorry, NIS)
1231 client library, which is part of libc.
1232
1233 The YP library code keeps static state -- a pointer into the data
1234 returned from the server.  When YP initializes itself (setpwent),
1235 it looks at this pointer and calls free on it if it's non-null. 
1236 So far, so good. 
1237
1238 If one of the YP functions is interrupted during getpwent (the
1239 exact function is interpretwithsave()), and returns NULL, the
1240 pointer is freed without being reset to NULL, and the function
1241 returns.  The next time getpwent is called, it sees that this
1242 pointer is non-null, calls free, and the bash free() blows up
1243 because it's being asked to free freed memory. 
1244
1245 The traditional Unix mallocs allow memory to be freed multiple
1246 times; that's probably why this has never been fixed.  You can
1247 run configure with the `--without-gnu-malloc' option to use
1248 the C library malloc and avoid the problem.
1249
1250 F4) I'm running SVR4.2.  Why is the line erased every time I type `@'?
1251
1252 The `@' character is the default `line kill' character in most
1253 versions of System V, including SVR4.2.  You can change this
1254 character to whatever you want using `stty'.  For example, to
1255 change the line kill character to control-u, type
1256
1257         stty kill ^U
1258
1259 where the `^' and `U' can be two separate characters.
1260
1261 F5) Why does bash report syntax errors when my C News scripts use a
1262     redirection before a subshell command?
1263
1264 The actual command in question is something like
1265
1266         < file ( command )
1267
1268 According to the grammar given in the POSIX.2 standard, this construct
1269 is, in fact, a syntax error.  Redirections may only precede `simple
1270 commands'.  A subshell construct such as the above is one of the shell's
1271 `compound commands'.  A redirection may only follow a compound command.
1272
1273 This affects the mechanical transformation of commands that use `cat'
1274 to pipe a file into a command (a favorite Useless-Use-Of-Cat topic on
1275 comp.unix.shell).  While most commands of the form
1276
1277         cat file | command
1278
1279 can be converted to `< file command', shell control structures such as
1280 loops and subshells require `command < file'.
1281
1282 The file CWRU/sh-redir-hack in the bash-2.05 distribution is an
1283 (unofficial) patch to parse.y that will modify the grammar to
1284 support this construct.  It will not apply with `patch'; you must
1285 modify parse.y by hand.  Note that if you apply this, you must
1286 recompile with -DREDIRECTION_HACK.  This introduces a large
1287 number of reduce/reduce conflicts into the shell grammar. 
1288
1289 F6) Why can't I use vi-mode editing on Red Hat Linux 6.1?
1290
1291 The short answer is that Red Hat screwed up.
1292
1293 The long answer is that they shipped an /etc/inputrc that only works
1294 for emacs mode editing, and then screwed all the vi users by setting
1295 INPUTRC to /etc/inputrc in /etc/profile.
1296
1297 The short fix is to do one of the following: remove or rename
1298 /etc/inputrc, set INPUTRC=~/.inputrc in ~/.bashrc (or .bash_profile,
1299 but make sure you export it if you do), remove the assignment to
1300 INPUTRC from /etc/profile, add
1301
1302         set keymap emacs
1303
1304 to the beginning of /etc/inputrc, or bracket the key bindings in
1305 /etc/inputrc with these lines
1306
1307         $if mode=emacs
1308                 [...]
1309         $endif
1310
1311 Section G:  How can I get bash to do certain common things?
1312
1313 G1) How can I get bash to read and display eight-bit characters?
1314
1315 This is a process requiring several steps.
1316
1317 First, you must ensure that the `physical' data path is a full eight
1318 bits.  For xterms, for example, the `vt100' resources `eightBitInput'
1319 and `eightBitOutput' should be set to `true'.
1320
1321 Once you have set up an eight-bit path, you must tell the kernel and
1322 tty driver to leave the eighth bit of characters alone when processing
1323 keyboard input.  Use `stty' to do this:
1324
1325         stty cs8 -istrip -parenb
1326
1327 For old BSD-style systems, you can use
1328
1329         stty pass8
1330
1331 You may also need
1332
1333         stty even odd
1334
1335 Finally, you need to tell readline that you will be inputting and
1336 displaying eight-bit characters.  You use readline variables to do
1337 this.  These variables can be set in your .inputrc or using the bash
1338 `bind' builtin.  Here's an example using `bind':
1339
1340         bash$ bind 'set convert-meta off'
1341         bash$ bind 'set meta-flag on'
1342         bash$ bind 'set output-meta on'
1343
1344 The `set' commands between the single quotes may also be placed
1345 in ~/.inputrc.
1346
1347 G2) How do I write a function `x' to replace builtin command `x', but
1348     still invoke the command from within the function?
1349
1350 This is why the `command' and `builtin' builtins exist.  The
1351 `command' builtin executes the command supplied as its first
1352 argument, skipping over any function defined with that name.  The
1353 `builtin' builtin executes the builtin command given as its first
1354 argument directly. 
1355
1356 For example, to write a function to replace `cd' that writes the
1357 hostname and current directory to an xterm title bar, use
1358 something like the following:
1359
1360         cd()
1361         {
1362                 builtin cd "$@" && xtitle "$HOST: $PWD"
1363         }
1364
1365 This could also be written using `command' instead of `builtin';
1366 the version above is marginally more efficient. 
1367
1368 G3) How can I find the value of a shell variable whose name is the value
1369     of another shell variable?
1370
1371 Versions of Bash newer than Bash-2.0 support this directly.  You can use 
1372
1373         ${!var}
1374
1375 For example, the following sequence of commands will echo `z':
1376
1377         var1=var2
1378         var2=z
1379         echo ${!var1}
1380
1381 For sh compatibility, use the `eval' builtin.  The important
1382 thing to remember is that `eval' expands the arguments you give
1383 it again, so you need to quote the parts of the arguments that
1384 you want `eval' to act on. 
1385
1386 For example, this expression prints the value of the last positional
1387 parameter:
1388
1389         eval echo \"\$\{$#\}\"
1390
1391 The expansion of the quoted portions of this expression will be
1392 deferred until `eval' runs, while the `$#' will be expanded
1393 before `eval' is executed.  In versions of bash later than bash-2.0,
1394
1395         echo ${!#}
1396
1397 does the same thing.
1398
1399 G4) How can I make the bash `time' reserved word print timing output that
1400      looks like the output from my system's /usr/bin/time?
1401
1402 The bash command timing code looks for a variable `TIMEFORMAT' and
1403 uses its value as a format string to decide how to display the
1404 timing statistics.
1405
1406 The value of TIMEFORMAT is a string with `%' escapes expanded in a
1407 fashion similar in spirit to printf(3).  The manual page explains
1408 the meanings of the escape sequences in the format string.
1409
1410 If TIMEFORMAT is not set, bash acts as if the following assignment had
1411 been performed:
1412
1413         TIMEFORMAT=$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
1414
1415 The POSIX.2 default time format (used by `time -p command') is
1416
1417         TIMEFORMAT=$'real %2R\nuser %2U\nsys %2S'
1418
1419 The BSD /usr/bin/time format can be emulated with:
1420
1421         TIMEFORMAT=$'\t%1R real\t%1U user\t%1S sys'
1422
1423 The System V /usr/bin/time format can be emulated with:
1424
1425         TIMEFORMAT=$'\nreal\t%1R\nuser\t%1U\nsys\t%1S'
1426
1427 The ksh format can be emulated with:
1428
1429         TIMEFORMAT=$'\nreal\t%2lR\nuser\t%2lU\nsys\t%2lS'
1430
1431 G5) How do I get the current directory into my prompt?
1432
1433 Bash provides a number of backslash-escape sequences which are expanded
1434 when the prompt string (PS1 or PS2) is displayed.  The full list is in
1435 the manual page.
1436
1437 The \w expansion gives the full pathname of the current directory, with
1438 a tilde (`~') substituted for the current value of $HOME.  The \W
1439 expansion gives the basename of the current directory.  To put the full
1440 pathname of the current directory into the path without any tilde
1441 subsitution, use $PWD.  Here are some examples:
1442
1443         PS1='\w$ '      # current directory with tilde
1444         PS1='\W$ '      # basename of current directory
1445         PS1='$PWD$ '    # full pathname of current directory
1446
1447 The single quotes are important in the final example to prevent $PWD from
1448 being expanded when the assignment to PS1 is performed.
1449
1450 G6) How can I rename "*.foo" to "*.bar"?
1451
1452 Use the pattern removal functionality described in D3.  The following `for'
1453 loop will do the trick:
1454
1455         for f in *.foo; do
1456                 mv $f ${f%foo}bar
1457         done
1458
1459 G7) How can I translate a filename from uppercase to lowercase?
1460
1461 The script examples/functions/lowercase, originally written by John DuBois,
1462 will do the trick.  The converse is left as an exercise.
1463
1464 G8) How can I write a filename expansion (globbing) pattern that will match
1465     all files in the current directory except "." and ".."?
1466
1467 You must have set the `extglob' shell option using `shopt -s extglob' to use
1468 this:
1469
1470         echo .!(.|) *
1471
1472 A solution that works without extended globbing is given in the Unix Shell
1473 FAQ, posted periodically to comp.unix.shell.
1474
1475 Section H:  Where do I go from here?
1476
1477 H1) How do I report bugs in bash, and where should I look for fixes and
1478     advice?
1479
1480 Use the `bashbug' script to report bugs.  It is built and
1481 installed at the same time as bash.  It provides a standard
1482 template for reporting a problem and automatically includes
1483 information about your configuration and build environment. 
1484
1485 `bashbug' sends its reports to bug-bash@gnu.org, which
1486 is a large mailing list gatewayed to the usenet newsgroup gnu.bash.bug. 
1487
1488 Bug fixes, answers to questions, and announcements of new releases
1489 are all posted to gnu.bash.bug.  Discussions concerning bash features
1490 and problems also take place there.
1491
1492 To reach the bash maintainers directly, send mail to
1493 bash-maintainers@gnu.org.
1494
1495 H2) What kind of bash documentation is there?
1496
1497 First, look in the doc directory in the bash distribution.  It should
1498 contain at least the following files:
1499
1500 bash.1          an extensive, thorough Unix-style manual page
1501 builtins.1      a manual page covering just bash builtin commands
1502 bashref.texi    a reference manual in GNU tex`info format
1503 bashref.info    an info version of the reference manual
1504 FAQ             this file
1505 article.ms      text of an article written for The Linux Journal
1506 readline.3      a man page describing readline
1507
1508 Postscript, HTML, and ASCII files created from the above source are
1509 available in the documentation distribution.
1510
1511 There is additional documentation available for anonymous FTP from host
1512 ftp.cwru.edu in the `pub/bash' directory.
1513
1514 Cameron Newham and Bill Rosenblatt have written a book on bash, published
1515 by O'Reilly and Associates.  The book is based on Bill Rosenblatt's Korn
1516 Shell book.  The title is ``Learning the Bash Shell'', and the ISBN number
1517 is 1-56592-147-X.  Look for it in fine bookstores near you.  This book
1518 covers bash-1.14, but has an appendix describing some of the new features
1519 in bash-2.0.  
1520
1521 A second edition of this book is available, published in January, 1998.
1522 The ISBN number is 1-56592-347-2.  Look for it in the same fine bookstores
1523 or on the web.
1524
1525 H3) What's coming in future versions?
1526
1527 These are features I plan to include in a future version of bash.
1528
1529 a bash debugger (a minimally-tested version is included with bash-2.05)
1530 associative arrays
1531 changes to the DEBUG trap to be compatible with ksh93 (which runs the
1532 trap before each simple command, instead of after each one like previous
1533 versions)
1534 an implementation of the ksh-like ERR trap
1535
1536 H4) What's on the bash `wish list' for future versions?
1537
1538 These are features that may or may not appear in a future version of bash.
1539
1540 breaking some of the shell functionality into embeddable libraries
1541 a module system like zsh's, using dynamic loading like builtins
1542 better internationalization using GNU `gettext'
1543 an option to use external files for the long `help' text
1544 date-stamped command history
1545 a bash programmer's guide with a chapter on creating loadable builtins
1546 a better loadable interface to perl with access to the shell builtins and
1547         variables (contributions gratefully accepted)
1548
1549 H5) When will the next release appear?
1550
1551 The next version will appear sometime in 2001 or 2002.  Never make
1552 predictions. 
1553
1554
1555 This document is Copyright 1995-2001 by Chester Ramey.
1556
1557 Permission is hereby granted, without written agreement and
1558 without license or royalty fees, to use, copy, and distribute
1559 this document for any purpose, provided that the above copyright
1560 notice appears in all copies of this document and that the
1561 contents of this document remain unaltered.