1 This is the Bash FAQ, version 3.17, for Bash version 2.05a.
3 This document contains a set of frequently-asked questions concerning
4 Bash, the GNU Bourne-Again Shell. Bash is a freely-available command
5 interpreter with advanced features for both interactive use and shell
8 Another good source of basic information about shells is the collection
9 of FAQ articles periodically posted to comp.unix.shell.
11 Questions and comments concerning this document should be sent to
14 This document is available for anonymous FTP with the URL
16 ftp://ftp.cwru.edu/pub/bash/FAQ
18 The Bash home page is http://cnswww.cns.cwru.edu/~chet/bash/bashtop.html
26 A2) What's the latest version?
27 A3) Where can I get it?
28 A4) On what machines will bash run?
29 A5) Will bash run on operating systems other than Unix?
30 A6) How can I build bash with gcc?
31 A7) How can I make bash my login shell?
32 A8) I just changed my login shell to bash, and now I can't FTP into my
34 A9) What's the `POSIX 1003.2 standard'?
35 A10) What is the bash `posix mode'?
37 Section B: The latest version
39 B1) What's new in version 2.05a?
40 B2) Are there any user-visible incompatibilities between bash-2.05a and
43 Section C: Differences from other Unix shells
45 C1) How does bash differ from sh, the Bourne shell?
46 C2) How does bash differ from the Korn shell, version ksh88?
47 C3) Which new features in ksh-93 are not in bash, and which are?
49 Section D: Why does bash do some things differently than other Unix shells?
51 D1) Why does bash run a different version of `command' than
52 `which command' says it will?
53 D2) Why doesn't bash treat brace expansions exactly like csh?
54 D3) Why doesn't bash have csh variable modifiers?
55 D4) How can I make my csh aliases work when I convert to bash?
56 D5) How can I pipe standard output and standard error from one command to
57 another, like csh does with `|&'?
58 D6) Now that I've converted from ksh to bash, are there equivalents to
59 ksh features like autoloaded functions and the `whence' command?
61 Section E: Why does bash do certain things the way it does?
63 E1) Why is the bash builtin `test' slightly different from /bin/test?
64 E2) Why does bash sometimes say `Broken pipe'?
65 E3) When I have terminal escape sequences in my prompt, why does bash
66 wrap lines at the wrong column?
67 E4) If I pipe the output of a command into `read variable', why doesn't
68 the output show up in $variable when the read command finishes?
69 E5) I have a bunch of shell scripts that use backslash-escaped characters
70 in arguments to `echo'. Bash doesn't interpret these characters. Why
71 not, and how can I make it understand them?
72 E6) Why doesn't a while or for loop get suspended when I type ^Z?
73 E7) What about empty for loops in Makefiles?
74 E8) Why does the arithmetic evaluation code complain about `08'?
75 E9) Why does the pattern matching expression [A-Z]* match files beginning
76 with every letter except `z'?
78 Section F: Things to watch out for on certain Unix versions
80 F1) Why can't I use command line editing in my `cmdtool'?
81 F2) I built bash on Solaris 2. Why do globbing expansions and filename
82 completion chop off the first few characters of each filename?
83 F3) Why does bash dump core after I interrupt username completion or
84 `~user' tilde expansion on a machine running NIS?
85 F4) I'm running SVR4.2. Why is the line erased every time I type `@'?
86 F5) Why does bash report syntax errors when my C News scripts use a
87 redirection before a subshell command?
88 F6) Why can't I use vi-mode editing on Red Hat Linux 6.1?
90 Section G: How can I get bash to do certain common things?
92 G1) How can I get bash to read and display eight-bit characters?
93 G2) How do I write a function `x' to replace builtin command `x', but
94 still invoke the command from within the function?
95 G3) How can I find the value of a shell variable whose name is the value
96 of another shell variable?
97 G4) 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?
99 G5) How do I get the current directory into my prompt?
100 G6) How can I rename "*.foo" to "*.bar"?
101 G7) How can I translate a filename from uppercase to lowercase?
102 G8) How can I write a filename expansion (globbing) pattern that will match
103 all files in the current directory except "." and ".."?
105 Section H: Where do I go from here?
107 H1) How do I report bugs in bash, and where should I look for fixes and
109 H2) What kind of bash documentation is there?
110 H3) What's coming in future versions?
111 H4) What's on the bash `wish list'?
112 H5) When will the next release appear?
115 Section A: The Basics
119 Bash is a Unix command interpreter (shell). It is an implementation of
120 the Posix 1003.2 shell standard, and resembles the Korn and System V
123 Bash contains a number of enhancements over those shells, both
124 for interactive use and shell programming. Features geared
125 toward interactive use include command line editing, command
126 history, job control, aliases, and prompt expansion. Programming
127 features include additional variable expansions, shell
128 arithmetic, and a number of variables and options to control
131 Bash was originally written by Brian Fox of the Free Software
132 Foundation. The current developer and maintainer is Chet Ramey
133 of Case Western Reserve University.
135 A2) What's the latest version?
137 The latest version is 2.05a, first made available on Thursday, 15
140 A3) Where can I get it?
142 Bash is the GNU project's shell, and so is available from the
143 master GNU archive site, ftp.gnu.org, and its mirrors. The
144 latest version is also available for FTP from ftp.cwru.edu.
145 The following URLs tell how to get version 2.05a:
147 ftp://ftp.gnu.org/pub/gnu/bash/bash-2.05a.tar.gz
148 ftp://ftp.cwru.edu/pub/bash/bash-2.05a.tar.gz
150 Formatted versions of the documentation are available with the URLs:
152 ftp://ftp.gnu.org/pub/gnu/bash/bash-doc-2.05a.tar.gz
153 ftp://ftp.cwru.edu/pub/bash/bash-doc-2.05a.tar.gz
155 A4) On what machines will bash run?
157 Bash has been ported to nearly every version of UNIX. All you
158 should have to do to build it on a machine for which a port
159 exists is to type `configure' and then `make'. The build process
160 will attempt to discover the version of UNIX you have and tailor
161 itself accordingly, using a script created by GNU autoconf.
163 More information appears in the file `INSTALL' in the distribution.
165 The Bash web page (http://cnswww.cns.cwru.edu/~chet/bash/bashtop.html)
166 explains how to obtain binary versions of bash for most of the major
167 commercial Unix systems.
169 A5) Will bash run on operating systems other than Unix?
171 Configuration specifics for Unix-like systems such as QNX and
172 LynxOS are included in the distribution. Bash-2.05 and later
173 versions should compile and run on Minix 2.0 (patches were
174 contributed), but I don't believe anyone has built bash-2.x on
175 earlier Minix versions yet.
177 Bash has been ported to versions of Windows implementing the Win32
178 programming interface. This includes Windows 95 and Windows NT.
179 The port was done by Cygnus Solutions as part of their CYGWIN
180 project. For more information about the project, look at the URLs
182 http://www.cygwin.com/
183 http://sourceware.cygnus.com/cygwin
185 Cygnus originally ported bash-1.14.7, and that port was part of their
186 early GNU-Win32 (the original name) releases. Cygnus has also done a
187 port of bash-2.05 to the CYGWIN environment, and it is available as
188 part of their current release.
190 Bash-2.05a should require no local Cygnus changes to build and run under
193 The 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
196 ftp://ftp.gnustep.org//pub/win32/bash-alpha-nt-1.01.tar.gz
198 DJ Delorie has a port of bash-2.x which runs under MS-DOS, as part
199 of the DJGPP project. For more information on the project, see
201 http://www.delorie.com/djgpp/
203 I have been told that the original DJGPP port was done by Daisuke Aoyama.
205 Mark Elbrecht <snowball3@bigfoot.com> has sent me notice that bash-2.04
206 is available for DJGPP V2. The files are available as:
208 ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh204b.zip binary
209 ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh204d.zip documentation
210 ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh204s.zip source
212 Mark has begun to work with bash-2.05, but I don't know the status.
214 Ports of bash-1.12 and bash-2.0 are available for OS/2 from
216 ftp://hobbes.nmsu.edu/pub/os2/util/shell/bash_112.zip
217 ftp://hobbes.nmsu.edu/pub/os2/util/shell/bash-2.0(253).zip
219 I haven't looked at either, but the second appears to be a binary-only
220 distribution. Beware.
222 I have received word that Bash (I'm not sure which version, but I
223 believe that it's at least bash-2.02.1) is the standard shell on
226 A6) How can I build bash with gcc?
228 Bash configures to use gcc by default if it is available. Read the
229 file INSTALL in the distribution for more information.
231 A7) How can I make bash my login shell?
233 Some machines let you use `chsh' to change your login shell. Other
234 systems use `passwd -s' or `passwd -e'. If one of these works for
235 you, that's all you need. Note that many systems require the full
236 pathname to a shell to appear in /etc/shells before you can make it
237 your login shell. For this, you may need the assistance of your
238 friendly local system administrator.
240 If you cannot do this, you can still use bash as your login shell, but
241 you need to perform some tricks. The basic idea is to add a command
242 to your login shell's startup file to replace your login shell with
245 For example, if your login shell is csh or tcsh, and you have installed
246 bash in /usr/gnu/bin/bash, add the following line to ~/.login:
248 if ( -f /usr/gnu/bin/bash ) exec /usr/gnu/bin/bash --login
250 (the `--login' tells bash that it is a login shell).
252 It's not a good idea to put this command into ~/.cshrc, because every
253 csh you run without the `-f' option, even ones started to run csh scripts,
254 reads that file. If you must put the command in ~/.cshrc, use something
257 if ( $?prompt ) exec /usr/gnu/bin/bash --login
259 to ensure that bash is exec'd only when the csh is interactive.
261 If your login shell is sh or ksh, you have to do two things.
263 First, create an empty file in your home directory named `.bash_profile'.
264 The existence of this file will prevent the exec'd bash from trying to
265 read ~/.profile, and re-execing itself over and over again. ~/.bash_profile
266 is the first file bash tries to read initialization commands from when
267 it is invoked as a login shell.
269 Next, add a line similar to the above to ~/.profile:
271 [ -f /usr/gnu/bin/bash ] && exec /usr/gnu/bin/bash --login
273 This will cause login shells to replace themselves with bash running as
274 a login shell. Once you have this working, you can copy your initialization
275 code from ~/.profile to ~/.bash_profile.
277 I have received word that the recipe supplied above is insufficient for
278 machines running CDE. CDE has a maze of twisty little startup files, all
281 If you cannot change your login shell in the password file to bash, you
282 will have to (apparently) live with CDE using the shell in the password
283 file to run its startup scripts. If you have changed your shell to bash,
284 there is code in the CDE startup files (on Solaris, at least) that attempts
285 to do the right thing. It is, however, often broken, and may require that
286 you use the $BASH_ENV trick described below.
288 `dtterm' claims to use $SHELL as the default program to start, so if you
289 can change $SHELL in the CDE startup files, you should be able to use bash
290 in your terminal windows.
292 Setting DTSOURCEPROFILE in ~/.dtprofile will cause the `Xsession' program
293 to read your login shell's startup files. You may be able to use bash for
294 the rest of the CDE programs by setting SHELL to bash in ~/.dtprofile as
295 well, but I have not tried this.
297 You can use the above `exec' recipe to start bash when not logging in with
298 CDE by testing the value of the DT variable:
300 if [ -n "$DT" ]; then
301 [ -f /usr/gnu/bin/bash ] && exec /usr/gnu/bin/bash --login
304 If CDE starts its shells non-interactively during login, the login shell
305 startup files (~/.profile, ~/.bash_profile) will not be sourced at login.
306 To get around this problem, append a line similar to the following to your
309 BASH_ENV=${HOME}/.bash_profile ; export BASH_ENV
311 and add the following line to the beginning of ~/.bash_profile:
315 A8) I just changed my login shell to bash, and now I can't FTP into my
318 You must add the full pathname to bash to the file /etc/shells. As
319 noted in the answer to the previous question, many systems require
320 this before you can make bash your login shell.
322 Most versions of ftpd use this file to prohibit `special' users
323 such as `uucp' and `news' from using FTP.
325 A9) What's the `POSIX 1003.2 standard'?
327 POSIX is a name originally coined by Richard Stallman for a
328 family of open system standards based on UNIX. There are a
329 number of aspects of UNIX under consideration for
330 standardization, from the basic system services at the system
331 call and C library level to applications and tools to system
332 administration and management. Each area of standardization is
333 assigned to a working group in the 1003 series.
335 The POSIX Shell and Utilities standard has been developed by IEEE
336 Working Group 1003.2 (POSIX.2). It concentrates on the command
337 interpreter interface and utility programs commonly executed from
338 the command line or by other programs. An initial version of the
339 standard has been approved and published by the IEEE, and work is
340 currently underway to update it.
342 Bash is concerned with the aspects of the shell's behavior
343 defined by POSIX.2. The shell command language has of course
344 been standardized, including the basic flow control and program
345 execution constructs, I/O redirection and pipelining, argument
346 handling, variable expansion, and quoting.
348 The `special' builtins, which must be implemented as part of the
349 shell to provide the desired functionality, are specified as
350 being part of the shell; examples of these are `eval' and
351 `export'. Other utilities appear in the sections of POSIX.2 not
352 devoted to the shell which are commonly (and in some cases must
353 be) implemented as builtin commands, such as `read' and `test'.
354 POSIX.2 also specifies aspects of the shell's interactive
355 behavior as part of the UPE, including job control and command
356 line editing. Only vi-style line editing commands have been
357 standardized; emacs editing commands were left out due to
360 The Open Group has made an older version of its Single Unix
361 Specification (version 2), which is very similar to POSIX.2,
362 available on the web at
364 http://www.opengroup.org/onlinepubs/007908799/
366 A10) What is the bash `posix mode'?
368 Although bash is an implementation of the POSIX.2 shell
369 specification, there are areas where the bash default behavior
370 differs from that spec. The bash `posix mode' changes the bash
371 behavior in these areas so that it obeys the spec more closely.
373 Posix mode is entered by starting bash with the --posix option or
374 executing `set -o posix' after bash is running.
376 The specific aspects of bash which change when posix mode is
377 active are listed in the file CWRU/POSIX.NOTES in the bash
378 distribution. They are also listed in a section in the Bash
381 Section B: The latest version
383 B1) What's new in version 2.05a?
385 The raison d'etre for bash-2.05a is to make an intermediate release
386 containing principally bug fixes (some very good work was done and
387 contributed after bash-2.05 was released) available before I start to
388 work 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
390 only a few relatively minor new features.
392 Bash-2.05a contains the following new features (see the manual page for
393 complete descriptions and the CHANGES and NEWS files in the bash-2.05a
396 o The `printf' builtin has undergone major work
398 o There is a new read-only `shopt' option: login_shell, which is set by
399 login shells and unset otherwise
401 o New `\A' prompt string escape sequence; expanding to time in 24-hour
404 o New `-A group/-g' option to complete and compgen; goes group name
407 o New [+-]O invocation option to set and unset `shopt' options at startup
409 o ksh-like `ERR' trap
411 o `for' loops now allow empty word lists after the `in' reserved word
413 o new `hard' and `soft' arguments for the `ulimit' builtin
415 o Readline can be configured to place the user at the same point on the line
416 when retrieving commands from the history list
418 o Readline can be configured to skip `hidden' files (filenames with a leading
419 `.' on Unix) when performing completion
421 A short feature history dating from bash-2.0:
423 Bash-2.05 introduced the following new features:
425 o This version has once again reverted to using locales and strcoll(3) when
426 processing pattern matching bracket expressions, as POSIX requires.
427 o Added a new `--init-file' invocation argument as a synonym for `--rcfile',
428 per the new GNU coding standards.
429 o The /dev/tcp and /dev/udp redirections now accept service names as well as
431 o `complete' and `compgen' now take a `-o value' option, which controls some
432 of the aspects of that compspec. Valid values are:
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
441 o A new loadable builtin, realpath, which canonicalizes and expands symlinks
442 in pathname arguments.
443 o 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.
448 Bash-2.04 introduced the following new features:
450 o Programmable word completion with the new `complete' and `compgen' builtins;
451 examples are provided in examples/complete/complete-examples
452 o `history' has a new `-d' option to delete a history entry
453 o `bind' has a new `-x' option to bind key sequences to shell commands
454 o The prompt expansion code has new `\j' and `\l' escape sequences
455 o The `no_empty_cmd_completion' shell option, if enabled, inhibits
456 command completion when TAB is typed on an empty line
457 o `help' has a new `-s' option to print a usage synopsis
458 o New arithmetic operators: var++, var--, ++var, --var, expr1,expr2 (comma)
459 o New ksh93-style arithmetic for command:
460 for ((expr1 ; expr2; expr3 )); do list; done
461 o `read' has new options: `-t', `-n', `-d', `-s'
462 o The redirection code handles several filenames specially: /dev/fd/N,
463 /dev/stdin, /dev/stdout, /dev/stderr
464 o 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
467 o The ${!prefix*} expansion has been implemented
468 o A new FUNCNAME variable, which expands to the name of a currently-executing
470 o The GROUPS variable is no longer readonly
471 o A new shopt `xpg_echo' variable, to control the behavior of echo with
472 respect to backslash-escape sequences at runtime
473 o The NON_INTERACTIVE_LOGIN_SHELLS #define has returned
475 The version of Readline released with Bash-2.04, Readline-4.1, had several
476 new features as well:
478 o Parentheses matching is always compiled into readline, and controllable
479 with the new `blink-matching-paren' variable
480 o 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
483 o A new function for applications: rl_on_new_line_with_prompt()
484 o New variables for applications: rl_already_prompted, and rl_gnu_readline_p
487 Bash-2.03 had very few new features, in keeping with the convention
488 that odd-numbered releases provide mainly bug fixes. A number of new
489 features were added to Readline, mostly at the request of the Cygnus
492 A new shopt option, `restricted_shell', so that startup files can test
493 whether or not the shell was started in restricted mode
494 Filename generation is now performed on the words between ( and ) in
495 compound array assignments (this is really a bug fix)
496 OLDPWD is now auto-exported, as POSIX.2 requires
497 ENV and BASH_ENV are read-only variables in a restricted shell
498 Bash may now be linked against an already-installed Readline library,
499 as long as the Readline library is version 4 or newer
500 All shells begun with the `--login' option will source the login shell
501 startup files, even if the shell is not interactive
503 There were lots of changes to the version of the Readline library released
504 along with Bash-2.03. For a complete list of the changes, read the file
505 CHANGES in the Bash-2.03 distribution.
507 Bash-2.02 contained the following new features:
509 a 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
514 POSIX.2-style globbing character classes ([:alpha:], [:alnum:], etc.)
515 POSIX.2-style globbing equivalence classes
516 POSIX.2-style globbing collating symbols
517 the ksh [[...]] extended conditional command
518 the ksh egrep-style extended pattern matching operators
519 a new `printf' builtin
520 the ksh-like $(<filename) command substitution, which is equivalent to
522 new tilde prefixes that expand to directories from the directory stack
523 new `**' arithmetic operator to do exponentiation
524 case-insensitive globbing (filename expansion)
525 menu completion a la tcsh
526 `magic-space' history expansion function like tcsh
527 the readline inputrc `language' has a new file inclusion directive ($include)
529 Bash-2.01 contained only a few new features:
531 new `GROUPS' builtin array variable containing the user's group list
532 new bindable readline commands: history-and-alias-expand-line and
535 Bash-2.0 contained extensive changes and new features from bash-1.14.7.
538 new `time' reserved word to time pipelines, shell builtins, and
540 one-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
543 new quoting syntaxes for ANSI-C string expansion and locale-specific
545 new expansions to do substring extraction, pattern replacement, and
546 indirect variable expansion
547 new builtins: `disown' and `shopt'
548 new variables: HISTIGNORE, SHELLOPTS, PIPESTATUS, DIRSTACK, GLOBIGNORE,
549 MACHTYPE, BASH_VERSINFO
550 special handling of many unused or redundant variables removed
551 (e.g., $notify, $glob_dot_filenames, $no_exit_on_failed_exec)
552 dynamic loading of new builtin commands; many loadable examples provided
553 new prompt expansions: \a, \e, \n, \H, \T, \@, \v, \V
554 history and aliases available in shell scripts
555 new readline variables: enable-keypad, mark-directories, input-meta,
556 visible-stats, disable-completion, comment-begin
557 new readline commands to manipulate the mark and operate on the region
558 new readline emacs mode commands and bindings for ksh-88 compatibility
559 updated and extended builtins
561 expanded (and now documented) restricted shell mode
563 implementation stuff:
564 autoconf-based configuration
565 nearly all of the bugs reported since version 1.14 have been fixed
566 most builtins converted to use builtin `getopt' for consistency
567 most builtins use -p option to display output in a reusable form
569 grammar tighter and smaller (66 reduce-reduce conflicts gone)
570 lots of code now smaller and faster
571 test suite greatly expanded
573 B2) Are there any user-visible incompatibilities between bash-2.05a and
576 There are a few incompatibilities between version 1.14.7 and version 2.05a.
577 They are detailed in the file COMPAT in the bash-2.05a distribution. That
578 file is not meant to be all-encompassing; send mail to bash-maintainers@gnu.org
579 if you find something that's not mentioned there.
581 Section C: Differences from other Unix shells
583 C1) How does bash differ from sh, the Bourne shell?
585 This is a non-comprehensive list of features that differentiate bash
586 from the SVR4.2 shell. The bash manual page explains these more
589 Things bash has that sh does not:
590 long invocation options
591 [+-]O invocation option
592 `!' reserved word to invert pipeline return value
593 `time' reserved word to time pipelines and shell builtins
594 the `function' reserved word
595 the `select' compound command and reserved word
596 arithmetic for command: for ((expr1 ; expr2; expr3 )); do list; done
597 new $'...' and $"..." quoting
598 the $(...) form of command substitution
599 the $(<filename) form of command substitution, equivalent to
601 the ${#param} parameter value length operator
602 the ${!param} indirect parameter expansion operator
603 the ${!param*} prefix expansion operator
604 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,
612 HISTFILESIZE, HISTCONTROL, HISTIGNORE, GLOBIGNORE, GROUPS,
613 PROMPT_COMMAND, FCEDIT, FIGNORE, IGNOREEOF, INPUTRC,
614 SHELLOPTS, OPTERR, HOSTFILE, TMOUT, FUNCNAME, histchars,
618 variable arrays with new compound assignment syntax
619 redirections: <>, &>, >|
620 prompt string special char translation and variable expansion
621 auto-export of variables in initial environment
622 command search finds functions before builtins
623 bash return builtin will exit a file sourced with `.'
624 builtins: cd -/-L/-P, exec -l/-c/-a, echo -e/-E, hash -p/-t.
625 export -n/-f/-p/name=value, pwd -L/-P,
626 read -e/-p/-a/-t/-n/-d/-s,
627 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
637 arithmetic expansion with $((...)) and `let' builtin
638 the `[[...]]' extended conditional command
640 aliases and alias/unalias builtins
641 local variables in functions and `local' builtin
642 readline and command-line editing with programmable completion
643 command history and history/fc builtins
644 csh-like history expansion
645 other new bash builtins: bind, command, compgen, complete, builtin,
646 declare/typeset, dirs, enable, fc, help,
647 history, logout, popd, pushd, disown, shopt,
650 filename generation when using output redirection (command >a*)
651 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
656 variable assignments preceding commands affect only that command,
657 even for builtins and functions
659 redirection to /dev/fd/N, /dev/stdin, /dev/stdout, /dev/stderr,
660 /dev/tcp/host/port, /dev/udp/host/port
662 Things 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')
666 turns on job control if called as `jsh'
667 $TIMEOUT (like bash $TMOUT)
668 `^' is a synonym for `|'
669 new SVR4.2 sh builtins: mldmode, priv
671 Implementation 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
682 to open a file named `-v', and, on SunOS 4.1.4, dumps core.
683 On Solaris 2.4 and earlier versions, sh goes into an infinite
685 sh exits a script if any builtin fails; bash exits only if one of
686 the POSIX.2 `special' builtins fails
688 C2) How does bash differ from the Korn shell, version ksh88?
690 Things bash has or uses that ksh88 does not:
691 long invocation options
692 [-+]O invocation option
694 arithmetic for command: for ((expr1 ; expr2; expr3 )); do list; done
695 posix mode and posix conformance
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
700 the ${!param*} prefix expansion operator
701 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,
708 GROUPS, FUNCNAME, histchars, auto_resume
709 prompt expansion with backslash escapes and command substitution
710 redirection: &> (stdout and stderr)
711 more extensive and extensible editing and programmable completion
712 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,
715 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
721 `!' csh-style history expansion
722 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
728 redirection to /dev/fd/N, /dev/stdin, /dev/stdout, /dev/stderr
729 arrays of unlimited size
731 Things ksh88 has or uses that bash does not:
732 tracked aliases (alias -t)
733 variables: ERRNO, FPATH, EDITOR, VISUAL
734 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,
741 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
746 Implementation differences:
747 ksh runs last command of a pipeline in parent shell context
748 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
752 bash waits for all commands in pipeline to exit before returning status
753 emacs-mode editing has some slightly different key bindings
755 C3) Which new features in ksh-93 are not in bash, and which are?
757 New things in ksh-93 not in bash-2.05a:
759 floating point arithmetic and variables
760 math library functions
761 ${!name[sub]} name of subscript for associative array
762 `.' is allowed in variable names to create a hierarchical namespace
763 more extensive compound assignment syntax
765 `sleep' and `getconf' builtins (bash has loadable versions)
766 typeset -n and `nameref' variables
768 variables: .sh.edchar, .sh.edmode, .sh.edcol, .sh.edtext, .sh.version,
769 .sh.name, .sh.subscript, .sh.value, .sh.match, HISTEDIT
770 backreferences in pattern matching (\N)
771 `&' operator in pattern lists for matching
772 print -f (bash uses printf)
773 `fc' has been renamed to `hist'
774 `.' can execute shell functions
775 exit statuses between 0 and 255
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
783 DEBUG trap now executed before each simple command, instead of after
784 printf %H, %P, %T, %Z modifiers, output base for %d
786 New things in ksh-93 present in bash-2.05a:
787 for (( expr1; expr2; expr3 )) ; do list; done - arithmetic for command
788 ?:, ++, --, `expr1 , expr2' arithmetic operators
789 expansions: ${!param}, ${param:offset[:len]}, ${param/pat[/str]},
791 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
798 changes to kill builtin
799 read -A (bash uses read -a)
803 `.' restores the positional parameters when it completes
807 command and arithmetic substitution performed on PS1, PS4, and ENV
808 command name completion
809 ENV processed only for interactive shells
811 Section D: Why does bash do some things differently than other Unix shells?
813 D1) Why does bash run a different version of `command' than
814 `which command' says it will?
816 On many systems, `which' is actually a csh script that assumes
817 you're running csh. In tcsh, `which' and its cousin `where'
818 are builtins. On other Unix systems, `which' is a perl script
819 that uses the PATH environment variable.
821 The csh script version reads the csh startup files from your
822 home directory and uses those to determine which `command' will
823 be invoked. Since bash doesn't use any of those startup files,
824 there's a good chance that your bash environment differs from
825 your csh environment. The bash `type' builtin does everything
826 `which' does, and will report correct results for the running
827 shell. If you're really wedded to the name `which', try adding
828 the following function definition to your .bashrc:
835 If you're moving from tcsh and would like to bring `where' along
836 as well, use this function:
843 D2) Why doesn't bash treat brace expansions exactly like csh?
845 The only difference between bash and csh brace expansion is that
846 bash requires a brace expression to contain at least one unquoted
847 comma if it is to be expanded. Any brace-surrounded word not
848 containing an unquoted comma is left unchanged by the brace
849 expansion code. This affords the greatest degree of sh
852 Bash, ksh, zsh, and pd-ksh all implement brace expansion this way.
854 D3) Why doesn't bash have csh variable modifiers?
856 Posix has specified a more powerful, albeit somewhat more cryptic,
857 mechanism cribbed from ksh, and bash implements it.
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.
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.
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.
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.
909 D4) How can I make my csh aliases work when I convert to bash?
911 Bash uses a different syntax to support aliases than csh does.
912 The details can be found in the documentation. We have provided
913 a shell script which does most of the work of conversion for you;
914 this script can be found in ./examples/misc/aliasconv.sh. Here is
917 Start csh in the normal way for you. (e.g., `csh')
919 Pipe the output of `alias' through `aliasconv.sh', saving the
920 results into `bash_aliases':
922 alias | bash aliasconv.sh >bash_aliases
924 Edit `bash_aliases', carefully reading through any created
925 functions. You will need to change the names of some csh specific
926 variables to the bash equivalents. The script converts $cwd to
927 $PWD, $term to $TERM, $home to $HOME, $user to $USER, and $prompt
928 to $PS1. You may also have to add quotes to avoid unwanted
931 For example, the csh alias:
933 alias cd 'cd \!*; echo $cwd'
935 is converted to the bash function:
937 cd () { command cd "$@"; echo $PWD ; }
939 The only thing that needs to be done is to quote $PWD:
941 cd () { command cd "$@"; echo "$PWD" ; }
943 Merge the edited file into your ~/.bashrc.
945 There is an additional, more ambitious, script in
946 examples/misc/cshtobash that attempts to convert your entire csh
947 environment to its bash equivalent. This script can be run as
948 simply `cshtobash' to convert your normal interactive
949 environment, or as `cshtobash ~/.login' to convert your login
952 D5) How can I pipe standard output and standard error from one command to
953 another, like csh does with `|&'?
956 command 2>&1 | command2
958 The key is to remember that piping is performed before redirection, so
959 file descriptor 1 points to the pipe when it is duplicated onto file
962 D6) Now that I've converted from ksh to bash, are there equivalents to
963 ksh features like autoloaded functions and the `whence' command?
965 There are features in ksh-88 and ksh-93 that do not have direct bash
966 equivalents. Most, however, can be emulated with very little trouble.
968 ksh-88 feature Bash equivalent
969 -------------- ---------------
970 compiled-in aliases set up aliases in .bashrc; some ksh aliases are
971 bash builtins (hash, history, type)
972 coprocesses named pipe pairs (one for read, one for write)
973 typeset +f declare -F
974 cd, print, whence function substitutes in examples/functions/kshenv
975 autoloaded functions examples/functions/autoload is the same as typeset -fu
976 read var?prompt read -p prompt var
978 ksh-93 feature Bash equivalent
979 -------------- ---------------
980 sleep, getconf Bash has loadable versions in examples/loadables
981 ${.sh.version} $BASH_VERSION
986 Section E: How can I get bash to do certain things, and why does bash do
987 things the way it does?
989 E1) Why is the bash builtin `test' slightly different from /bin/test?
991 The specific example used here is [ ! x -o x ], which is false.
993 Bash's builtin `test' implements the Posix.2 spec, which can be
994 summarized as follows (the wording is due to David Korn):
996 Here is the set of rules for processing test arguments.
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
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.
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
1013 The operators -a and -o are considered binary operators for the purpose
1016 As you can see, the test becomes (not (x or x)), which is false.
1018 E2) Why does bash sometimes say `Broken pipe'?
1020 If a sequence of commands appears in a pipeline, and one of the
1021 reading commands finishes before the writer has finished, the
1022 writer receives a SIGPIPE signal. Many other shells special-case
1023 SIGPIPE as an exit status in the pipeline and do not report it.
1028 `head' can finish before `ps' writes all of its output, and ps
1029 will try to write on a pipe without a reader. In that case, bash
1030 will print `Broken pipe' to stderr when ps is killed by a
1033 You can build a version of bash that will not report SIGPIPE errors
1034 by uncommenting the definition of DONT_REPORT_SIGPIPE in the file
1037 E3) When I have terminal escape sequences in my prompt, why does bash
1038 wrap lines at the wrong column?
1040 Readline, the line editing library that bash uses, does not know
1041 that the terminal escape sequences do not take up space on the
1042 screen. The redisplay code assumes, unless told otherwise, that
1043 each character in the prompt is a `printable' character that
1044 takes up one character position on the screen.
1046 You can use the bash prompt expansion facility (see the PROMPTING
1047 section in the manual page) to tell readline that sequences of
1048 characters in the prompt strings take up no screen space.
1050 Use the \[ escape to begin a sequence of non-printing characters,
1051 and the \] escape to signal the end of such a sequence.
1053 E4) If I pipe the output of a command into `read variable', why doesn't
1054 the output show up in $variable when the read command finishes?
1056 This has to do with the parent-child relationship between Unix
1057 processes. It affects all commands run in pipelines, not just
1058 simple calls to `read'. For example, piping a command's output
1059 into a `while' loop that repeatedly calls `read' will result in
1062 Each element of a pipeline runs in a separate process, a child of
1063 the shell running the pipeline. A subprocess cannot affect its
1064 parent's environment. When the `read' command sets the variable
1065 to the input, that variable is set only in the subshell, not the
1066 parent shell. When the subshell exits, the value of the variable
1069 Many pipelines that end with `read variable' can be converted
1070 into command substitutions, which will capture the output of
1071 a specified command. The output can then be assigned to a
1074 grep ^gnu /usr/lib/news/active | wc -l | read ngroup
1076 can be converted into
1078 ngroup=$(grep ^gnu /usr/lib/news/active | wc -l)
1080 This does not, unfortunately, work to split the text among
1081 multiple variables, as read does when given multiple variable
1082 arguments. If you need to do this, you can either use the
1083 command substitution above to read the output into a variable
1084 and chop up the variable using the bash pattern removal
1085 expansion operators or use some variant of the following
1088 Say /usr/local/bin/ipaddr is the following shell script:
1091 host `hostname` | awk '/address/ {print $NF}'
1095 /usr/local/bin/ipaddr | read A B C D
1097 to break the local machine's IP address into separate octets, use
1101 set -- $(/usr/local/bin/ipaddr)
1103 A="$1" B="$2" C="$3" D="$4"
1105 Beware, however, that this will change the shell's positional
1106 parameters. If you need them, you should save them before doing
1109 This is the general approach -- in most cases you will not need to
1110 set $IFS to a different value.
1112 Some other user-supplied alternatives include:
1114 read A B C D << HERE
1115 $(IFS=.; echo $(/usr/local/bin/ipaddr))
1118 and, where process substitution is available,
1120 read A B C D < <(IFS=.; echo $(/usr/local/bin/ipaddr))
1122 E5) I have a bunch of shell scripts that use backslash-escaped characters
1123 in arguments to `echo'. Bash doesn't interpret these characters. Why
1124 not, and how can I make it understand them?
1126 This is the behavior of echo on most Unix System V machines.
1128 The bash builtin `echo' is modeled after the 9th Edition
1129 Research Unix version of `echo'. It does not interpret
1130 backslash-escaped characters in its argument strings by default;
1131 it requires the use of the -e option to enable the
1132 interpretation. The System V echo provides no way to disable the
1133 special characters; the bash echo has a -E option to disable
1136 There is a configuration option that will make bash behave like
1137 the System V echo and interpret things like `\t' by default. Run
1138 configure with the --enable-xpg-echo-default option to turn this
1139 on. Be aware that this will cause some of the tests run when you
1140 type `make tests' to fail.
1142 There is a shell option, `xpg_echo', settable with `shopt' that will
1143 change the behavior of echo at runtime. Enabling this option turns
1144 on expansion of backslash-escape sequences.
1146 E6) Why doesn't a while or for loop get suspended when I type ^Z?
1148 This is a consequence of how job control works on Unix. The only
1149 thing that can be suspended is the process group. This is a single
1150 command or pipeline of commands that the shell forks and executes.
1152 When you run a while or for loop, the only thing that the shell forks
1153 and executes are any commands in the while loop test and commands in
1154 the loop bodies. These, therefore, are the only things that can be
1155 suspended when you type ^Z.
1157 If you want to be able to stop the entire loop, you need to put it
1158 within parentheses, which will force the loop into a subshell that
1159 may be stopped (and subsequently restarted) as a single unit.
1161 E7) What about empty for loops in Makefiles?
1163 It's fairly common to see constructs like this in automatically-generated
1171 for d in ${SUBDIRS}; do \
1172 ( cd $$d && ${MAKE} ${MFLAGS} clean ) \
1175 When SUBDIRS is empty, this results in a command like this being passed to
1179 ( cd $d && ${MAKE} ${MFLAGS} clean )
1182 In versions of bash before bash-2.05a, this was a syntax error. If the
1183 reserved word `in' was present, a word must follow it before the semicolon
1184 or newline. The language in the manual page referring to the list of words
1185 being empty referred to the list after it is expanded. These versions of
1186 bash required that there be at least one word following the `in' when the
1187 construct was parsed.
1189 The idiomatic Makefile solution is something like:
1194 subdirs=$SUBDIRS ; for d in $$subdirs; do \
1195 ( cd $$d && ${MAKE} ${MFLAGS} clean ) \
1198 The latest drafts of the updated POSIX standard have changed this: the
1199 word list is no longer required. Bash versions 2.05a and later accept
1202 E8) Why does the arithmetic evaluation code complain about `08'?
1204 The bash arithmetic evaluation code (used for `let', $(()), (()), and in
1205 other places), interprets a leading `0' in numeric constants as denoting
1206 an octal number, and a leading `0x' as denoting hexadecimal. This is
1207 in accordance with the POSIX.2 spec, section 2.9.2.1, which states that
1208 arithmetic constants should be handled as signed long integers as defined
1209 by the ANSI/ISO C standard.
1211 The POSIX.2 interpretation committee has confirmed this:
1213 http://www.pasc.org/interps/unofficial/db/p1003.2/pasc-1003.2-173.html
1215 E9) Why does the pattern matching expression [A-Z]* match files beginning
1216 with every letter except `z'?
1218 Bash-2.05 and later versions have reverted to the bash-2.03 behavior of
1219 honoring the current locale setting when processing ranges within pattern
1220 matching bracket expressions ([A-Z]). This is what POSIX.2 and SUSv2/XPG5
1223 The behavior of the matcher in bash-2.05 and later versions depends on the
1224 current LC_COLLATE setting. Setting this variable to `C' or `POSIX' will
1225 result in the traditional behavior ([A-Z] matches all uppercase ASCII
1226 characters). Many other locales, including the en_US locale (the default
1227 on many US versions of Linux) collate the upper and lower case letters like
1232 which means that [A-Z] matches every letter except `z'.
1234 The portable way to specify upper case letters is [:upper:] instead of
1235 A-Z; lower case may be specified as [:lower:] instead of a-z.
1237 Look at the manual pages for setlocale(3), strcoll(3), and, if it is
1238 present, locale(1). If you have locale(1), you can use it to find
1239 your current locale information even if you do not have any of the
1246 into /etc/profile and inspect any shell scripts run from cron for
1247 constructs like [A-Z]. This will prevent things like
1251 from removing every file in the current directory except those beginning
1252 with `z' and still allow individual users to change the collation order.
1253 Users may put the above command into their own profiles as well, of course.
1255 Section F: Things to watch out for on certain Unix versions
1257 F1) Why can't I use command line editing in my `cmdtool'?
1259 The problem is `cmdtool' and bash fighting over the input. When
1260 scrolling is enabled in a cmdtool window, cmdtool puts the tty in
1261 `raw mode' to permit command-line editing using the mouse for
1262 applications that cannot do it themselves. As a result, bash and
1263 cmdtool each try to read keyboard input immediately, with neither
1264 getting enough of it to be useful.
1266 This mode also causes cmdtool to not implement many of the
1267 terminal functions and control sequences appearing in the
1268 `sun-cmd' termcap entry. For a more complete explanation, see
1269 that file examples/suncmd.termcap in the bash distribution.
1271 `xterm' is a better choice, and gets along with bash much more
1274 If you must use cmdtool, you can use the termcap description in
1275 examples/suncmd.termcap. Set the TERMCAP variable to the terminal
1276 description contained in that file, i.e.
1278 TERMCAP='Mu|sun-cmd:am:bs:km:pt:li#34:co#80:cl=^L:ce=\E[K:cd=\E[J:rs=\E[s:'
1280 Then export TERMCAP and start a new cmdtool window from that shell.
1281 The bash command-line editing should behave better in the new
1282 cmdtool. If this works, you can put the assignment to TERMCAP
1283 in your bashrc file.
1285 F2) I built bash on Solaris 2. Why do globbing expansions and filename
1286 completion chop off the first few characters of each filename?
1288 This is the consequence of building bash on SunOS 5 and linking
1289 with the libraries in /usr/ucblib, but using the definitions
1290 and structures from files in /usr/include.
1292 The 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').
1296 Make sure you've got /usr/ccs/bin ahead of /usr/ucb in your $PATH
1297 when configuring and building bash. This will ensure that you
1298 use /usr/ccs/bin/cc or acc instead of /usr/ucb/cc and that you
1299 link with libc before libucb.
1301 If you have installed the Sun C compiler, you may also need to
1302 put /usr/ccs/bin and /opt/SUNWspro/bin into your $PATH before
1305 F3) Why does bash dump core after I interrupt username completion or
1306 `~user' tilde expansion on a machine running NIS?
1308 This is a famous and long-standing bug in the SunOS YP (sorry, NIS)
1309 client library, which is part of libc.
1311 The YP library code keeps static state -- a pointer into the data
1312 returned from the server. When YP initializes itself (setpwent),
1313 it looks at this pointer and calls free on it if it's non-null.
1316 If one of the YP functions is interrupted during getpwent (the
1317 exact function is interpretwithsave()), and returns NULL, the
1318 pointer is freed without being reset to NULL, and the function
1319 returns. The next time getpwent is called, it sees that this
1320 pointer is non-null, calls free, and the bash free() blows up
1321 because it's being asked to free freed memory.
1323 The traditional Unix mallocs allow memory to be freed multiple
1324 times; that's probably why this has never been fixed. You can
1325 run configure with the `--without-gnu-malloc' option to use
1326 the C library malloc and avoid the problem.
1328 F4) I'm running SVR4.2. Why is the line erased every time I type `@'?
1330 The `@' character is the default `line kill' character in most
1331 versions of System V, including SVR4.2. You can change this
1332 character to whatever you want using `stty'. For example, to
1333 change the line kill character to control-u, type
1337 where the `^' and `U' can be two separate characters.
1339 F5) Why does bash report syntax errors when my C News scripts use a
1340 redirection before a subshell command?
1342 The actual command in question is something like
1346 According to the grammar given in the POSIX.2 standard, this construct
1347 is, in fact, a syntax error. Redirections may only precede `simple
1348 commands'. A subshell construct such as the above is one of the shell's
1349 `compound commands'. A redirection may only follow a compound command.
1351 This affects the mechanical transformation of commands that use `cat'
1352 to pipe a file into a command (a favorite Useless-Use-Of-Cat topic on
1353 comp.unix.shell). While most commands of the form
1357 can be converted to `< file command', shell control structures such as
1358 loops and subshells require `command < file'.
1360 The file CWRU/sh-redir-hack in the bash-2.05a distribution is an
1361 (unofficial) patch to parse.y that will modify the grammar to
1362 support this construct. It will not apply with `patch'; you must
1363 modify parse.y by hand. Note that if you apply this, you must
1364 recompile with -DREDIRECTION_HACK. This introduces a large
1365 number of reduce/reduce conflicts into the shell grammar.
1367 F6) Why can't I use vi-mode editing on Red Hat Linux 6.1?
1369 The short answer is that Red Hat screwed up.
1371 The long answer is that they shipped an /etc/inputrc that only works
1372 for emacs mode editing, and then screwed all the vi users by setting
1373 INPUTRC to /etc/inputrc in /etc/profile.
1375 The short fix is to do one of the following: remove or rename
1376 /etc/inputrc, set INPUTRC=~/.inputrc in ~/.bashrc (or .bash_profile,
1377 but make sure you export it if you do), remove the assignment to
1378 INPUTRC from /etc/profile, add
1382 to the beginning of /etc/inputrc, or bracket the key bindings in
1383 /etc/inputrc with these lines
1389 Section G: How can I get bash to do certain common things?
1391 G1) How can I get bash to read and display eight-bit characters?
1393 This is a process requiring several steps.
1395 First, you must ensure that the `physical' data path is a full eight
1396 bits. For xterms, for example, the `vt100' resources `eightBitInput'
1397 and `eightBitOutput' should be set to `true'.
1399 Once you have set up an eight-bit path, you must tell the kernel and
1400 tty driver to leave the eighth bit of characters alone when processing
1401 keyboard input. Use `stty' to do this:
1403 stty cs8 -istrip -parenb
1405 For old BSD-style systems, you can use
1413 Finally, you need to tell readline that you will be inputting and
1414 displaying eight-bit characters. You use readline variables to do
1415 this. These variables can be set in your .inputrc or using the bash
1416 `bind' builtin. Here's an example using `bind':
1418 bash$ bind 'set convert-meta off'
1419 bash$ bind 'set meta-flag on'
1420 bash$ bind 'set output-meta on'
1422 The `set' commands between the single quotes may also be placed
1425 G2) How do I write a function `x' to replace builtin command `x', but
1426 still invoke the command from within the function?
1428 This is why the `command' and `builtin' builtins exist. The
1429 `command' builtin executes the command supplied as its first
1430 argument, skipping over any function defined with that name. The
1431 `builtin' builtin executes the builtin command given as its first
1434 For example, to write a function to replace `cd' that writes the
1435 hostname and current directory to an xterm title bar, use
1436 something like the following:
1440 builtin cd "$@" && xtitle "$HOST: $PWD"
1443 This could also be written using `command' instead of `builtin';
1444 the version above is marginally more efficient.
1446 G3) How can I find the value of a shell variable whose name is the value
1447 of another shell variable?
1449 Versions of Bash newer than Bash-2.0 support this directly. You can use
1453 For example, the following sequence of commands will echo `z':
1459 For sh compatibility, use the `eval' builtin. The important
1460 thing to remember is that `eval' expands the arguments you give
1461 it again, so you need to quote the parts of the arguments that
1462 you want `eval' to act on.
1464 For example, this expression prints the value of the last positional
1467 eval echo \"\$\{$#\}\"
1469 The expansion of the quoted portions of this expression will be
1470 deferred until `eval' runs, while the `$#' will be expanded
1471 before `eval' is executed. In versions of bash later than bash-2.0,
1475 does the same thing.
1477 This is not the same thing as ksh93 `nameref' variables, though the syntax
1478 is similar. I may add namerefs in a future bash version.
1480 G4) 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?
1483 The bash command timing code looks for a variable `TIMEFORMAT' and
1484 uses its value as a format string to decide how to display the
1487 The value of TIMEFORMAT is a string with `%' escapes expanded in a
1488 fashion similar in spirit to printf(3). The manual page explains
1489 the meanings of the escape sequences in the format string.
1491 If TIMEFORMAT is not set, bash acts as if the following assignment had
1494 TIMEFORMAT=$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
1496 The POSIX.2 default time format (used by `time -p command') is
1498 TIMEFORMAT=$'real %2R\nuser %2U\nsys %2S'
1500 The BSD /usr/bin/time format can be emulated with:
1502 TIMEFORMAT=$'\t%1R real\t%1U user\t%1S sys'
1504 The System V /usr/bin/time format can be emulated with:
1506 TIMEFORMAT=$'\nreal\t%1R\nuser\t%1U\nsys\t%1S'
1508 The ksh format can be emulated with:
1510 TIMEFORMAT=$'\nreal\t%2lR\nuser\t%2lU\nsys\t%2lS'
1512 G5) How do I get the current directory into my prompt?
1514 Bash provides a number of backslash-escape sequences which are expanded
1515 when the prompt string (PS1 or PS2) is displayed. The full list is in
1518 The \w expansion gives the full pathname of the current directory, with
1519 a tilde (`~') substituted for the current value of $HOME. The \W
1520 expansion gives the basename of the current directory. To put the full
1521 pathname of the current directory into the path without any tilde
1522 subsitution, use $PWD. Here are some examples:
1524 PS1='\w$ ' # current directory with tilde
1525 PS1='\W$ ' # basename of current directory
1526 PS1='$PWD$ ' # full pathname of current directory
1528 The single quotes are important in the final example to prevent $PWD from
1529 being expanded when the assignment to PS1 is performed.
1531 G6) How can I rename "*.foo" to "*.bar"?
1533 Use the pattern removal functionality described in D3. The following `for'
1534 loop will do the trick:
1540 G7) How can I translate a filename from uppercase to lowercase?
1542 The script examples/functions/lowercase, originally written by John DuBois,
1543 will do the trick. The converse is left as an exercise.
1545 G8) How can I write a filename expansion (globbing) pattern that will match
1546 all files in the current directory except "." and ".."?
1548 You must have set the `extglob' shell option using `shopt -s extglob' to use
1553 A solution that works without extended globbing is given in the Unix Shell
1554 FAQ, posted periodically to comp.unix.shell.
1556 Section H: Where do I go from here?
1558 H1) How do I report bugs in bash, and where should I look for fixes and
1561 Use the `bashbug' script to report bugs. It is built and
1562 installed at the same time as bash. It provides a standard
1563 template for reporting a problem and automatically includes
1564 information about your configuration and build environment.
1566 `bashbug' sends its reports to bug-bash@gnu.org, which
1567 is a large mailing list gatewayed to the usenet newsgroup gnu.bash.bug.
1569 Bug fixes, answers to questions, and announcements of new releases
1570 are all posted to gnu.bash.bug. Discussions concerning bash features
1571 and problems also take place there.
1573 To reach the bash maintainers directly, send mail to
1574 bash-maintainers@gnu.org.
1576 H2) What kind of bash documentation is there?
1578 First, look in the doc directory in the bash distribution. It should
1579 contain at least the following files:
1581 bash.1 an extensive, thorough Unix-style manual page
1582 builtins.1 a manual page covering just bash builtin commands
1583 bashref.texi a reference manual in GNU tex`info format
1584 bashref.info an info version of the reference manual
1586 article.ms text of an article written for The Linux Journal
1587 readline.3 a man page describing readline
1589 Postscript, HTML, and ASCII files created from the above source are
1590 available in the documentation distribution.
1592 There is additional documentation available for anonymous FTP from host
1593 ftp.cwru.edu in the `pub/bash' directory.
1595 Cameron Newham and Bill Rosenblatt have written a book on bash, published
1596 by O'Reilly and Associates. The book is based on Bill Rosenblatt's Korn
1597 Shell book. The title is ``Learning the Bash Shell'', and the ISBN number
1598 is 1-56592-147-X. Look for it in fine bookstores near you. This book
1599 covers bash-1.14, but has an appendix describing some of the new features
1602 A second edition of this book is available, published in January, 1998.
1603 The ISBN number is 1-56592-347-2. Look for it in the same fine bookstores
1606 H3) What's coming in future versions?
1608 These are features I hope to include in a future version of bash.
1610 a better bash debugger (a minimally-tested version is included with bash-2.05a)
1612 changes to the DEBUG trap to be compatible with ksh93 (which runs the
1613 trap before each simple command, instead of after each one like previous
1615 co-processes, but with a new-style syntax that looks like function declaration
1617 H4) What's on the bash `wish list' for future versions?
1619 These are features that may or may not appear in a future version of bash.
1621 breaking some of the shell functionality into embeddable libraries
1622 a module system like zsh's, using dynamic loading like builtins
1623 better internationalization using GNU `gettext'
1624 an option to use external files for the long `help' text
1625 date-stamped command history
1626 a bash programmer's guide with a chapter on creating loadable builtins
1627 a better loadable interface to perl with access to the shell builtins and
1628 variables (contributions gratefully accepted)
1629 ksh93-like `nameref' variables
1630 ksh93-like `+=' variable assignment operator
1631 ksh93-like `xx.yy' variables (including some of the .sh.* variables) and
1632 associated disipline functions
1633 Some of the new ksh93 pattern matching operators, like backreferencing
1635 H5) When will the next release appear?
1637 The next version will appear sometime in 2002. Never make predictions.
1640 This document is Copyright 1995-2001 by Chester Ramey.
1642 Permission is hereby granted, without written agreement and
1643 without license or royalty fees, to use, copy, and distribute
1644 this document for any purpose, provided that the above copyright
1645 notice appears in all copies of this document and that the
1646 contents of this document remain unaltered.