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