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