source: Daodan/MSYS2/usr/share/doc/bash/FAQ@ 1167

Last change on this file since 1167 was 1166, checked in by rossy, 3 years ago

Daodan: Replace MinGW build env with an up-to-date MSYS2 env

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