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