blob: 2a4c7526c58a7b2bc217cd7dec1230089ac77e7f [file] [log] [blame]
Jari Aaltof73dda02001-11-13 17:56:06 +00001This is the Bash FAQ, version 3.17, for Bash version 2.05a.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002
3This document contains a set of frequently-asked questions concerning
4Bash, the GNU Bourne-Again Shell. Bash is a freely-available command
5interpreter with advanced features for both interactive use and shell
6programming.
7
8Another good source of basic information about shells is the collection
9of FAQ articles periodically posted to comp.unix.shell.
10
11Questions and comments concerning this document should be sent to
12chet@po.cwru.edu.
13
14This document is available for anonymous FTP with the URL
15
Jari Aaltocce855b1998-04-17 19:52:44 +000016ftp://ftp.cwru.edu/pub/bash/FAQ
Jari Aaltoccc6cda1996-12-23 17:02:34 +000017
Jari Aaltobb706242000-03-17 21:46:59 +000018The Bash home page is http://cnswww.cns.cwru.edu/~chet/bash/bashtop.html
19
Jari Aaltoccc6cda1996-12-23 17:02:34 +000020----------
21Contents:
22
23Section A: The Basics
24
Jari Aaltob72432f1999-02-19 17:11:39 +000025A1) 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 1003.2 standard'?
35A10) What is the bash `posix mode'?
Jari Aaltoccc6cda1996-12-23 17:02:34 +000036
37Section B: The latest version
38
Jari Aaltof73dda02001-11-13 17:56:06 +000039B1) What's new in version 2.05a?
40B2) Are there any user-visible incompatibilities between bash-2.05a and
Jari Aaltoccc6cda1996-12-23 17:02:34 +000041 bash-1.14.7?
42
43Section C: Differences from other Unix shells
44
Jari Aaltob72432f1999-02-19 17:11:39 +000045C1) 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?
Jari Aaltoccc6cda1996-12-23 17:02:34 +000048
49Section D: Why does bash do some things differently than other Unix shells?
50
Jari Aaltob72432f1999-02-19 17:11:39 +000051D1) Why does bash run a different version of `command' than
Jari Aaltoccc6cda1996-12-23 17:02:34 +000052 `which command' says it will?
Jari Aaltob72432f1999-02-19 17:11:39 +000053D2) 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
Jari Aaltoccc6cda1996-12-23 17:02:34 +000057 another, like csh does with `|&'?
Jari Aaltob72432f1999-02-19 17:11:39 +000058D6) Now that I've converted from ksh to bash, are there equivalents to
Jari Aaltoccc6cda1996-12-23 17:02:34 +000059 ksh features like autoloaded functions and the `whence' command?
60
Jari Aaltobb706242000-03-17 21:46:59 +000061Section E: Why does bash do certain things the way it does?
Jari Aaltoccc6cda1996-12-23 17:02:34 +000062
Jari Aaltob72432f1999-02-19 17:11:39 +000063E1) Why is the bash builtin `test' slightly different from /bin/test?
64E2) Why does bash sometimes say `Broken pipe'?
Jari Aaltobb706242000-03-17 21:46:59 +000065E3) When I have terminal escape sequences in my prompt, why does bash
Jari Aaltoccc6cda1996-12-23 17:02:34 +000066 wrap lines at the wrong column?
Jari Aaltobb706242000-03-17 21:46:59 +000067E4) If I pipe the output of a command into `read variable', why doesn't
Jari Aaltoccc6cda1996-12-23 17:02:34 +000068 the output show up in $variable when the read command finishes?
Jari Aaltobb706242000-03-17 21:46:59 +000069E5) I have a bunch of shell scripts that use backslash-escaped characters
Jari Aaltoccc6cda1996-12-23 17:02:34 +000070 in arguments to `echo'. Bash doesn't interpret these characters. Why
71 not, and how can I make it understand them?
Jari Aaltobb706242000-03-17 21:46:59 +000072E6) Why doesn't a while or for loop get suspended when I type ^Z?
Jari Aalto28ef6c32001-04-06 19:14:31 +000073E7) 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'?
Jari Aaltoccc6cda1996-12-23 17:02:34 +000077
78Section F: Things to watch out for on certain Unix versions
79
Jari Aaltob72432f1999-02-19 17:11:39 +000080F1) Why can't I use command line editing in my `cmdtool'?
81F2) I built bash on Solaris 2. Why do globbing expansions and filename
Jari Aaltoccc6cda1996-12-23 17:02:34 +000082 completion chop off the first few characters of each filename?
Jari Aaltob72432f1999-02-19 17:11:39 +000083F3) Why does bash dump core after I interrupt username completion or
Jari Aaltoccc6cda1996-12-23 17:02:34 +000084 `~user' tilde expansion on a machine running NIS?
Jari Aaltob72432f1999-02-19 17:11:39 +000085F4) I'm running SVR4.2. Why is the line erased every time I type `@'?
86F5) Why does bash report syntax errors when my C News scripts use a
Jari Aaltoccc6cda1996-12-23 17:02:34 +000087 redirection before a subshell command?
Jari Aaltobb706242000-03-17 21:46:59 +000088F6) Why can't I use vi-mode editing on Red Hat Linux 6.1?
Jari Aaltoccc6cda1996-12-23 17:02:34 +000089
Jari Aaltobb706242000-03-17 21:46:59 +000090Section G: How can I get bash to do certain common things?
Jari Aaltoccc6cda1996-12-23 17:02:34 +000091
Jari Aaltobb706242000-03-17 21:46:59 +000092G1) How can I get bash to read and display eight-bit characters?
93G2) How do I write a function `x' to replace builtin command `x', but
94 still invoke the command from within the function?
95G3) How can I find the value of a shell variable whose name is the value
96 of another shell variable?
97G4) How can I make the bash `time' reserved word print timing output that
98 looks like the output from my system's /usr/bin/time?
99G5) How do I get the current directory into my prompt?
100G6) How can I rename "*.foo" to "*.bar"?
101G7) How can I translate a filename from uppercase to lowercase?
102G8) How can I write a filename expansion (globbing) pattern that will match
103 all files in the current directory except "." and ".."?
104
105Section H: Where do I go from here?
106
107H1) How do I report bugs in bash, and where should I look for fixes and
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000108 advice?
Jari Aaltobb706242000-03-17 21:46:59 +0000109H2) What kind of bash documentation is there?
110H3) What's coming in future versions?
111H4) What's on the bash `wish list'?
112H5) When will the next release appear?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000113
114----------
115Section A: The Basics
116
Jari Aaltob72432f1999-02-19 17:11:39 +0000117A1) What is it?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000118
119Bash is a Unix command interpreter (shell). It is an implementation of
120the Posix 1003.2 shell standard, and resembles the Korn and System V
121shells.
122
123Bash contains a number of enhancements over those shells, both
124for interactive use and shell programming. Features geared
125toward interactive use include command line editing, command
126history, job control, aliases, and prompt expansion. Programming
127features include additional variable expansions, shell
128arithmetic, and a number of variables and options to control
129shell behavior.
130
131Bash was originally written by Brian Fox of the Free Software
132Foundation. The current developer and maintainer is Chet Ramey
133of Case Western Reserve University.
134
Jari Aaltob72432f1999-02-19 17:11:39 +0000135A2) What's the latest version?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000136
Jari Aaltof73dda02001-11-13 17:56:06 +0000137The latest version is 2.05a, first made available on Thursday, 15
138November, 2001.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000139
Jari Aaltob72432f1999-02-19 17:11:39 +0000140A3) Where can I get it?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000141
142Bash is the GNU project's shell, and so is available from the
Jari Aaltob72432f1999-02-19 17:11:39 +0000143master GNU archive site, ftp.gnu.org, and its mirrors. The
Jari Aaltocce855b1998-04-17 19:52:44 +0000144latest version is also available for FTP from ftp.cwru.edu.
Jari Aaltof73dda02001-11-13 17:56:06 +0000145The following URLs tell how to get version 2.05a:
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000146
Jari Aaltof73dda02001-11-13 17:56:06 +0000147ftp://ftp.gnu.org/pub/gnu/bash/bash-2.05a.tar.gz
148ftp://ftp.cwru.edu/pub/bash/bash-2.05a.tar.gz
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000149
150Formatted versions of the documentation are available with the URLs:
151
Jari Aaltof73dda02001-11-13 17:56:06 +0000152ftp://ftp.gnu.org/pub/gnu/bash/bash-doc-2.05a.tar.gz
153ftp://ftp.cwru.edu/pub/bash/bash-doc-2.05a.tar.gz
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000154
Jari Aaltob72432f1999-02-19 17:11:39 +0000155A4) On what machines will bash run?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000156
157Bash has been ported to nearly every version of UNIX. All you
158should have to do to build it on a machine for which a port
159exists is to type `configure' and then `make'. The build process
160will attempt to discover the version of UNIX you have and tailor
161itself accordingly, using a script created by GNU autoconf.
162
163More information appears in the file `INSTALL' in the distribution.
164
Jari Aaltof73dda02001-11-13 17:56:06 +0000165The Bash web page (http://cnswww.cns.cwru.edu/~chet/bash/bashtop.html)
166explains how to obtain binary versions of bash for most of the major
167commercial Unix systems.
168
Jari Aaltob72432f1999-02-19 17:11:39 +0000169A5) Will bash run on operating systems other than Unix?
Jari Aaltod166f041997-06-05 14:59:13 +0000170
171Configuration specifics for Unix-like systems such as QNX and
Jari Aaltof73dda02001-11-13 17:56:06 +0000172LynxOS are included in the distribution. Bash-2.05 and later
173versions should compile and run on Minix 2.0 (patches were
174contributed), but I don't believe anyone has built bash-2.x on
175earlier Minix versions yet.
Jari Aaltod166f041997-06-05 14:59:13 +0000176
177Bash has been ported to versions of Windows implementing the Win32
178programming interface. This includes Windows 95 and Windows NT.
Jari Aaltobb706242000-03-17 21:46:59 +0000179The port was done by Cygnus Solutions as part of their CYGWIN
Jari Aalto28ef6c32001-04-06 19:14:31 +0000180project. For more information about the project, look at the URLs
Jari Aaltod166f041997-06-05 14:59:13 +0000181
Jari Aalto28ef6c32001-04-06 19:14:31 +0000182http://www.cygwin.com/
183http://sourceware.cygnus.com/cygwin
Jari Aaltod166f041997-06-05 14:59:13 +0000184
Jari Aaltob72432f1999-02-19 17:11:39 +0000185Cygnus originally ported bash-1.14.7, and that port was part of their
Jari Aaltobb706242000-03-17 21:46:59 +0000186early GNU-Win32 (the original name) releases. Cygnus has also done a
Jari Aaltof73dda02001-11-13 17:56:06 +0000187port of bash-2.05 to the CYGWIN environment, and it is available as
Jari Aalto28ef6c32001-04-06 19:14:31 +0000188part of their current release.
Jari Aaltocce855b1998-04-17 19:52:44 +0000189
Jari Aaltof73dda02001-11-13 17:56:06 +0000190Bash-2.05a should require no local Cygnus changes to build and run under
Jari Aaltobb706242000-03-17 21:46:59 +0000191CYGWIN.
Jari Aaltocce855b1998-04-17 19:52:44 +0000192
193The Cygnus port works only on Intel machines. There is a port of bash
194(I don't know which version) to the alpha/NT environment available from
195
196ftp://ftp.gnustep.org//pub/win32/bash-alpha-nt-1.01.tar.gz
197
Jari Aaltof73dda02001-11-13 17:56:06 +0000198DJ Delorie has a port of bash-2.x which runs under MS-DOS, as part
Jari Aalto28ef6c32001-04-06 19:14:31 +0000199of the DJGPP project. For more information on the project, see
Jari Aaltod166f041997-06-05 14:59:13 +0000200
201http://www.delorie.com/djgpp/
202
Jari Aalto28ef6c32001-04-06 19:14:31 +0000203I have been told that the original DJGPP port was done by Daisuke Aoyama.
204
Jari Aaltof73dda02001-11-13 17:56:06 +0000205Mark Elbrecht <snowball3@bigfoot.com> has sent me notice that bash-2.04
206is available for DJGPP V2. The files are available as:
Jari Aaltod166f041997-06-05 14:59:13 +0000207
Jari Aaltof73dda02001-11-13 17:56:06 +0000208ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh204b.zip binary
209ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh204d.zip documentation
210ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh204s.zip source
Jari Aaltod166f041997-06-05 14:59:13 +0000211
Jari Aaltof73dda02001-11-13 17:56:06 +0000212Mark has begun to work with bash-2.05, but I don't know the status.
Jari Aaltobb706242000-03-17 21:46:59 +0000213
Jari Aaltocce855b1998-04-17 19:52:44 +0000214Ports of bash-1.12 and bash-2.0 are available for OS/2 from
Jari Aaltod166f041997-06-05 14:59:13 +0000215
Jari Aaltocce855b1998-04-17 19:52:44 +0000216ftp://hobbes.nmsu.edu/pub/os2/util/shell/bash_112.zip
217ftp://hobbes.nmsu.edu/pub/os2/util/shell/bash-2.0(253).zip
Jari Aaltod166f041997-06-05 14:59:13 +0000218
Jari Aaltocce855b1998-04-17 19:52:44 +0000219I haven't looked at either, but the second appears to be a binary-only
220distribution. Beware.
Jari Aaltod166f041997-06-05 14:59:13 +0000221
Jari Aaltobb706242000-03-17 21:46:59 +0000222I have received word that Bash (I'm not sure which version, but I
223believe that it's at least bash-2.02.1) is the standard shell on
224BeOS.
225
Jari Aaltob72432f1999-02-19 17:11:39 +0000226A6) How can I build bash with gcc?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000227
228Bash configures to use gcc by default if it is available. Read the
229file INSTALL in the distribution for more information.
230
Jari Aaltob72432f1999-02-19 17:11:39 +0000231A7) How can I make bash my login shell?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000232
233Some machines let you use `chsh' to change your login shell. Other
Jari Aaltocce855b1998-04-17 19:52:44 +0000234systems use `passwd -s' or `passwd -e'. If one of these works for
235you, that's all you need. Note that many systems require the full
236pathname to a shell to appear in /etc/shells before you can make it
237your login shell. For this, you may need the assistance of your
238friendly local system administrator.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000239
240If you cannot do this, you can still use bash as your login shell, but
241you need to perform some tricks. The basic idea is to add a command
242to your login shell's startup file to replace your login shell with
243bash.
244
245For example, if your login shell is csh or tcsh, and you have installed
246bash in /usr/gnu/bin/bash, add the following line to ~/.login:
247
248 if ( -f /usr/gnu/bin/bash ) exec /usr/gnu/bin/bash --login
249
250(the `--login' tells bash that it is a login shell).
251
252It's not a good idea to put this command into ~/.cshrc, because every
253csh you run without the `-f' option, even ones started to run csh scripts,
254reads that file. If you must put the command in ~/.cshrc, use something
255like
256
257 if ( $?prompt ) exec /usr/gnu/bin/bash --login
258
259to ensure that bash is exec'd only when the csh is interactive.
260
Jari Aaltod166f041997-06-05 14:59:13 +0000261If your login shell is sh or ksh, you have to do two things.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000262
Jari Aaltod166f041997-06-05 14:59:13 +0000263First, create an empty file in your home directory named `.bash_profile'.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000264The existence of this file will prevent the exec'd bash from trying to
265read ~/.profile, and re-execing itself over and over again. ~/.bash_profile
Jari Aaltod166f041997-06-05 14:59:13 +0000266is the first file bash tries to read initialization commands from when
267it is invoked as a login shell.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000268
Jari Aaltod166f041997-06-05 14:59:13 +0000269Next, add a line similar to the above to ~/.profile:
270
271 [ -f /usr/gnu/bin/bash ] && 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
Jari Aaltobb706242000-03-17 21:46:59 +0000277I 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,
Jari Aaltof73dda02001-11-13 17:56:06 +0000284there 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.
Jari Aaltobb706242000-03-17 21:46:59 +0000287
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
Jari Aaltof73dda02001-11-13 17:56:06 +0000304If 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
Jari Aaltobb706242000-03-17 21:46:59 +0000314
Jari Aaltob72432f1999-02-19 17:11:39 +0000315A8) I just changed my login shell to bash, and now I can't FTP into my
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000316 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
Jari Aaltob72432f1999-02-19 17:11:39 +0000325A9) What's the `POSIX 1003.2 standard'?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000326
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 has been developed by IEEE
336Working Group 1003.2 (POSIX.2). It concentrates on the command
337interpreter interface and utility programs commonly executed from
338the command line or by other programs. An initial version of the
339standard has been approved and published by the IEEE, and work is
340currently underway to update it.
341
342Bash is concerned with the aspects of the shell's behavior
343defined by POSIX.2. The shell command language has of course
344been standardized, including the basic flow control and program
345execution constructs, I/O redirection and pipelining, argument
346handling, variable expansion, and quoting.
347
348The `special' builtins, which must be implemented as part of the
349shell to provide the desired functionality, are specified as
350being part of the shell; examples of these are `eval' and
351`export'. Other utilities appear in the sections of POSIX.2 not
352devoted to the shell which are commonly (and in some cases must
353be) implemented as builtin commands, such as `read' and `test'.
354POSIX.2 also specifies aspects of the shell's interactive
355behavior as part of the UPE, including job control and command
356line editing. Only vi-style line editing commands have been
357standardized; emacs editing commands were left out due to
358objections.
359
Jari Aaltof73dda02001-11-13 17:56:06 +0000360The Open Group has made an older version of its Single Unix
361Specification (version 2), which is very similar to POSIX.2,
362available on the web at
363
364http://www.opengroup.org/onlinepubs/007908799/
365
Jari Aaltob72432f1999-02-19 17:11:39 +0000366A10) What is the bash `posix mode'?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000367
368Although bash is an implementation of the POSIX.2 shell
369specification, there are areas where the bash default behavior
370differs from that spec. The bash `posix mode' changes the bash
371behavior in these areas so that it obeys the spec more closely.
372
373Posix mode is entered by starting bash with the --posix option or
374executing `set -o posix' after bash is running.
375
376The specific aspects of bash which change when posix mode is
377active are listed in the file CWRU/POSIX.NOTES in the bash
378distribution. They are also listed in a section in the Bash
379Reference Manual.
380
381Section B: The latest version
382
Jari Aaltof73dda02001-11-13 17:56:06 +0000383B1) What's new in version 2.05a?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000384
Jari Aaltof73dda02001-11-13 17:56:06 +0000385The raison d'etre for bash-2.05a is to make an intermediate release
386containing principally bug fixes (some very good work was done and
387contributed after bash-2.05 was released) available before I start to
388work on the major new features to be available in the next release
389(bash-2.06 or bash-3.0 or whatever I tag it). As such, there are
390only a few relatively minor new features.
391
392Bash-2.05a contains the following new features (see the manual page for
393complete descriptions and the CHANGES and NEWS files in the bash-2.05a
Jari Aaltobb706242000-03-17 21:46:59 +0000394distribution):
395
Jari Aaltof73dda02001-11-13 17:56:06 +0000396o The `printf' builtin has undergone major work
397
398o There is a new read-only `shopt' option: login_shell, which is set by
399 login shells and unset otherwise
400
401o New `\A' prompt string escape sequence; expanding to time in 24-hour
402 HH:MM format
403
404o New `-A group/-g' option to complete and compgen; goes group name
405 completion
406
407o New [+-]O invocation option to set and unset `shopt' options at startup
408
409o ksh-like `ERR' trap
410
411o `for' loops now allow empty word lists after the `in' reserved word
412
413o new `hard' and `soft' arguments for the `ulimit' builtin
414
415o Readline can be configured to place the user at the same point on the line
416 when retrieving commands from the history list
417
418o Readline can be configured to skip `hidden' files (filenames with a leading
419 `.' on Unix) when performing completion
420
421A short feature history dating from bash-2.0:
422
423Bash-2.05 introduced the following new features:
424
Jari Aalto28ef6c32001-04-06 19:14:31 +0000425o This version has once again reverted to using locales and strcoll(3) when
426 processing pattern matching bracket expressions, as POSIX requires.
427o Added a new `--init-file' invocation argument as a synonym for `--rcfile',
428 per the new GNU coding standards.
429o The /dev/tcp and /dev/udp redirections now accept service names as well as
430 port numbers.
431o `complete' and `compgen' now take a `-o value' option, which controls some
432 of the aspects of that compspec. Valid values are:
433
434 default - perform bash default completion if programmable
435 completion produces no matches
436 dirnames - perform directory name completion if programmable
437 completion produces no matches
438 filenames - tell readline that the compspec produces filenames,
439 so it can do things like append slashes to
440 directory names and suppress trailing spaces
441o A new loadable builtin, realpath, which canonicalizes and expands symlinks
442 in pathname arguments.
443o When `set' is called without options, it prints function defintions in a
444 way that allows them to be reused as input. This affects `declare' and
445 `declare -p' as well. This only happens when the shell is not in POSIX
446 mode, since POSIX.2 forbids this behavior.
447
Jari Aalto28ef6c32001-04-06 19:14:31 +0000448Bash-2.04 introduced the following new features:
449
Jari Aaltobb706242000-03-17 21:46:59 +0000450o Programmable word completion with the new `complete' and `compgen' builtins;
451 examples are provided in examples/complete/complete-examples
452o `history' has a new `-d' option to delete a history entry
453o `bind' has a new `-x' option to bind key sequences to shell commands
454o The prompt expansion code has new `\j' and `\l' escape sequences
Jari Aalto28ef6c32001-04-06 19:14:31 +0000455o The `no_empty_cmd_completion' shell option, if enabled, inhibits
Jari Aaltobb706242000-03-17 21:46:59 +0000456 command completion when TAB is typed on an empty line
457o `help' has a new `-s' option to print a usage synopsis
458o New arithmetic operators: var++, var--, ++var, --var, expr1,expr2 (comma)
459o New ksh93-style arithmetic for command:
460 for ((expr1 ; expr2; expr3 )); do list; done
461o `read' has new options: `-t', `-n', `-d', `-s'
462o The redirection code handles several filenames specially: /dev/fd/N,
463 /dev/stdin, /dev/stdout, /dev/stderr
464o The redirection code now recognizes /dev/tcp/HOST/PORT and
465 /dev/udp/HOST/PORT and tries to open a TCP or UDP socket, respectively,
466 to the specified port on the specified host
467o The ${!prefix*} expansion has been implemented
468o A new FUNCNAME variable, which expands to the name of a currently-executing
469 function
470o The GROUPS variable is no longer readonly
471o A new shopt `xpg_echo' variable, to control the behavior of echo with
472 respect to backslash-escape sequences at runtime
473o The NON_INTERACTIVE_LOGIN_SHELLS #define has returned
474
Jari Aalto28ef6c32001-04-06 19:14:31 +0000475The version of Readline released with Bash-2.04, Readline-4.1, had several
Jari Aaltobb706242000-03-17 21:46:59 +0000476new features as well:
477
478o Parentheses matching is always compiled into readline, and controllable
479 with the new `blink-matching-paren' variable
480o The history-search-forward and history-search-backward functions now leave
481 point at the end of the line when the search string is empty, like
482 reverse-search-history, and forward-search-history
483o A new function for applications: rl_on_new_line_with_prompt()
484o New variables for applications: rl_already_prompted, and rl_gnu_readline_p
485
486
Jari Aaltobb706242000-03-17 21:46:59 +0000487Bash-2.03 had very few new features, in keeping with the convention
Jari Aaltob72432f1999-02-19 17:11:39 +0000488that odd-numbered releases provide mainly bug fixes. A number of new
489features were added to Readline, mostly at the request of the Cygnus
490folks.
491
Jari Aaltobb706242000-03-17 21:46:59 +0000492A new shopt option, `restricted_shell', so that startup files can test
Jari Aaltob72432f1999-02-19 17:11:39 +0000493 whether or not the shell was started in restricted mode
Jari Aaltobb706242000-03-17 21:46:59 +0000494Filename generation is now performed on the words between ( and ) in
Jari Aaltob72432f1999-02-19 17:11:39 +0000495 compound array assignments (this is really a bug fix)
496OLDPWD is now auto-exported, as POSIX.2 requires
497ENV and BASH_ENV are read-only variables in a restricted shell
498Bash may now be linked against an already-installed Readline library,
499 as long as the Readline library is version 4 or newer
500All shells begun with the `--login' option will source the login shell
501 startup files, even if the shell is not interactive
502
Jari Aaltobb706242000-03-17 21:46:59 +0000503There were lots of changes to the version of the Readline library released
Jari Aaltob72432f1999-02-19 17:11:39 +0000504along with Bash-2.03. For a complete list of the changes, read the file
505CHANGES in the Bash-2.03 distribution.
506
507Bash-2.02 contained the following new features:
Jari Aaltocce855b1998-04-17 19:52:44 +0000508
509a new version of malloc (based on the old GNU malloc code in previous
510 bash versions) that is more page-oriented, more conservative
511 with memory usage, does not `orphan' large blocks when they
512 are freed, is usable on 64-bit machines, and has allocation
513 checking turned on unconditionally
514POSIX.2-style globbing character classes ([:alpha:], [:alnum:], etc.)
515POSIX.2-style globbing equivalence classes
516POSIX.2-style globbing collating symbols
517the ksh [[...]] extended conditional command
518the ksh egrep-style extended pattern matching operators
519a new `printf' builtin
520the ksh-like $(<filename) command substitution, which is equivalent to
521 $(cat filename)
522new tilde prefixes that expand to directories from the directory stack
523new `**' arithmetic operator to do exponentiation
524case-insensitive globbing (filename expansion)
525menu completion a la tcsh
526`magic-space' history expansion function like tcsh
527the readline inputrc `language' has a new file inclusion directive ($include)
528
529Bash-2.01 contained only a few new features:
Jari Aaltod166f041997-06-05 14:59:13 +0000530
531new `GROUPS' builtin array variable containing the user's group list
532new bindable readline commands: history-and-alias-expand-line and
533 alias-expand-line
534
Jari Aaltocce855b1998-04-17 19:52:44 +0000535Bash-2.0 contained extensive changes and new features from bash-1.14.7.
Jari Aaltod166f041997-06-05 14:59:13 +0000536Here's a short list:
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000537
538new `time' reserved word to time pipelines, shell builtins, and
539 shell functions
540one-dimensional arrays with a new compound assignment statement,
541 appropriate expansion constructs and modifications to some
542 of the builtins (read, declare, etc.) to use them
543new quoting syntaxes for ANSI-C string expansion and locale-specific
544 string translation
545new expansions to do substring extraction, pattern replacement, and
546 indirect variable expansion
547new builtins: `disown' and `shopt'
548new variables: HISTIGNORE, SHELLOPTS, PIPESTATUS, DIRSTACK, GLOBIGNORE,
549 MACHTYPE, BASH_VERSINFO
550special handling of many unused or redundant variables removed
551 (e.g., $notify, $glob_dot_filenames, $no_exit_on_failed_exec)
552dynamic loading of new builtin commands; many loadable examples provided
553new prompt expansions: \a, \e, \n, \H, \T, \@, \v, \V
554history and aliases available in shell scripts
555new readline variables: enable-keypad, mark-directories, input-meta,
556 visible-stats, disable-completion, comment-begin
557new readline commands to manipulate the mark and operate on the region
558new readline emacs mode commands and bindings for ksh-88 compatibility
559updated and extended builtins
560new DEBUG trap
561expanded (and now documented) restricted shell mode
562
563implementation stuff:
564autoconf-based configuration
565nearly all of the bugs reported since version 1.14 have been fixed
566most builtins converted to use builtin `getopt' for consistency
567most builtins use -p option to display output in a reusable form
568 (for consistency)
569grammar tighter and smaller (66 reduce-reduce conflicts gone)
570lots of code now smaller and faster
571test suite greatly expanded
572
Jari Aaltof73dda02001-11-13 17:56:06 +0000573B2) Are there any user-visible incompatibilities between bash-2.05a and
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000574 bash-1.14.7?
575
Jari Aaltof73dda02001-11-13 17:56:06 +0000576There are a few incompatibilities between version 1.14.7 and version 2.05a.
577They are detailed in the file COMPAT in the bash-2.05a distribution. That
578file is not meant to be all-encompassing; send mail to bash-maintainers@gnu.org
579if you find something that's not mentioned there.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000580
581Section C: Differences from other Unix shells
582
Jari Aaltob72432f1999-02-19 17:11:39 +0000583C1) How does bash differ from sh, the Bourne shell?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000584
585This is a non-comprehensive list of features that differentiate bash
586from the SVR4.2 shell. The bash manual page explains these more
587completely.
588
589Things bash has that sh does not:
590 long invocation options
Jari Aaltof73dda02001-11-13 17:56:06 +0000591 [+-]O invocation option
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000592 `!' reserved word to invert pipeline return value
593 `time' reserved word to time pipelines and shell builtins
594 the `function' reserved word
Jari Aaltobb706242000-03-17 21:46:59 +0000595 the `select' compound command and reserved word
596 arithmetic for command: for ((expr1 ; expr2; expr3 )); do list; done
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000597 new $'...' and $"..." quoting
598 the $(...) form of command substitution
Jari Aaltobc4cd231998-07-23 14:37:54 +0000599 the $(<filename) form of command substitution, equivalent to
600 $(cat filename)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000601 the ${#param} parameter value length operator
602 the ${!param} indirect parameter expansion operator
Jari Aaltobb706242000-03-17 21:46:59 +0000603 the ${!param*} prefix expansion operator
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000604 the ${param:length[:offset]} parameter substring operator
605 the ${param/pat[/string]} parameter pattern substitution operator
606 expansions to perform substring removal (${p%[%]w}, ${p#[#]w})
607 expansion of positional parameters beyond $9 with ${num}
608 variables: BASH, BASH_VERSION, BASH_VERSINFO, UID, EUID, REPLY,
609 TIMEFORMAT, PPID, PWD, OLDPWD, SHLVL, RANDOM, SECONDS,
610 LINENO, HISTCMD, HOSTTYPE, OSTYPE, MACHTYPE, HOSTNAME,
611 ENV, PS3, PS4, DIRSTACK, PIPESTATUS, HISTSIZE, HISTFILE,
Jari Aaltobc4cd231998-07-23 14:37:54 +0000612 HISTFILESIZE, HISTCONTROL, HISTIGNORE, GLOBIGNORE, GROUPS,
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000613 PROMPT_COMMAND, FCEDIT, FIGNORE, IGNOREEOF, INPUTRC,
Jari Aaltobb706242000-03-17 21:46:59 +0000614 SHELLOPTS, OPTERR, HOSTFILE, TMOUT, FUNCNAME, histchars,
615 auto_resume
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000616 DEBUG trap
Jari Aaltof73dda02001-11-13 17:56:06 +0000617 ERR trap
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000618 variable arrays with new compound assignment syntax
619 redirections: <>, &>, >|
620 prompt string special char translation and variable expansion
Jari Aaltobb706242000-03-17 21:46:59 +0000621 auto-export of variables in initial environment
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000622 command search finds functions before builtins
623 bash return builtin will exit a file sourced with `.'
Jari Aaltof73dda02001-11-13 17:56:06 +0000624 builtins: cd -/-L/-P, exec -l/-c/-a, echo -e/-E, hash -p/-t.
Jari Aaltobb706242000-03-17 21:46:59 +0000625 export -n/-f/-p/name=value, pwd -L/-P,
626 read -e/-p/-a/-t/-n/-d/-s,
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000627 readonly -a/-f/name=value, trap -l, set +o,
628 set -b/-m/-o option/-h/-p/-B/-C/-H/-P,
629 unset -f/-v, ulimit -m/-p/-u,
630 type -a/-p/-t, suspend -f, kill -n,
631 test -o optname/s1 == s2/s1 < s2/s1 > s2/-nt/-ot/-ef/-O/-G/-S
632 bash reads ~/.bashrc for interactive shells, $ENV for non-interactive
633 bash restricted shell mode is more extensive
634 bash allows functions and variables with the same name
635 brace expansion
636 tilde expansion
637 arithmetic expansion with $((...)) and `let' builtin
Jari Aaltobc4cd231998-07-23 14:37:54 +0000638 the `[[...]]' extended conditional command
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000639 process substitution
640 aliases and alias/unalias builtins
641 local variables in functions and `local' builtin
Jari Aaltobb706242000-03-17 21:46:59 +0000642 readline and command-line editing with programmable completion
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000643 command history and history/fc builtins
644 csh-like history expansion
Jari Aaltobb706242000-03-17 21:46:59 +0000645 other new bash builtins: bind, command, compgen, complete, builtin,
646 declare/typeset, dirs, enable, fc, help,
647 history, logout, popd, pushd, disown, shopt,
648 printf
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000649 exported functions
650 filename generation when using output redirection (command >a*)
Jari Aaltobc4cd231998-07-23 14:37:54 +0000651 POSIX.2-style globbing character classes
652 POSIX.2-style globbing equivalence classes
653 POSIX.2-style globbing collating symbols
654 egrep-like extended pattern matching operators
655 case-insensitive pattern matching and globbing
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000656 variable assignments preceding commands affect only that command,
657 even for builtins and functions
658 posix mode
Jari Aaltobb706242000-03-17 21:46:59 +0000659 redirection to /dev/fd/N, /dev/stdin, /dev/stdout, /dev/stderr,
660 /dev/tcp/host/port, /dev/udp/host/port
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000661
662Things sh has that bash does not:
663 uses variable SHACCT to do shell accounting
664 includes `stop' builtin (bash can use alias stop='kill -s STOP')
665 `newgrp' builtin
666 turns on job control if called as `jsh'
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000667 $TIMEOUT (like bash $TMOUT)
668 `^' is a synonym for `|'
669 new SVR4.2 sh builtins: mldmode, priv
670
671Implementation differences:
672 redirection to/from compound commands causes sh to create a subshell
673 bash does not allow unbalanced quotes; sh silently inserts them at EOF
674 bash does not mess with signal 11
675 sh sets (euid, egid) to (uid, gid) if -p not supplied and uid < 100
676 bash splits only the results of expansions on IFS, using POSIX.2
677 field splitting rules; sh splits all words on IFS
678 sh does not allow MAILCHECK to be unset (?)
679 sh does not allow traps on SIGALRM or SIGCHLD
680 bash allows multiple option arguments when invoked (e.g. -x -v);
681 sh allows only a single option argument (`sh -x -v' attempts
Jari Aaltod166f041997-06-05 14:59:13 +0000682 to open a file named `-v', and, on SunOS 4.1.4, dumps core.
Jari Aaltob72432f1999-02-19 17:11:39 +0000683 On Solaris 2.4 and earlier versions, sh goes into an infinite
684 loop.)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000685 sh exits a script if any builtin fails; bash exits only if one of
686 the POSIX.2 `special' builtins fails
687
Jari Aaltob72432f1999-02-19 17:11:39 +0000688C2) How does bash differ from the Korn shell, version ksh88?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000689
690Things bash has or uses that ksh88 does not:
691 long invocation options
Jari Aaltof73dda02001-11-13 17:56:06 +0000692 [-+]O invocation option
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000693 `!' reserved word
Jari Aaltobb706242000-03-17 21:46:59 +0000694 arithmetic for command: for ((expr1 ; expr2; expr3 )); do list; done
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000695 posix mode and posix conformance
696 command hashing
697 tilde expansion for assignment statements that look like $PATH
698 process substitution with named pipes if /dev/fd is not available
699 the ${!param} indirect parameter expansion operator
Jari Aaltobb706242000-03-17 21:46:59 +0000700 the ${!param*} prefix expansion operator
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000701 the ${param:length[:offset]} parameter substring operator
702 the ${param/pat[/string]} parameter pattern substitution operator
703 variables: BASH, BASH_VERSION, BASH_VERSINFO, UID, EUID, SHLVL,
704 TIMEFORMAT, HISTCMD, HOSTTYPE, OSTYPE, MACHTYPE,
705 HISTFILESIZE, HISTIGNORE, HISTCONTROL, PROMPT_COMMAND,
706 IGNOREEOF, FIGNORE, INPUTRC, HOSTFILE, DIRSTACK,
707 PIPESTATUS, HOSTNAME, OPTERR, SHELLOPTS, GLOBIGNORE,
Jari Aaltobb706242000-03-17 21:46:59 +0000708 GROUPS, FUNCNAME, histchars, auto_resume
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000709 prompt expansion with backslash escapes and command substitution
710 redirection: &> (stdout and stderr)
Jari Aaltobb706242000-03-17 21:46:59 +0000711 more extensive and extensible editing and programmable completion
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000712 builtins: bind, builtin, command, declare, dirs, echo -e/-E, enable,
713 exec -l/-c/-a, fc -s, export -n/-f/-p, hash, help, history,
714 jobs -x/-r/-s, kill -s/-n/-l, local, logout, popd, pushd,
Jari Aaltobb706242000-03-17 21:46:59 +0000715 read -e/-p/-a/-t/-n/-d/-s, readonly -a/-n/-f/-p,
716 set -o braceexpand/-o histexpand/-o interactive-comments/
717 -o notify/-o physical/-o posix/-o hashall/-o onecmd/
718 -h/-B/-C/-b/-H/-P, set +o, suspend, trap -l, type,
719 typeset -a/-F/-p, ulimit -u, umask -S, alias -p, shopt,
720 disown, printf, complete, compgen
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000721 `!' csh-style history expansion
Jari Aaltobc4cd231998-07-23 14:37:54 +0000722 POSIX.2-style globbing character classes
723 POSIX.2-style globbing equivalence classes
724 POSIX.2-style globbing collating symbols
725 egrep-like extended pattern matching operators
726 case-insensitive pattern matching and globbing
727 `**' arithmetic operator to do exponentiation
Jari Aaltobb706242000-03-17 21:46:59 +0000728 redirection to /dev/fd/N, /dev/stdin, /dev/stdout, /dev/stderr
Jari Aaltof73dda02001-11-13 17:56:06 +0000729 arrays of unlimited size
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000730
731Things ksh88 has or uses that bash does not:
Jari Aaltof73dda02001-11-13 17:56:06 +0000732 tracked aliases (alias -t)
Jari Aaltobb706242000-03-17 21:46:59 +0000733 variables: ERRNO, FPATH, EDITOR, VISUAL
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000734 co-processes (|&, >&p, <&p)
735 weirdly-scoped functions
736 typeset +f to list all function names without definitions
737 text of command history kept in a file, not memory
738 builtins: alias -x, cd old new, fc -e -, newgrp, print,
739 read -p/-s/-u/var?prompt, set -A/-o gmacs/
740 -o bgnice/-o markdirs/-o nolog/-o trackall/-o viraw/-s,
Jari Aaltof73dda02001-11-13 17:56:06 +0000741 typeset -H/-L/-R/-Z/-A/-ft/-fu/-fx/-l/-u/-t, whence
742 using environment to pass attributes of exported variables
743 arithmetic evaluation done on arguments to some builtins
744 reads .profile from $PWD when invoked as login shell
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000745
746Implementation differences:
747 ksh runs last command of a pipeline in parent shell context
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000748 bash has brace expansion by default (ksh88 compile-time option)
749 bash has fixed startup file for all interactive shells; ksh reads $ENV
750 bash has exported functions
751 bash command search finds functions before builtins
Jari Aaltof73dda02001-11-13 17:56:06 +0000752 bash waits for all commands in pipeline to exit before returning status
753 emacs-mode editing has some slightly different key bindings
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000754
Jari Aaltob72432f1999-02-19 17:11:39 +0000755C3) Which new features in ksh-93 are not in bash, and which are?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000756
Jari Aaltof73dda02001-11-13 17:56:06 +0000757New things in ksh-93 not in bash-2.05a:
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000758 associative arrays
Jari Aaltof73dda02001-11-13 17:56:06 +0000759 floating point arithmetic and variables
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000760 math library functions
761 ${!name[sub]} name of subscript for associative array
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000762 `.' is allowed in variable names to create a hierarchical namespace
763 more extensive compound assignment syntax
764 discipline functions
765 `sleep' and `getconf' builtins (bash has loadable versions)
766 typeset -n and `nameref' variables
767 KEYBD trap
Jari Aaltobb706242000-03-17 21:46:59 +0000768 variables: .sh.edchar, .sh.edmode, .sh.edcol, .sh.edtext, .sh.version,
Jari Aaltof73dda02001-11-13 17:56:06 +0000769 .sh.name, .sh.subscript, .sh.value, .sh.match, HISTEDIT
770 backreferences in pattern matching (\N)
771 `&' operator in pattern lists for matching
Jari Aaltobb706242000-03-17 21:46:59 +0000772 print -f (bash uses printf)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000773 `fc' has been renamed to `hist'
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000774 `.' can execute shell functions
Jari Aaltof73dda02001-11-13 17:56:06 +0000775 exit statuses between 0 and 255
776 set -o pipefail
777 `+=' variable assignment operator
778 TMOUT is default timeout for `read' and `select'
779 <&N- and >&N- redirections (combination dup and close)
780 FPATH and PATH mixing
781 getopts -a
782 -I invocation option
783 DEBUG trap now executed before each simple command, instead of after
784 printf %H, %P, %T, %Z modifiers, output base for %d
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000785
Jari Aaltof73dda02001-11-13 17:56:06 +0000786New things in ksh-93 present in bash-2.05a:
Jari Aaltobb706242000-03-17 21:46:59 +0000787 for (( expr1; expr2; expr3 )) ; do list; done - arithmetic for command
788 ?:, ++, --, `expr1 , expr2' arithmetic operators
789 expansions: ${!param}, ${param:offset[:len]}, ${param/pat[/str]},
790 ${!param*}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000791 compound array assignment
792 the `!' reserved word
793 loadable builtins -- but ksh uses `builtin' while bash uses `enable'
794 `command', `builtin', `disown' builtins
795 new $'...' and $"..." quoting
796 FIGNORE (but bash uses GLOBIGNORE), HISTCMD
797 set -o notify/-C
798 changes to kill builtin
799 read -A (bash uses read -a)
Jari Aaltobb706242000-03-17 21:46:59 +0000800 read -t/-d
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000801 trap -p
802 exec -c/-a
803 `.' restores the positional parameters when it completes
804 POSIX.2 `test'
805 umask -S
806 unalias -a
807 command and arithmetic substitution performed on PS1, PS4, and ENV
808 command name completion
Jari Aaltod166f041997-06-05 14:59:13 +0000809 ENV processed only for interactive shells
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000810
811Section D: Why does bash do some things differently than other Unix shells?
812
Jari Aaltob72432f1999-02-19 17:11:39 +0000813D1) Why does bash run a different version of `command' than
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000814 `which command' says it will?
815
Jari Aaltob72432f1999-02-19 17:11:39 +0000816On many systems, `which' is actually a csh script that assumes
817you're running csh. In tcsh, `which' and its cousin `where'
818are builtins. On other Unix systems, `which' is a perl script
819that uses the PATH environment variable.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000820
Jari Aaltob72432f1999-02-19 17:11:39 +0000821The csh script version reads the csh startup files from your
822home directory and uses those to determine which `command' will
823be invoked. Since bash doesn't use any of those startup files,
824there's a good chance that your bash environment differs from
825your csh environment. The bash `type' builtin does everything
826`which' does, and will report correct results for the running
827shell. If you're really wedded to the name `which', try adding
828the following function definition to your .bashrc:
829
830 which()
831 {
Jari Aaltobb706242000-03-17 21:46:59 +0000832 builtin type "$@"
Jari Aaltob72432f1999-02-19 17:11:39 +0000833 }
834
835If you're moving from tcsh and would like to bring `where' along
836as well, use this function:
837
838 where()
839 {
840 builtin type -a "$@"
841 }
842
843D2) Why doesn't bash treat brace expansions exactly like csh?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000844
845The only difference between bash and csh brace expansion is that
846bash requires a brace expression to contain at least one unquoted
847comma if it is to be expanded. Any brace-surrounded word not
848containing an unquoted comma is left unchanged by the brace
849expansion code. This affords the greatest degree of sh
850compatibility.
851
852Bash, ksh, zsh, and pd-ksh all implement brace expansion this way.
853
Jari Aaltob72432f1999-02-19 17:11:39 +0000854D3) Why doesn't bash have csh variable modifiers?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000855
856Posix has specified a more powerful, albeit somewhat more cryptic,
857mechanism cribbed from ksh, and bash implements it.
858
859${parameter%word}
860 Remove smallest suffix pattern. The WORD is expanded to produce
861 a pattern. It then expands to the value of PARAMETER, with the
862 smallest portion of the suffix matched by the pattern deleted.
863
864 x=file.c
865 echo ${x%.c}.o
866 -->file.o
867
868${parameter%%word}
869
870 Remove largest suffix pattern. The WORD is expanded to produce
871 a pattern. It then expands to the value of PARAMETER, with the
872 largest portion of the suffix matched by the pattern deleted.
873
874 x=posix/src/std
875 echo ${x%%/*}
876 -->posix
877
878${parameter#word}
879 Remove smallest prefix pattern. The WORD is expanded to produce
880 a pattern. It then expands to the value of PARAMETER, with the
881 smallest portion of the prefix matched by the pattern deleted.
882
883 x=$HOME/src/cmd
884 echo ${x#$HOME}
885 -->/src/cmd
886
887${parameter##word}
888 Remove largest prefix pattern. The WORD is expanded to produce
889 a pattern. It then expands to the value of PARAMETER, with the
890 largest portion of the prefix matched by the pattern deleted.
891
892 x=/one/two/three
893 echo ${x##*/}
894 -->three
895
896
897Given
898 a=/a/b/c/d
899 b=b.xxx
900
901 csh bash result
902 --- ---- ------
903 $a:h ${a%/*} /a/b/c
904 $a:t ${a##*/} d
905 $b:r ${b%.*} b
906 $b:e ${b##*.} xxx
907
908
Jari Aaltob72432f1999-02-19 17:11:39 +0000909D4) How can I make my csh aliases work when I convert to bash?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000910
911Bash uses a different syntax to support aliases than csh does.
912The details can be found in the documentation. We have provided
913a shell script which does most of the work of conversion for you;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000914this script can be found in ./examples/misc/aliasconv.sh. Here is
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000915how you use it:
916
917Start csh in the normal way for you. (e.g., `csh')
918
Jari Aalto28ef6c32001-04-06 19:14:31 +0000919Pipe the output of `alias' through `aliasconv.sh', saving the
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000920results into `bash_aliases':
921
Jari Aalto28ef6c32001-04-06 19:14:31 +0000922 alias | bash aliasconv.sh >bash_aliases
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000923
924Edit `bash_aliases', carefully reading through any created
925functions. You will need to change the names of some csh specific
926variables to the bash equivalents. The script converts $cwd to
927$PWD, $term to $TERM, $home to $HOME, $user to $USER, and $prompt
928to $PS1. You may also have to add quotes to avoid unwanted
929expansion.
930
931For example, the csh alias:
932
933 alias cd 'cd \!*; echo $cwd'
934
935is converted to the bash function:
936
937 cd () { command cd "$@"; echo $PWD ; }
938
939The only thing that needs to be done is to quote $PWD:
940
941 cd () { command cd "$@"; echo "$PWD" ; }
942
943Merge the edited file into your ~/.bashrc.
944
945There is an additional, more ambitious, script in
946examples/misc/cshtobash that attempts to convert your entire csh
947environment to its bash equivalent. This script can be run as
948simply `cshtobash' to convert your normal interactive
949environment, or as `cshtobash ~/.login' to convert your login
950environment.
951
Jari Aaltob72432f1999-02-19 17:11:39 +0000952D5) How can I pipe standard output and standard error from one command to
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000953 another, like csh does with `|&'?
954
955Use
956 command 2>&1 | command2
957
958The key is to remember that piping is performed before redirection, so
959file descriptor 1 points to the pipe when it is duplicated onto file
960descriptor 2.
961
Jari Aaltob72432f1999-02-19 17:11:39 +0000962D6) Now that I've converted from ksh to bash, are there equivalents to
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000963 ksh features like autoloaded functions and the `whence' command?
964
Jari Aaltobb706242000-03-17 21:46:59 +0000965There are features in ksh-88 and ksh-93 that do not have direct bash
966equivalents. Most, however, can be emulated with very little trouble.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000967
968ksh-88 feature Bash equivalent
969-------------- ---------------
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000970compiled-in aliases set up aliases in .bashrc; some ksh aliases are
971 bash builtins (hash, history, type)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000972coprocesses named pipe pairs (one for read, one for write)
973typeset +f declare -F
974cd, print, whence function substitutes in examples/functions/kshenv
975autoloaded functions examples/functions/autoload is the same as typeset -fu
976read var?prompt read -p prompt var
977
Jari Aaltobb706242000-03-17 21:46:59 +0000978ksh-93 feature Bash equivalent
979-------------- ---------------
980sleep, getconf Bash has loadable versions in examples/loadables
981${.sh.version} $BASH_VERSION
982print -f printf
983hist alias fc=hist
984$HISTEDIT $FCEDIT
985
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000986Section E: How can I get bash to do certain things, and why does bash do
987 things the way it does?
988
Jari Aaltob72432f1999-02-19 17:11:39 +0000989E1) Why is the bash builtin `test' slightly different from /bin/test?
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000990
991The specific example used here is [ ! x -o x ], which is false.
992
993Bash's builtin `test' implements the Posix.2 spec, which can be
994summarized as follows (the wording is due to David Korn):
995
996Here is the set of rules for processing test arguments.
997
998 0 Args: False
999 1 Arg: True iff argument is not null.
1000 2 Args: If first arg is !, True iff second argument is null.
1001 If first argument is unary, then true if unary test is true
1002 Otherwise error.
1003 3 Args: If second argument is a binary operator, do binary test of $1 $3
1004 If first argument is !, negate two argument test of $2 $3
1005 If first argument is `(' and third argument is `)', do the
1006 one-argument test of the second argument.
1007 Otherwise error.
1008 4 Args: If first argument is !, negate three argument test of $2 $3 $4.
1009 Otherwise unspecified
1010 5 or more Args: unspecified. (Historical shells would use their
1011 current algorithm).
1012
1013The operators -a and -o are considered binary operators for the purpose
1014of the 3 Arg case.
1015
1016As you can see, the test becomes (not (x or x)), which is false.
1017
Jari Aaltob72432f1999-02-19 17:11:39 +00001018E2) Why does bash sometimes say `Broken pipe'?
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001019
1020If a sequence of commands appears in a pipeline, and one of the
1021reading commands finishes before the writer has finished, the
1022writer receives a SIGPIPE signal. Many other shells special-case
1023SIGPIPE as an exit status in the pipeline and do not report it.
1024For example, in:
1025
1026 ps -aux | head
1027
1028`head' can finish before `ps' writes all of its output, and ps
1029will try to write on a pipe without a reader. In that case, bash
1030will print `Broken pipe' to stderr when ps is killed by a
1031SIGPIPE.
1032
Jari Aaltob72432f1999-02-19 17:11:39 +00001033You can build a version of bash that will not report SIGPIPE errors
1034by uncommenting the definition of DONT_REPORT_SIGPIPE in the file
1035config-top.h.
1036
Jari Aaltobb706242000-03-17 21:46:59 +00001037E3) When I have terminal escape sequences in my prompt, why does bash
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001038 wrap lines at the wrong column?
1039
1040Readline, the line editing library that bash uses, does not know
1041that the terminal escape sequences do not take up space on the
1042screen. The redisplay code assumes, unless told otherwise, that
1043each character in the prompt is a `printable' character that
1044takes up one character position on the screen.
1045
1046You can use the bash prompt expansion facility (see the PROMPTING
1047section in the manual page) to tell readline that sequences of
1048characters in the prompt strings take up no screen space.
1049
1050Use the \[ escape to begin a sequence of non-printing characters,
1051and the \] escape to signal the end of such a sequence.
1052
Jari Aaltobb706242000-03-17 21:46:59 +00001053E4) If I pipe the output of a command into `read variable', why doesn't
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001054 the output show up in $variable when the read command finishes?
1055
1056This has to do with the parent-child relationship between Unix
Jari Aalto28ef6c32001-04-06 19:14:31 +00001057processes. It affects all commands run in pipelines, not just
1058simple calls to `read'. For example, piping a command's output
1059into a `while' loop that repeatedly calls `read' will result in
1060the same behavior.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001061
1062Each element of a pipeline runs in a separate process, a child of
1063the shell running the pipeline. A subprocess cannot affect its
1064parent's environment. When the `read' command sets the variable
1065to the input, that variable is set only in the subshell, not the
1066parent shell. When the subshell exits, the value of the variable
1067is lost.
1068
1069Many pipelines that end with `read variable' can be converted
1070into command substitutions, which will capture the output of
1071a specified command. The output can then be assigned to a
1072variable:
1073
1074 grep ^gnu /usr/lib/news/active | wc -l | read ngroup
1075
1076can be converted into
1077
1078 ngroup=$(grep ^gnu /usr/lib/news/active | wc -l)
1079
1080This does not, unfortunately, work to split the text among
1081multiple variables, as read does when given multiple variable
1082arguments. If you need to do this, you can either use the
1083command substitution above to read the output into a variable
1084and chop up the variable using the bash pattern removal
1085expansion operators or use some variant of the following
1086approach.
1087
1088Say /usr/local/bin/ipaddr is the following shell script:
1089
1090#! /bin/sh
1091host `hostname` | awk '/address/ {print $NF}'
1092
1093Instead of using
1094
1095 /usr/local/bin/ipaddr | read A B C D
1096
1097to break the local machine's IP address into separate octets, use
1098
1099 OIFS="$IFS"
1100 IFS=.
1101 set -- $(/usr/local/bin/ipaddr)
1102 IFS="$OIFS"
1103 A="$1" B="$2" C="$3" D="$4"
1104
1105Beware, however, that this will change the shell's positional
1106parameters. If you need them, you should save them before doing
1107this.
1108
1109This is the general approach -- in most cases you will not need to
1110set $IFS to a different value.
1111
Jari Aaltof73dda02001-11-13 17:56:06 +00001112Some other user-supplied alternatives include:
1113
1114read A B C D << HERE
1115 $(IFS=.; echo $(/usr/local/bin/ipaddr))
1116HERE
1117
1118and, where process substitution is available,
1119
1120read A B C D < <(IFS=.; echo $(/usr/local/bin/ipaddr))
1121
Jari Aaltobb706242000-03-17 21:46:59 +00001122E5) I have a bunch of shell scripts that use backslash-escaped characters
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001123 in arguments to `echo'. Bash doesn't interpret these characters. Why
1124 not, and how can I make it understand them?
1125
1126This is the behavior of echo on most Unix System V machines.
1127
Jari Aaltobb706242000-03-17 21:46:59 +00001128The bash builtin `echo' is modeled after the 9th Edition
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001129Research Unix version of `echo'. It does not interpret
1130backslash-escaped characters in its argument strings by default;
1131it requires the use of the -e option to enable the
1132interpretation. The System V echo provides no way to disable the
1133special characters; the bash echo has a -E option to disable
1134them.
1135
1136There is a configuration option that will make bash behave like
1137the System V echo and interpret things like `\t' by default. Run
Jari Aaltof73dda02001-11-13 17:56:06 +00001138configure with the --enable-xpg-echo-default option to turn this
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001139on. Be aware that this will cause some of the tests run when you
1140type `make tests' to fail.
1141
Jari Aaltobb706242000-03-17 21:46:59 +00001142There is a shell option, `xpg_echo', settable with `shopt' that will
1143change the behavior of echo at runtime. Enabling this option turns
1144on expansion of backslash-escape sequences.
1145
1146E6) Why doesn't a while or for loop get suspended when I type ^Z?
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001147
1148This is a consequence of how job control works on Unix. The only
1149thing that can be suspended is the process group. This is a single
1150command or pipeline of commands that the shell forks and executes.
1151
1152When you run a while or for loop, the only thing that the shell forks
1153and executes are any commands in the while loop test and commands in
1154the loop bodies. These, therefore, are the only things that can be
1155suspended when you type ^Z.
1156
1157If you want to be able to stop the entire loop, you need to put it
1158within parentheses, which will force the loop into a subshell that
1159may be stopped (and subsequently restarted) as a single unit.
1160
Jari Aalto28ef6c32001-04-06 19:14:31 +00001161E7) What about empty for loops in Makefiles?
1162
1163It's fairly common to see constructs like this in automatically-generated
1164Makefiles:
1165
1166SUBDIRS = @SUBDIRS@
1167
1168 ...
1169
1170subdirs-clean:
1171 for d in ${SUBDIRS}; do \
1172 ( cd $$d && ${MAKE} ${MFLAGS} clean ) \
1173 done
1174
1175When SUBDIRS is empty, this results in a command like this being passed to
1176bash:
1177
1178 for d in ; do
1179 ( cd $d && ${MAKE} ${MFLAGS} clean )
1180 done
1181
Jari Aaltof73dda02001-11-13 17:56:06 +00001182In versions of bash before bash-2.05a, this was a syntax error. If the
1183reserved word `in' was present, a word must follow it before the semicolon
1184or newline. The language in the manual page referring to the list of words
1185being empty referred to the list after it is expanded. These versions of
1186bash required that there be at least one word following the `in' when the
1187construct was parsed.
Jari Aalto28ef6c32001-04-06 19:14:31 +00001188
1189The idiomatic Makefile solution is something like:
1190
1191SUBDIRS = @SUBDIRS@
1192
1193subdirs-clean:
1194 subdirs=$SUBDIRS ; for d in $$subdirs; do \
1195 ( cd $$d && ${MAKE} ${MFLAGS} clean ) \
1196 done
1197
Jari Aaltof73dda02001-11-13 17:56:06 +00001198The latest drafts of the updated POSIX standard have changed this: the
1199word list is no longer required. Bash versions 2.05a and later accept
1200the new syntax.
Jari Aalto28ef6c32001-04-06 19:14:31 +00001201
1202E8) Why does the arithmetic evaluation code complain about `08'?
1203
1204The bash arithmetic evaluation code (used for `let', $(()), (()), and in
1205other places), interprets a leading `0' in numeric constants as denoting
1206an octal number, and a leading `0x' as denoting hexadecimal. This is
1207in accordance with the POSIX.2 spec, section 2.9.2.1, which states that
1208arithmetic constants should be handled as signed long integers as defined
1209by the ANSI/ISO C standard.
1210
1211The POSIX.2 interpretation committee has confirmed this:
1212
1213http://www.pasc.org/interps/unofficial/db/p1003.2/pasc-1003.2-173.html
1214
1215E9) Why does the pattern matching expression [A-Z]* match files beginning
1216 with every letter except `z'?
1217
1218Bash-2.05 and later versions have reverted to the bash-2.03 behavior of
1219honoring the current locale setting when processing ranges within pattern
1220matching bracket expressions ([A-Z]). This is what POSIX.2 and SUSv2/XPG5
1221specify.
1222
Jari Aaltof73dda02001-11-13 17:56:06 +00001223The behavior of the matcher in bash-2.05 and later versions depends on the
1224current LC_COLLATE setting. Setting this variable to `C' or `POSIX' will
1225result in the traditional behavior ([A-Z] matches all uppercase ASCII
1226characters). Many other locales, including the en_US locale (the default
1227on many US versions of Linux) collate the upper and lower case letters like
1228this:
Jari Aalto28ef6c32001-04-06 19:14:31 +00001229
1230 AaBb...Zz
1231
1232which means that [A-Z] matches every letter except `z'.
1233
1234The portable way to specify upper case letters is [:upper:] instead of
1235A-Z; lower case may be specified as [:lower:] instead of a-z.
1236
1237Look at the manual pages for setlocale(3), strcoll(3), and, if it is
1238present, locale(1). If you have locale(1), you can use it to find
1239your current locale information even if you do not have any of the
1240LC_ variables set.
1241
1242My advice is to put
1243
1244 export LC_COLLATE=C
1245
1246into /etc/profile and inspect any shell scripts run from cron for
1247constructs like [A-Z]. This will prevent things like
1248
1249 rm [A-Z]*
1250
1251from removing every file in the current directory except those beginning
1252with `z' and still allow individual users to change the collation order.
1253Users may put the above command into their own profiles as well, of course.
1254
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001255Section F: Things to watch out for on certain Unix versions
1256
Jari Aaltob72432f1999-02-19 17:11:39 +00001257F1) Why can't I use command line editing in my `cmdtool'?
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001258
1259The problem is `cmdtool' and bash fighting over the input. When
1260scrolling is enabled in a cmdtool window, cmdtool puts the tty in
1261`raw mode' to permit command-line editing using the mouse for
1262applications that cannot do it themselves. As a result, bash and
1263cmdtool each try to read keyboard input immediately, with neither
1264getting enough of it to be useful.
1265
1266This mode also causes cmdtool to not implement many of the
1267terminal functions and control sequences appearing in the
1268`sun-cmd' termcap entry. For a more complete explanation, see
1269that file examples/suncmd.termcap in the bash distribution.
1270
1271`xterm' is a better choice, and gets along with bash much more
1272smoothly.
1273
1274If you must use cmdtool, you can use the termcap description in
1275examples/suncmd.termcap. Set the TERMCAP variable to the terminal
1276description contained in that file, i.e.
1277
1278TERMCAP='Mu|sun-cmd:am:bs:km:pt:li#34:co#80:cl=^L:ce=\E[K:cd=\E[J:rs=\E[s:'
1279
1280Then export TERMCAP and start a new cmdtool window from that shell.
1281The bash command-line editing should behave better in the new
1282cmdtool. If this works, you can put the assignment to TERMCAP
1283in your bashrc file.
1284
Jari Aaltob72432f1999-02-19 17:11:39 +00001285F2) I built bash on Solaris 2. Why do globbing expansions and filename
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001286 completion chop off the first few characters of each filename?
1287
1288This is the consequence of building bash on SunOS 5 and linking
1289with the libraries in /usr/ucblib, but using the definitions
1290and structures from files in /usr/include.
1291
1292The actual conflict is between the dirent structure in
1293/usr/include/dirent.h and the struct returned by the version of
1294`readdir' in libucb.a (a 4.3-BSD style `struct direct').
1295
1296Make sure you've got /usr/ccs/bin ahead of /usr/ucb in your $PATH
1297when configuring and building bash. This will ensure that you
1298use /usr/ccs/bin/cc or acc instead of /usr/ucb/cc and that you
1299link with libc before libucb.
1300
1301If you have installed the Sun C compiler, you may also need to
1302put /usr/ccs/bin and /opt/SUNWspro/bin into your $PATH before
1303/usr/ucb.
1304
Jari Aaltob72432f1999-02-19 17:11:39 +00001305F3) Why does bash dump core after I interrupt username completion or
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001306 `~user' tilde expansion on a machine running NIS?
1307
1308This is a famous and long-standing bug in the SunOS YP (sorry, NIS)
1309client library, which is part of libc.
1310
1311The YP library code keeps static state -- a pointer into the data
1312returned from the server. When YP initializes itself (setpwent),
1313it looks at this pointer and calls free on it if it's non-null.
1314So far, so good.
1315
1316If one of the YP functions is interrupted during getpwent (the
1317exact function is interpretwithsave()), and returns NULL, the
1318pointer is freed without being reset to NULL, and the function
1319returns. The next time getpwent is called, it sees that this
1320pointer is non-null, calls free, and the bash free() blows up
1321because it's being asked to free freed memory.
1322
1323The traditional Unix mallocs allow memory to be freed multiple
1324times; that's probably why this has never been fixed. You can
1325run configure with the `--without-gnu-malloc' option to use
1326the C library malloc and avoid the problem.
1327
Jari Aaltob72432f1999-02-19 17:11:39 +00001328F4) I'm running SVR4.2. Why is the line erased every time I type `@'?
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001329
1330The `@' character is the default `line kill' character in most
1331versions of System V, including SVR4.2. You can change this
1332character to whatever you want using `stty'. For example, to
1333change the line kill character to control-u, type
1334
1335 stty kill ^U
1336
1337where the `^' and `U' can be two separate characters.
1338
Jari Aaltob72432f1999-02-19 17:11:39 +00001339F5) Why does bash report syntax errors when my C News scripts use a
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001340 redirection before a subshell command?
1341
1342The actual command in question is something like
1343
1344 < file ( command )
1345
1346According to the grammar given in the POSIX.2 standard, this construct
1347is, in fact, a syntax error. Redirections may only precede `simple
1348commands'. A subshell construct such as the above is one of the shell's
1349`compound commands'. A redirection may only follow a compound command.
1350
Jari Aaltobb706242000-03-17 21:46:59 +00001351This affects the mechanical transformation of commands that use `cat'
1352to pipe a file into a command (a favorite Useless-Use-Of-Cat topic on
1353comp.unix.shell). While most commands of the form
1354
1355 cat file | command
1356
1357can be converted to `< file command', shell control structures such as
1358loops and subshells require `command < file'.
1359
Jari Aaltof73dda02001-11-13 17:56:06 +00001360The file CWRU/sh-redir-hack in the bash-2.05a distribution is an
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001361(unofficial) patch to parse.y that will modify the grammar to
1362support this construct. It will not apply with `patch'; you must
1363modify parse.y by hand. Note that if you apply this, you must
1364recompile with -DREDIRECTION_HACK. This introduces a large
1365number of reduce/reduce conflicts into the shell grammar.
1366
Jari Aaltobb706242000-03-17 21:46:59 +00001367F6) Why can't I use vi-mode editing on Red Hat Linux 6.1?
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001368
Jari Aaltobb706242000-03-17 21:46:59 +00001369The short answer is that Red Hat screwed up.
1370
1371The long answer is that they shipped an /etc/inputrc that only works
1372for emacs mode editing, and then screwed all the vi users by setting
1373INPUTRC to /etc/inputrc in /etc/profile.
1374
1375The short fix is to do one of the following: remove or rename
1376/etc/inputrc, set INPUTRC=~/.inputrc in ~/.bashrc (or .bash_profile,
1377but make sure you export it if you do), remove the assignment to
1378INPUTRC from /etc/profile, add
1379
1380 set keymap emacs
1381
1382to the beginning of /etc/inputrc, or bracket the key bindings in
1383/etc/inputrc with these lines
1384
1385 $if mode=emacs
1386 [...]
1387 $endif
1388
1389Section G: How can I get bash to do certain common things?
1390
1391G1) How can I get bash to read and display eight-bit characters?
1392
1393This is a process requiring several steps.
1394
1395First, you must ensure that the `physical' data path is a full eight
1396bits. For xterms, for example, the `vt100' resources `eightBitInput'
1397and `eightBitOutput' should be set to `true'.
1398
1399Once you have set up an eight-bit path, you must tell the kernel and
1400tty driver to leave the eighth bit of characters alone when processing
1401keyboard input. Use `stty' to do this:
1402
1403 stty cs8 -istrip -parenb
1404
1405For old BSD-style systems, you can use
1406
1407 stty pass8
1408
1409You may also need
1410
1411 stty even odd
1412
1413Finally, you need to tell readline that you will be inputting and
1414displaying eight-bit characters. You use readline variables to do
1415this. These variables can be set in your .inputrc or using the bash
1416`bind' builtin. Here's an example using `bind':
1417
1418 bash$ bind 'set convert-meta off'
1419 bash$ bind 'set meta-flag on'
1420 bash$ bind 'set output-meta on'
1421
1422The `set' commands between the single quotes may also be placed
1423in ~/.inputrc.
1424
1425G2) How do I write a function `x' to replace builtin command `x', but
1426 still invoke the command from within the function?
1427
1428This is why the `command' and `builtin' builtins exist. The
1429`command' builtin executes the command supplied as its first
1430argument, skipping over any function defined with that name. The
1431`builtin' builtin executes the builtin command given as its first
1432argument directly.
1433
1434For example, to write a function to replace `cd' that writes the
1435hostname and current directory to an xterm title bar, use
1436something like the following:
1437
1438 cd()
1439 {
1440 builtin cd "$@" && xtitle "$HOST: $PWD"
1441 }
1442
1443This could also be written using `command' instead of `builtin';
1444the version above is marginally more efficient.
1445
1446G3) How can I find the value of a shell variable whose name is the value
1447 of another shell variable?
1448
1449Versions of Bash newer than Bash-2.0 support this directly. You can use
1450
1451 ${!var}
1452
1453For example, the following sequence of commands will echo `z':
1454
1455 var1=var2
1456 var2=z
1457 echo ${!var1}
1458
1459For sh compatibility, use the `eval' builtin. The important
1460thing to remember is that `eval' expands the arguments you give
1461it again, so you need to quote the parts of the arguments that
1462you want `eval' to act on.
1463
1464For example, this expression prints the value of the last positional
1465parameter:
1466
1467 eval echo \"\$\{$#\}\"
1468
1469The expansion of the quoted portions of this expression will be
1470deferred until `eval' runs, while the `$#' will be expanded
1471before `eval' is executed. In versions of bash later than bash-2.0,
1472
1473 echo ${!#}
1474
1475does the same thing.
1476
Jari Aaltof73dda02001-11-13 17:56:06 +00001477This is not the same thing as ksh93 `nameref' variables, though the syntax
1478is similar. I may add namerefs in a future bash version.
1479
Jari Aaltobb706242000-03-17 21:46:59 +00001480G4) How can I make the bash `time' reserved word print timing output that
1481 looks like the output from my system's /usr/bin/time?
1482
1483The bash command timing code looks for a variable `TIMEFORMAT' and
1484uses its value as a format string to decide how to display the
1485timing statistics.
1486
1487The value of TIMEFORMAT is a string with `%' escapes expanded in a
1488fashion similar in spirit to printf(3). The manual page explains
1489the meanings of the escape sequences in the format string.
1490
1491If TIMEFORMAT is not set, bash acts as if the following assignment had
1492been performed:
1493
1494 TIMEFORMAT=$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
1495
1496The POSIX.2 default time format (used by `time -p command') is
1497
1498 TIMEFORMAT=$'real %2R\nuser %2U\nsys %2S'
1499
1500The BSD /usr/bin/time format can be emulated with:
1501
1502 TIMEFORMAT=$'\t%1R real\t%1U user\t%1S sys'
1503
1504The System V /usr/bin/time format can be emulated with:
1505
1506 TIMEFORMAT=$'\nreal\t%1R\nuser\t%1U\nsys\t%1S'
1507
1508The ksh format can be emulated with:
1509
1510 TIMEFORMAT=$'\nreal\t%2lR\nuser\t%2lU\nsys\t%2lS'
1511
1512G5) How do I get the current directory into my prompt?
1513
1514Bash provides a number of backslash-escape sequences which are expanded
1515when the prompt string (PS1 or PS2) is displayed. The full list is in
1516the manual page.
1517
1518The \w expansion gives the full pathname of the current directory, with
1519a tilde (`~') substituted for the current value of $HOME. The \W
1520expansion gives the basename of the current directory. To put the full
1521pathname of the current directory into the path without any tilde
1522subsitution, use $PWD. Here are some examples:
1523
1524 PS1='\w$ ' # current directory with tilde
1525 PS1='\W$ ' # basename of current directory
1526 PS1='$PWD$ ' # full pathname of current directory
1527
1528The single quotes are important in the final example to prevent $PWD from
1529being expanded when the assignment to PS1 is performed.
1530
1531G6) How can I rename "*.foo" to "*.bar"?
1532
1533Use the pattern removal functionality described in D3. The following `for'
1534loop will do the trick:
1535
1536 for f in *.foo; do
1537 mv $f ${f%foo}bar
1538 done
1539
1540G7) How can I translate a filename from uppercase to lowercase?
1541
1542The script examples/functions/lowercase, originally written by John DuBois,
1543will do the trick. The converse is left as an exercise.
1544
1545G8) How can I write a filename expansion (globbing) pattern that will match
1546 all files in the current directory except "." and ".."?
1547
1548You must have set the `extglob' shell option using `shopt -s extglob' to use
1549this:
1550
1551 echo .!(.|) *
1552
1553A solution that works without extended globbing is given in the Unix Shell
1554FAQ, posted periodically to comp.unix.shell.
1555
1556Section H: Where do I go from here?
1557
1558H1) How do I report bugs in bash, and where should I look for fixes and
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001559 advice?
1560
1561Use the `bashbug' script to report bugs. It is built and
1562installed at the same time as bash. It provides a standard
1563template for reporting a problem and automatically includes
1564information about your configuration and build environment.
1565
Jari Aaltob72432f1999-02-19 17:11:39 +00001566`bashbug' sends its reports to bug-bash@gnu.org, which
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001567is a large mailing list gatewayed to the usenet newsgroup gnu.bash.bug.
1568
1569Bug fixes, answers to questions, and announcements of new releases
1570are all posted to gnu.bash.bug. Discussions concerning bash features
1571and problems also take place there.
1572
1573To reach the bash maintainers directly, send mail to
Jari Aaltob72432f1999-02-19 17:11:39 +00001574bash-maintainers@gnu.org.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001575
Jari Aaltobb706242000-03-17 21:46:59 +00001576H2) What kind of bash documentation is there?
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001577
1578First, look in the doc directory in the bash distribution. It should
1579contain at least the following files:
1580
1581bash.1 an extensive, thorough Unix-style manual page
1582builtins.1 a manual page covering just bash builtin commands
Jari Aaltob72432f1999-02-19 17:11:39 +00001583bashref.texi a reference manual in GNU tex`info format
1584bashref.info an info version of the reference manual
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001585FAQ this file
1586article.ms text of an article written for The Linux Journal
1587readline.3 a man page describing readline
1588
Jari Aaltob72432f1999-02-19 17:11:39 +00001589Postscript, HTML, and ASCII files created from the above source are
1590available in the documentation distribution.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001591
1592There is additional documentation available for anonymous FTP from host
Jari Aaltocce855b1998-04-17 19:52:44 +00001593ftp.cwru.edu in the `pub/bash' directory.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001594
1595Cameron Newham and Bill Rosenblatt have written a book on bash, published
1596by O'Reilly and Associates. The book is based on Bill Rosenblatt's Korn
Jari Aaltod166f041997-06-05 14:59:13 +00001597Shell book. The title is ``Learning the Bash Shell'', and the ISBN number
1598is 1-56592-147-X. Look for it in fine bookstores near you. This book
1599covers bash-1.14, but has an appendix describing some of the new features
Jari Aaltocce855b1998-04-17 19:52:44 +00001600in bash-2.0.
1601
Jari Aaltobc4cd231998-07-23 14:37:54 +00001602A second edition of this book is available, published in January, 1998.
Jari Aaltocce855b1998-04-17 19:52:44 +00001603The ISBN number is 1-56592-347-2. Look for it in the same fine bookstores
1604or on the web.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001605
Jari Aaltobb706242000-03-17 21:46:59 +00001606H3) What's coming in future versions?
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001607
Jari Aaltof73dda02001-11-13 17:56:06 +00001608These are features I hope to include in a future version of bash.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001609
Jari Aaltof73dda02001-11-13 17:56:06 +00001610a better bash debugger (a minimally-tested version is included with bash-2.05a)
Jari Aaltobb706242000-03-17 21:46:59 +00001611associative arrays
Jari Aalto28ef6c32001-04-06 19:14:31 +00001612changes to the DEBUG trap to be compatible with ksh93 (which runs the
1613trap before each simple command, instead of after each one like previous
1614versions)
Jari Aaltof73dda02001-11-13 17:56:06 +00001615co-processes, but with a new-style syntax that looks like function declaration
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001616
Jari Aaltobb706242000-03-17 21:46:59 +00001617H4) What's on the bash `wish list' for future versions?
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001618
1619These are features that may or may not appear in a future version of bash.
1620
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001621breaking some of the shell functionality into embeddable libraries
Jari Aaltobb706242000-03-17 21:46:59 +00001622a module system like zsh's, using dynamic loading like builtins
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001623better internationalization using GNU `gettext'
1624an option to use external files for the long `help' text
Jari Aaltocce855b1998-04-17 19:52:44 +00001625date-stamped command history
Jari Aaltobb706242000-03-17 21:46:59 +00001626a bash programmer's guide with a chapter on creating loadable builtins
1627a better loadable interface to perl with access to the shell builtins and
1628 variables (contributions gratefully accepted)
Jari Aaltof73dda02001-11-13 17:56:06 +00001629ksh93-like `nameref' variables
1630ksh93-like `+=' variable assignment operator
1631ksh93-like `xx.yy' variables (including some of the .sh.* variables) and
1632 associated disipline functions
1633Some of the new ksh93 pattern matching operators, like backreferencing
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001634
Jari Aaltobb706242000-03-17 21:46:59 +00001635H5) When will the next release appear?
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001636
Jari Aaltof73dda02001-11-13 17:56:06 +00001637The next version will appear sometime in 2002. Never make predictions.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001638
1639
Jari Aalto28ef6c32001-04-06 19:14:31 +00001640This document is Copyright 1995-2001 by Chester Ramey.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001641
1642Permission is hereby granted, without written agreement and
1643without license or royalty fees, to use, copy, and distribute
1644this document for any purpose, provided that the above copyright
1645notice appears in all copies of this document and that the
1646contents of this document remain unaltered.