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