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