| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 1 | This is the Bash FAQ, version 3.17, for Bash version 2.05a. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2 | |
| 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 |
| 6 | programming. |
| 7 | |
| 8 | Another good source of basic information about shells is the collection |
| 9 | of FAQ articles periodically posted to comp.unix.shell. |
| 10 | |
| 11 | Questions and comments concerning this document should be sent to |
| 12 | chet@po.cwru.edu. |
| 13 | |
| 14 | This document is available for anonymous FTP with the URL |
| 15 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 16 | ftp://ftp.cwru.edu/pub/bash/FAQ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 17 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 18 | The Bash home page is http://cnswww.cns.cwru.edu/~chet/bash/bashtop.html |
| 19 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 20 | ---------- |
| 21 | Contents: |
| 22 | |
| 23 | Section A: The Basics |
| 24 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 25 | A1) What is it? |
| 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 |
| 33 | machine. Why not? |
| 34 | A9) What's the `POSIX 1003.2 standard'? |
| 35 | A10) What is the bash `posix mode'? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 36 | |
| 37 | Section B: The latest version |
| 38 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 39 | B1) What's new in version 2.05a? |
| 40 | B2) Are there any user-visible incompatibilities between bash-2.05a and |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 41 | bash-1.14.7? |
| 42 | |
| 43 | Section C: Differences from other Unix shells |
| 44 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 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? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 48 | |
| 49 | Section D: Why does bash do some things differently than other Unix shells? |
| 50 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 51 | D1) Why does bash run a different version of `command' than |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 52 | `which command' says it will? |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 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 |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 57 | another, like csh does with `|&'? |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 58 | D6) Now that I've converted from ksh to bash, are there equivalents to |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 59 | ksh features like autoloaded functions and the `whence' command? |
| 60 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 61 | Section E: Why does bash do certain things the way it does? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 62 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 63 | E1) Why is the bash builtin `test' slightly different from /bin/test? |
| 64 | E2) Why does bash sometimes say `Broken pipe'? |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 65 | E3) When I have terminal escape sequences in my prompt, why does bash |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 66 | wrap lines at the wrong column? |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 67 | E4) If I pipe the output of a command into `read variable', why doesn't |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 68 | the output show up in $variable when the read command finishes? |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 69 | E5) I have a bunch of shell scripts that use backslash-escaped characters |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 70 | in arguments to `echo'. Bash doesn't interpret these characters. Why |
| 71 | not, and how can I make it understand them? |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 72 | E6) Why doesn't a while or for loop get suspended when I type ^Z? |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 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'? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 77 | |
| 78 | Section F: Things to watch out for on certain Unix versions |
| 79 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 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 |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 82 | completion chop off the first few characters of each filename? |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 83 | F3) Why does bash dump core after I interrupt username completion or |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 84 | `~user' tilde expansion on a machine running NIS? |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 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 |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 87 | redirection before a subshell command? |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 88 | F6) Why can't I use vi-mode editing on Red Hat Linux 6.1? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 89 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 90 | Section G: How can I get bash to do certain common things? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 91 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 ".."? |
| 104 | |
| 105 | Section H: Where do I go from here? |
| 106 | |
| 107 | H1) How do I report bugs in bash, and where should I look for fixes and |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 108 | advice? |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 113 | |
| 114 | ---------- |
| 115 | Section A: The Basics |
| 116 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 117 | A1) What is it? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 118 | |
| 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 |
| 121 | shells. |
| 122 | |
| 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 |
| 129 | shell behavior. |
| 130 | |
| 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. |
| 134 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 135 | A2) What's the latest version? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 136 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 137 | The latest version is 2.05a, first made available on Thursday, 15 |
| 138 | November, 2001. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 139 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 140 | A3) Where can I get it? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 141 | |
| 142 | Bash is the GNU project's shell, and so is available from the |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 143 | master GNU archive site, ftp.gnu.org, and its mirrors. The |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 144 | latest version is also available for FTP from ftp.cwru.edu. |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 145 | The following URLs tell how to get version 2.05a: |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 146 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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 |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 149 | |
| 150 | Formatted versions of the documentation are available with the URLs: |
| 151 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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 |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 154 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 155 | A4) On what machines will bash run? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 156 | |
| 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. |
| 162 | |
| 163 | More information appears in the file `INSTALL' in the distribution. |
| 164 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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. |
| 168 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 169 | A5) Will bash run on operating systems other than Unix? |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 170 | |
| 171 | Configuration specifics for Unix-like systems such as QNX and |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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. |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 176 | |
| 177 | Bash has been ported to versions of Windows implementing the Win32 |
| 178 | programming interface. This includes Windows 95 and Windows NT. |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 179 | The port was done by Cygnus Solutions as part of their CYGWIN |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 180 | project. For more information about the project, look at the URLs |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 181 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 182 | http://www.cygwin.com/ |
| 183 | http://sourceware.cygnus.com/cygwin |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 184 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 185 | Cygnus originally ported bash-1.14.7, and that port was part of their |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 186 | early GNU-Win32 (the original name) releases. Cygnus has also done a |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 187 | port of bash-2.05 to the CYGWIN environment, and it is available as |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 188 | part of their current release. |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 189 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 190 | Bash-2.05a should require no local Cygnus changes to build and run under |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 191 | CYGWIN. |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 192 | |
| 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 |
| 195 | |
| 196 | ftp://ftp.gnustep.org//pub/win32/bash-alpha-nt-1.01.tar.gz |
| 197 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 198 | DJ Delorie has a port of bash-2.x which runs under MS-DOS, as part |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 199 | of the DJGPP project. For more information on the project, see |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 200 | |
| 201 | http://www.delorie.com/djgpp/ |
| 202 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 203 | I have been told that the original DJGPP port was done by Daisuke Aoyama. |
| 204 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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: |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 207 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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 |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 211 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 212 | Mark has begun to work with bash-2.05, but I don't know the status. |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 213 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 214 | Ports of bash-1.12 and bash-2.0 are available for OS/2 from |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 215 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 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 |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 218 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 219 | I haven't looked at either, but the second appears to be a binary-only |
| 220 | distribution. Beware. |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 221 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 |
| 224 | BeOS. |
| 225 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 226 | A6) How can I build bash with gcc? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 227 | |
| 228 | Bash configures to use gcc by default if it is available. Read the |
| 229 | file INSTALL in the distribution for more information. |
| 230 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 231 | A7) How can I make bash my login shell? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 232 | |
| 233 | Some machines let you use `chsh' to change your login shell. Other |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 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. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 239 | |
| 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 |
| 243 | bash. |
| 244 | |
| 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: |
| 247 | |
| 248 | if ( -f /usr/gnu/bin/bash ) exec /usr/gnu/bin/bash --login |
| 249 | |
| 250 | (the `--login' tells bash that it is a login shell). |
| 251 | |
| 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 |
| 255 | like |
| 256 | |
| 257 | if ( $?prompt ) exec /usr/gnu/bin/bash --login |
| 258 | |
| 259 | to ensure that bash is exec'd only when the csh is interactive. |
| 260 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 261 | If your login shell is sh or ksh, you have to do two things. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 262 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 263 | First, create an empty file in your home directory named `.bash_profile'. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 266 | is the first file bash tries to read initialization commands from when |
| 267 | it is invoked as a login shell. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 268 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 269 | Next, add a line similar to the above to ~/.profile: |
| 270 | |
| 271 | [ -f /usr/gnu/bin/bash ] && exec /usr/gnu/bin/bash --login |
| 272 | |
| 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. |
| 276 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 |
| 279 | slightly different. |
| 280 | |
| 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, |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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. |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 287 | |
| 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. |
| 291 | |
| 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. |
| 296 | |
| 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: |
| 299 | |
| 300 | if [ -n "$DT" ]; then |
| 301 | [ -f /usr/gnu/bin/bash ] && exec /usr/gnu/bin/bash --login |
| 302 | fi |
| 303 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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 |
| 307 | ~/.dtprofile: |
| 308 | |
| 309 | BASH_ENV=${HOME}/.bash_profile ; export BASH_ENV |
| 310 | |
| 311 | and add the following line to the beginning of ~/.bash_profile: |
| 312 | |
| 313 | unset BASH_ENV |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 314 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 315 | A8) I just changed my login shell to bash, and now I can't FTP into my |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 316 | machine. Why not? |
| 317 | |
| 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. |
| 321 | |
| 322 | Most versions of ftpd use this file to prohibit `special' users |
| 323 | such as `uucp' and `news' from using FTP. |
| 324 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 325 | A9) What's the `POSIX 1003.2 standard'? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 326 | |
| 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. |
| 334 | |
| 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. |
| 341 | |
| 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. |
| 347 | |
| 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 |
| 358 | objections. |
| 359 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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 |
| 363 | |
| 364 | http://www.opengroup.org/onlinepubs/007908799/ |
| 365 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 366 | A10) What is the bash `posix mode'? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 367 | |
| 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. |
| 372 | |
| 373 | Posix mode is entered by starting bash with the --posix option or |
| 374 | executing `set -o posix' after bash is running. |
| 375 | |
| 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 |
| 379 | Reference Manual. |
| 380 | |
| 381 | Section B: The latest version |
| 382 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 383 | B1) What's new in version 2.05a? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 384 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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. |
| 391 | |
| 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 |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 394 | distribution): |
| 395 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 396 | o The `printf' builtin has undergone major work |
| 397 | |
| 398 | o There is a new read-only `shopt' option: login_shell, which is set by |
| 399 | login shells and unset otherwise |
| 400 | |
| 401 | o New `\A' prompt string escape sequence; expanding to time in 24-hour |
| 402 | HH:MM format |
| 403 | |
| 404 | o New `-A group/-g' option to complete and compgen; goes group name |
| 405 | completion |
| 406 | |
| 407 | o New [+-]O invocation option to set and unset `shopt' options at startup |
| 408 | |
| 409 | o ksh-like `ERR' trap |
| 410 | |
| 411 | o `for' loops now allow empty word lists after the `in' reserved word |
| 412 | |
| 413 | o new `hard' and `soft' arguments for the `ulimit' builtin |
| 414 | |
| 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 |
| 417 | |
| 418 | o Readline can be configured to skip `hidden' files (filenames with a leading |
| 419 | `.' on Unix) when performing completion |
| 420 | |
| 421 | A short feature history dating from bash-2.0: |
| 422 | |
| 423 | Bash-2.05 introduced the following new features: |
| 424 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 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 |
| 430 | port numbers. |
| 431 | o `complete' and `compgen' now take a `-o value' option, which controls some |
| 432 | of the aspects of that compspec. Valid values are: |
| 433 | |
| 434 | default - perform bash default completion if programmable |
| 435 | completion produces no matches |
| 436 | dirnames - perform directory name completion if programmable |
| 437 | completion produces no matches |
| 438 | filenames - tell readline that the compspec produces filenames, |
| 439 | so it can do things like append slashes to |
| 440 | directory names and suppress trailing spaces |
| 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. |
| 447 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 448 | Bash-2.04 introduced the following new features: |
| 449 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 455 | o The `no_empty_cmd_completion' shell option, if enabled, inhibits |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 |
| 469 | function |
| 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 |
| 474 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 475 | The version of Readline released with Bash-2.04, Readline-4.1, had several |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 476 | new features as well: |
| 477 | |
| 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 |
| 485 | |
| 486 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 487 | Bash-2.03 had very few new features, in keeping with the convention |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 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 |
| 490 | folks. |
| 491 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 492 | A new shopt option, `restricted_shell', so that startup files can test |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 493 | whether or not the shell was started in restricted mode |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 494 | Filename generation is now performed on the words between ( and ) in |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 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 |
| 502 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 503 | There were lots of changes to the version of the Readline library released |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 504 | along with Bash-2.03. For a complete list of the changes, read the file |
| 505 | CHANGES in the Bash-2.03 distribution. |
| 506 | |
| 507 | Bash-2.02 contained the following new features: |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 508 | |
| 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 |
| 521 | $(cat filename) |
| 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) |
| 528 | |
| 529 | Bash-2.01 contained only a few new features: |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 530 | |
| 531 | new `GROUPS' builtin array variable containing the user's group list |
| 532 | new bindable readline commands: history-and-alias-expand-line and |
| 533 | alias-expand-line |
| 534 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 535 | Bash-2.0 contained extensive changes and new features from bash-1.14.7. |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 536 | Here's a short list: |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 537 | |
| 538 | new `time' reserved word to time pipelines, shell builtins, and |
| 539 | shell functions |
| 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 |
| 544 | string translation |
| 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 |
| 560 | new DEBUG trap |
| 561 | expanded (and now documented) restricted shell mode |
| 562 | |
| 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 |
| 568 | (for consistency) |
| 569 | grammar tighter and smaller (66 reduce-reduce conflicts gone) |
| 570 | lots of code now smaller and faster |
| 571 | test suite greatly expanded |
| 572 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 573 | B2) Are there any user-visible incompatibilities between bash-2.05a and |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 574 | bash-1.14.7? |
| 575 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 580 | |
| 581 | Section C: Differences from other Unix shells |
| 582 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 583 | C1) How does bash differ from sh, the Bourne shell? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 584 | |
| 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 |
| 587 | completely. |
| 588 | |
| 589 | Things bash has that sh does not: |
| 590 | long invocation options |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 591 | [+-]O invocation option |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 592 | `!' reserved word to invert pipeline return value |
| 593 | `time' reserved word to time pipelines and shell builtins |
| 594 | the `function' reserved word |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 595 | the `select' compound command and reserved word |
| 596 | arithmetic for command: for ((expr1 ; expr2; expr3 )); do list; done |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 597 | new $'...' and $"..." quoting |
| 598 | the $(...) form of command substitution |
| Jari Aalto | bc4cd23 | 1998-07-23 14:37:54 +0000 | [diff] [blame] | 599 | the $(<filename) form of command substitution, equivalent to |
| 600 | $(cat filename) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 601 | the ${#param} parameter value length operator |
| 602 | the ${!param} indirect parameter expansion operator |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 603 | the ${!param*} prefix expansion operator |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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, |
| Jari Aalto | bc4cd23 | 1998-07-23 14:37:54 +0000 | [diff] [blame] | 612 | HISTFILESIZE, HISTCONTROL, HISTIGNORE, GLOBIGNORE, GROUPS, |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 613 | PROMPT_COMMAND, FCEDIT, FIGNORE, IGNOREEOF, INPUTRC, |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 614 | SHELLOPTS, OPTERR, HOSTFILE, TMOUT, FUNCNAME, histchars, |
| 615 | auto_resume |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 616 | DEBUG trap |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 617 | ERR trap |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 618 | variable arrays with new compound assignment syntax |
| 619 | redirections: <>, &>, >| |
| 620 | prompt string special char translation and variable expansion |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 621 | auto-export of variables in initial environment |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 622 | command search finds functions before builtins |
| 623 | bash return builtin will exit a file sourced with `.' |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 624 | builtins: cd -/-L/-P, exec -l/-c/-a, echo -e/-E, hash -p/-t. |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 625 | export -n/-f/-p/name=value, pwd -L/-P, |
| 626 | read -e/-p/-a/-t/-n/-d/-s, |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 |
| 635 | brace expansion |
| 636 | tilde expansion |
| 637 | arithmetic expansion with $((...)) and `let' builtin |
| Jari Aalto | bc4cd23 | 1998-07-23 14:37:54 +0000 | [diff] [blame] | 638 | the `[[...]]' extended conditional command |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 639 | process substitution |
| 640 | aliases and alias/unalias builtins |
| 641 | local variables in functions and `local' builtin |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 642 | readline and command-line editing with programmable completion |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 643 | command history and history/fc builtins |
| 644 | csh-like history expansion |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 645 | other new bash builtins: bind, command, compgen, complete, builtin, |
| 646 | declare/typeset, dirs, enable, fc, help, |
| 647 | history, logout, popd, pushd, disown, shopt, |
| 648 | printf |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 649 | exported functions |
| 650 | filename generation when using output redirection (command >a*) |
| Jari Aalto | bc4cd23 | 1998-07-23 14:37:54 +0000 | [diff] [blame] | 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 |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 656 | variable assignments preceding commands affect only that command, |
| 657 | even for builtins and functions |
| 658 | posix mode |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 659 | redirection to /dev/fd/N, /dev/stdin, /dev/stdout, /dev/stderr, |
| 660 | /dev/tcp/host/port, /dev/udp/host/port |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 661 | |
| 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') |
| 665 | `newgrp' builtin |
| 666 | turns on job control if called as `jsh' |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 667 | $TIMEOUT (like bash $TMOUT) |
| 668 | `^' is a synonym for `|' |
| 669 | new SVR4.2 sh builtins: mldmode, priv |
| 670 | |
| 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 |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 682 | to open a file named `-v', and, on SunOS 4.1.4, dumps core. |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 683 | On Solaris 2.4 and earlier versions, sh goes into an infinite |
| 684 | loop.) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 685 | sh exits a script if any builtin fails; bash exits only if one of |
| 686 | the POSIX.2 `special' builtins fails |
| 687 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 688 | C2) How does bash differ from the Korn shell, version ksh88? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 689 | |
| 690 | Things bash has or uses that ksh88 does not: |
| 691 | long invocation options |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 692 | [-+]O invocation option |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 693 | `!' reserved word |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 694 | arithmetic for command: for ((expr1 ; expr2; expr3 )); do list; done |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 695 | posix mode and posix conformance |
| 696 | command hashing |
| 697 | tilde expansion for assignment statements that look like $PATH |
| 698 | process substitution with named pipes if /dev/fd is not available |
| 699 | the ${!param} indirect parameter expansion operator |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 700 | the ${!param*} prefix expansion operator |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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, |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 708 | GROUPS, FUNCNAME, histchars, auto_resume |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 709 | prompt expansion with backslash escapes and command substitution |
| 710 | redirection: &> (stdout and stderr) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 711 | more extensive and extensible editing and programmable completion |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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, |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 721 | `!' csh-style history expansion |
| Jari Aalto | bc4cd23 | 1998-07-23 14:37:54 +0000 | [diff] [blame] | 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 |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 728 | redirection to /dev/fd/N, /dev/stdin, /dev/stdout, /dev/stderr |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 729 | arrays of unlimited size |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 730 | |
| 731 | Things ksh88 has or uses that bash does not: |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 732 | tracked aliases (alias -t) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 733 | variables: ERRNO, FPATH, EDITOR, VISUAL |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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, |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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 |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 745 | |
| 746 | Implementation differences: |
| 747 | ksh runs last command of a pipeline in parent shell context |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 752 | bash waits for all commands in pipeline to exit before returning status |
| 753 | emacs-mode editing has some slightly different key bindings |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 754 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 755 | C3) Which new features in ksh-93 are not in bash, and which are? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 756 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 757 | New things in ksh-93 not in bash-2.05a: |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 758 | associative arrays |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 759 | floating point arithmetic and variables |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 760 | math library functions |
| 761 | ${!name[sub]} name of subscript for associative array |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 762 | `.' is allowed in variable names to create a hierarchical namespace |
| 763 | more extensive compound assignment syntax |
| 764 | discipline functions |
| 765 | `sleep' and `getconf' builtins (bash has loadable versions) |
| 766 | typeset -n and `nameref' variables |
| 767 | KEYBD trap |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 768 | variables: .sh.edchar, .sh.edmode, .sh.edcol, .sh.edtext, .sh.version, |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 769 | .sh.name, .sh.subscript, .sh.value, .sh.match, HISTEDIT |
| 770 | backreferences in pattern matching (\N) |
| 771 | `&' operator in pattern lists for matching |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 772 | print -f (bash uses printf) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 773 | `fc' has been renamed to `hist' |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 774 | `.' can execute shell functions |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 775 | exit statuses between 0 and 255 |
| 776 | set -o pipefail |
| 777 | `+=' variable assignment operator |
| 778 | TMOUT is default timeout for `read' and `select' |
| 779 | <&N- and >&N- redirections (combination dup and close) |
| 780 | FPATH and PATH mixing |
| 781 | getopts -a |
| 782 | -I invocation option |
| 783 | DEBUG trap now executed before each simple command, instead of after |
| 784 | printf %H, %P, %T, %Z modifiers, output base for %d |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 785 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 786 | New things in ksh-93 present in bash-2.05a: |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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]}, |
| 790 | ${!param*} |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 |
| 797 | set -o notify/-C |
| 798 | changes to kill builtin |
| 799 | read -A (bash uses read -a) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 800 | read -t/-d |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 801 | trap -p |
| 802 | exec -c/-a |
| 803 | `.' restores the positional parameters when it completes |
| 804 | POSIX.2 `test' |
| 805 | umask -S |
| 806 | unalias -a |
| 807 | command and arithmetic substitution performed on PS1, PS4, and ENV |
| 808 | command name completion |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 809 | ENV processed only for interactive shells |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 810 | |
| 811 | Section D: Why does bash do some things differently than other Unix shells? |
| 812 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 813 | D1) Why does bash run a different version of `command' than |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 814 | `which command' says it will? |
| 815 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 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. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 820 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 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: |
| 829 | |
| 830 | which() |
| 831 | { |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 832 | builtin type "$@" |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | If you're moving from tcsh and would like to bring `where' along |
| 836 | as well, use this function: |
| 837 | |
| 838 | where() |
| 839 | { |
| 840 | builtin type -a "$@" |
| 841 | } |
| 842 | |
| 843 | D2) Why doesn't bash treat brace expansions exactly like csh? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 844 | |
| 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 |
| 850 | compatibility. |
| 851 | |
| 852 | Bash, ksh, zsh, and pd-ksh all implement brace expansion this way. |
| 853 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 854 | D3) Why doesn't bash have csh variable modifiers? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 855 | |
| 856 | Posix has specified a more powerful, albeit somewhat more cryptic, |
| 857 | mechanism cribbed from ksh, and bash implements it. |
| 858 | |
| 859 | ${parameter%word} |
| 860 | Remove smallest suffix pattern. The WORD is expanded to produce |
| 861 | a pattern. It then expands to the value of PARAMETER, with the |
| 862 | smallest portion of the suffix matched by the pattern deleted. |
| 863 | |
| 864 | x=file.c |
| 865 | echo ${x%.c}.o |
| 866 | -->file.o |
| 867 | |
| 868 | ${parameter%%word} |
| 869 | |
| 870 | Remove largest suffix pattern. The WORD is expanded to produce |
| 871 | a pattern. It then expands to the value of PARAMETER, with the |
| 872 | largest portion of the suffix matched by the pattern deleted. |
| 873 | |
| 874 | x=posix/src/std |
| 875 | echo ${x%%/*} |
| 876 | -->posix |
| 877 | |
| 878 | ${parameter#word} |
| 879 | Remove smallest prefix pattern. The WORD is expanded to produce |
| 880 | a pattern. It then expands to the value of PARAMETER, with the |
| 881 | smallest portion of the prefix matched by the pattern deleted. |
| 882 | |
| 883 | x=$HOME/src/cmd |
| 884 | echo ${x#$HOME} |
| 885 | -->/src/cmd |
| 886 | |
| 887 | ${parameter##word} |
| 888 | Remove largest prefix pattern. The WORD is expanded to produce |
| 889 | a pattern. It then expands to the value of PARAMETER, with the |
| 890 | largest portion of the prefix matched by the pattern deleted. |
| 891 | |
| 892 | x=/one/two/three |
| 893 | echo ${x##*/} |
| 894 | -->three |
| 895 | |
| 896 | |
| 897 | Given |
| 898 | a=/a/b/c/d |
| 899 | b=b.xxx |
| 900 | |
| 901 | csh bash result |
| 902 | --- ---- ------ |
| 903 | $a:h ${a%/*} /a/b/c |
| 904 | $a:t ${a##*/} d |
| 905 | $b:r ${b%.*} b |
| 906 | $b:e ${b##*.} xxx |
| 907 | |
| 908 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 909 | D4) How can I make my csh aliases work when I convert to bash? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 910 | |
| 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; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 914 | this script can be found in ./examples/misc/aliasconv.sh. Here is |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 915 | how you use it: |
| 916 | |
| 917 | Start csh in the normal way for you. (e.g., `csh') |
| 918 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 919 | Pipe the output of `alias' through `aliasconv.sh', saving the |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 920 | results into `bash_aliases': |
| 921 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 922 | alias | bash aliasconv.sh >bash_aliases |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 923 | |
| 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 |
| 929 | expansion. |
| 930 | |
| 931 | For example, the csh alias: |
| 932 | |
| 933 | alias cd 'cd \!*; echo $cwd' |
| 934 | |
| 935 | is converted to the bash function: |
| 936 | |
| 937 | cd () { command cd "$@"; echo $PWD ; } |
| 938 | |
| 939 | The only thing that needs to be done is to quote $PWD: |
| 940 | |
| 941 | cd () { command cd "$@"; echo "$PWD" ; } |
| 942 | |
| 943 | Merge the edited file into your ~/.bashrc. |
| 944 | |
| 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 |
| 950 | environment. |
| 951 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 952 | D5) How can I pipe standard output and standard error from one command to |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 953 | another, like csh does with `|&'? |
| 954 | |
| 955 | Use |
| 956 | command 2>&1 | command2 |
| 957 | |
| 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 |
| 960 | descriptor 2. |
| 961 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 962 | D6) Now that I've converted from ksh to bash, are there equivalents to |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 963 | ksh features like autoloaded functions and the `whence' command? |
| 964 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 967 | |
| 968 | ksh-88 feature Bash equivalent |
| 969 | -------------- --------------- |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 970 | compiled-in aliases set up aliases in .bashrc; some ksh aliases are |
| 971 | bash builtins (hash, history, type) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 |
| 977 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 978 | ksh-93 feature Bash equivalent |
| 979 | -------------- --------------- |
| 980 | sleep, getconf Bash has loadable versions in examples/loadables |
| 981 | ${.sh.version} $BASH_VERSION |
| 982 | print -f printf |
| 983 | hist alias fc=hist |
| 984 | $HISTEDIT $FCEDIT |
| 985 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 986 | Section E: How can I get bash to do certain things, and why does bash do |
| 987 | things the way it does? |
| 988 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 989 | E1) Why is the bash builtin `test' slightly different from /bin/test? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 990 | |
| 991 | The specific example used here is [ ! x -o x ], which is false. |
| 992 | |
| 993 | Bash's builtin `test' implements the Posix.2 spec, which can be |
| 994 | summarized as follows (the wording is due to David Korn): |
| 995 | |
| 996 | Here is the set of rules for processing test arguments. |
| 997 | |
| 998 | 0 Args: False |
| 999 | 1 Arg: True iff argument is not null. |
| 1000 | 2 Args: If first arg is !, True iff second argument is null. |
| 1001 | If first argument is unary, then true if unary test is true |
| 1002 | Otherwise error. |
| 1003 | 3 Args: If second argument is a binary operator, do binary test of $1 $3 |
| 1004 | If first argument is !, negate two argument test of $2 $3 |
| 1005 | If first argument is `(' and third argument is `)', do the |
| 1006 | one-argument test of the second argument. |
| 1007 | Otherwise error. |
| 1008 | 4 Args: If first argument is !, negate three argument test of $2 $3 $4. |
| 1009 | Otherwise unspecified |
| 1010 | 5 or more Args: unspecified. (Historical shells would use their |
| 1011 | current algorithm). |
| 1012 | |
| 1013 | The operators -a and -o are considered binary operators for the purpose |
| 1014 | of the 3 Arg case. |
| 1015 | |
| 1016 | As you can see, the test becomes (not (x or x)), which is false. |
| 1017 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1018 | E2) Why does bash sometimes say `Broken pipe'? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1019 | |
| 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. |
| 1024 | For example, in: |
| 1025 | |
| 1026 | ps -aux | head |
| 1027 | |
| 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 |
| 1031 | SIGPIPE. |
| 1032 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 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 |
| 1035 | config-top.h. |
| 1036 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1037 | E3) When I have terminal escape sequences in my prompt, why does bash |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1038 | wrap lines at the wrong column? |
| 1039 | |
| 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. |
| 1045 | |
| 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. |
| 1049 | |
| 1050 | Use the \[ escape to begin a sequence of non-printing characters, |
| 1051 | and the \] escape to signal the end of such a sequence. |
| 1052 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1053 | E4) If I pipe the output of a command into `read variable', why doesn't |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1054 | the output show up in $variable when the read command finishes? |
| 1055 | |
| 1056 | This has to do with the parent-child relationship between Unix |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 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 |
| 1060 | the same behavior. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1061 | |
| 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 |
| 1067 | is lost. |
| 1068 | |
| 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 |
| 1072 | variable: |
| 1073 | |
| 1074 | grep ^gnu /usr/lib/news/active | wc -l | read ngroup |
| 1075 | |
| 1076 | can be converted into |
| 1077 | |
| 1078 | ngroup=$(grep ^gnu /usr/lib/news/active | wc -l) |
| 1079 | |
| 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 |
| 1086 | approach. |
| 1087 | |
| 1088 | Say /usr/local/bin/ipaddr is the following shell script: |
| 1089 | |
| 1090 | #! /bin/sh |
| 1091 | host `hostname` | awk '/address/ {print $NF}' |
| 1092 | |
| 1093 | Instead of using |
| 1094 | |
| 1095 | /usr/local/bin/ipaddr | read A B C D |
| 1096 | |
| 1097 | to break the local machine's IP address into separate octets, use |
| 1098 | |
| 1099 | OIFS="$IFS" |
| 1100 | IFS=. |
| 1101 | set -- $(/usr/local/bin/ipaddr) |
| 1102 | IFS="$OIFS" |
| 1103 | A="$1" B="$2" C="$3" D="$4" |
| 1104 | |
| 1105 | Beware, however, that this will change the shell's positional |
| 1106 | parameters. If you need them, you should save them before doing |
| 1107 | this. |
| 1108 | |
| 1109 | This is the general approach -- in most cases you will not need to |
| 1110 | set $IFS to a different value. |
| 1111 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 1112 | Some other user-supplied alternatives include: |
| 1113 | |
| 1114 | read A B C D << HERE |
| 1115 | $(IFS=.; echo $(/usr/local/bin/ipaddr)) |
| 1116 | HERE |
| 1117 | |
| 1118 | and, where process substitution is available, |
| 1119 | |
| 1120 | read A B C D < <(IFS=.; echo $(/usr/local/bin/ipaddr)) |
| 1121 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1122 | E5) I have a bunch of shell scripts that use backslash-escaped characters |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1123 | in arguments to `echo'. Bash doesn't interpret these characters. Why |
| 1124 | not, and how can I make it understand them? |
| 1125 | |
| 1126 | This is the behavior of echo on most Unix System V machines. |
| 1127 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1128 | The bash builtin `echo' is modeled after the 9th Edition |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 |
| 1134 | them. |
| 1135 | |
| 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 |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 1138 | configure with the --enable-xpg-echo-default option to turn this |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1139 | on. Be aware that this will cause some of the tests run when you |
| 1140 | type `make tests' to fail. |
| 1141 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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. |
| 1145 | |
| 1146 | E6) Why doesn't a while or for loop get suspended when I type ^Z? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1147 | |
| 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. |
| 1151 | |
| 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. |
| 1156 | |
| 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. |
| 1160 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1161 | E7) What about empty for loops in Makefiles? |
| 1162 | |
| 1163 | It's fairly common to see constructs like this in automatically-generated |
| 1164 | Makefiles: |
| 1165 | |
| 1166 | SUBDIRS = @SUBDIRS@ |
| 1167 | |
| 1168 | ... |
| 1169 | |
| 1170 | subdirs-clean: |
| 1171 | for d in ${SUBDIRS}; do \ |
| 1172 | ( cd $$d && ${MAKE} ${MFLAGS} clean ) \ |
| 1173 | done |
| 1174 | |
| 1175 | When SUBDIRS is empty, this results in a command like this being passed to |
| 1176 | bash: |
| 1177 | |
| 1178 | for d in ; do |
| 1179 | ( cd $d && ${MAKE} ${MFLAGS} clean ) |
| 1180 | done |
| 1181 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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. |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1188 | |
| 1189 | The idiomatic Makefile solution is something like: |
| 1190 | |
| 1191 | SUBDIRS = @SUBDIRS@ |
| 1192 | |
| 1193 | subdirs-clean: |
| 1194 | subdirs=$SUBDIRS ; for d in $$subdirs; do \ |
| 1195 | ( cd $$d && ${MAKE} ${MFLAGS} clean ) \ |
| 1196 | done |
| 1197 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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 |
| 1200 | the new syntax. |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1201 | |
| 1202 | E8) Why does the arithmetic evaluation code complain about `08'? |
| 1203 | |
| 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. |
| 1210 | |
| 1211 | The POSIX.2 interpretation committee has confirmed this: |
| 1212 | |
| 1213 | http://www.pasc.org/interps/unofficial/db/p1003.2/pasc-1003.2-173.html |
| 1214 | |
| 1215 | E9) Why does the pattern matching expression [A-Z]* match files beginning |
| 1216 | with every letter except `z'? |
| 1217 | |
| 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 |
| 1221 | specify. |
| 1222 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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 |
| 1228 | this: |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1229 | |
| 1230 | AaBb...Zz |
| 1231 | |
| 1232 | which means that [A-Z] matches every letter except `z'. |
| 1233 | |
| 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. |
| 1236 | |
| 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 |
| 1240 | LC_ variables set. |
| 1241 | |
| 1242 | My advice is to put |
| 1243 | |
| 1244 | export LC_COLLATE=C |
| 1245 | |
| 1246 | into /etc/profile and inspect any shell scripts run from cron for |
| 1247 | constructs like [A-Z]. This will prevent things like |
| 1248 | |
| 1249 | rm [A-Z]* |
| 1250 | |
| 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. |
| 1254 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1255 | Section F: Things to watch out for on certain Unix versions |
| 1256 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1257 | F1) Why can't I use command line editing in my `cmdtool'? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1258 | |
| 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. |
| 1265 | |
| 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. |
| 1270 | |
| 1271 | `xterm' is a better choice, and gets along with bash much more |
| 1272 | smoothly. |
| 1273 | |
| 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. |
| 1277 | |
| 1278 | TERMCAP='Mu|sun-cmd:am:bs:km:pt:li#34:co#80:cl=^L:ce=\E[K:cd=\E[J:rs=\E[s:' |
| 1279 | |
| 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. |
| 1284 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1285 | F2) I built bash on Solaris 2. Why do globbing expansions and filename |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1286 | completion chop off the first few characters of each filename? |
| 1287 | |
| 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. |
| 1291 | |
| 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'). |
| 1295 | |
| 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. |
| 1300 | |
| 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 |
| 1303 | /usr/ucb. |
| 1304 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1305 | F3) Why does bash dump core after I interrupt username completion or |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1306 | `~user' tilde expansion on a machine running NIS? |
| 1307 | |
| 1308 | This is a famous and long-standing bug in the SunOS YP (sorry, NIS) |
| 1309 | client library, which is part of libc. |
| 1310 | |
| 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. |
| 1314 | So far, so good. |
| 1315 | |
| 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. |
| 1322 | |
| 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. |
| 1327 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1328 | F4) I'm running SVR4.2. Why is the line erased every time I type `@'? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1329 | |
| 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 |
| 1334 | |
| 1335 | stty kill ^U |
| 1336 | |
| 1337 | where the `^' and `U' can be two separate characters. |
| 1338 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1339 | F5) Why does bash report syntax errors when my C News scripts use a |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1340 | redirection before a subshell command? |
| 1341 | |
| 1342 | The actual command in question is something like |
| 1343 | |
| 1344 | < file ( command ) |
| 1345 | |
| 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. |
| 1350 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 |
| 1354 | |
| 1355 | cat file | command |
| 1356 | |
| 1357 | can be converted to `< file command', shell control structures such as |
| 1358 | loops and subshells require `command < file'. |
| 1359 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 1360 | The file CWRU/sh-redir-hack in the bash-2.05a distribution is an |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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. |
| 1366 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1367 | F6) Why can't I use vi-mode editing on Red Hat Linux 6.1? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1368 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1369 | The short answer is that Red Hat screwed up. |
| 1370 | |
| 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. |
| 1374 | |
| 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 |
| 1379 | |
| 1380 | set keymap emacs |
| 1381 | |
| 1382 | to the beginning of /etc/inputrc, or bracket the key bindings in |
| 1383 | /etc/inputrc with these lines |
| 1384 | |
| 1385 | $if mode=emacs |
| 1386 | [...] |
| 1387 | $endif |
| 1388 | |
| 1389 | Section G: How can I get bash to do certain common things? |
| 1390 | |
| 1391 | G1) How can I get bash to read and display eight-bit characters? |
| 1392 | |
| 1393 | This is a process requiring several steps. |
| 1394 | |
| 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'. |
| 1398 | |
| 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: |
| 1402 | |
| 1403 | stty cs8 -istrip -parenb |
| 1404 | |
| 1405 | For old BSD-style systems, you can use |
| 1406 | |
| 1407 | stty pass8 |
| 1408 | |
| 1409 | You may also need |
| 1410 | |
| 1411 | stty even odd |
| 1412 | |
| 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': |
| 1417 | |
| 1418 | bash$ bind 'set convert-meta off' |
| 1419 | bash$ bind 'set meta-flag on' |
| 1420 | bash$ bind 'set output-meta on' |
| 1421 | |
| 1422 | The `set' commands between the single quotes may also be placed |
| 1423 | in ~/.inputrc. |
| 1424 | |
| 1425 | G2) How do I write a function `x' to replace builtin command `x', but |
| 1426 | still invoke the command from within the function? |
| 1427 | |
| 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 |
| 1432 | argument directly. |
| 1433 | |
| 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: |
| 1437 | |
| 1438 | cd() |
| 1439 | { |
| 1440 | builtin cd "$@" && xtitle "$HOST: $PWD" |
| 1441 | } |
| 1442 | |
| 1443 | This could also be written using `command' instead of `builtin'; |
| 1444 | the version above is marginally more efficient. |
| 1445 | |
| 1446 | G3) How can I find the value of a shell variable whose name is the value |
| 1447 | of another shell variable? |
| 1448 | |
| 1449 | Versions of Bash newer than Bash-2.0 support this directly. You can use |
| 1450 | |
| 1451 | ${!var} |
| 1452 | |
| 1453 | For example, the following sequence of commands will echo `z': |
| 1454 | |
| 1455 | var1=var2 |
| 1456 | var2=z |
| 1457 | echo ${!var1} |
| 1458 | |
| 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. |
| 1463 | |
| 1464 | For example, this expression prints the value of the last positional |
| 1465 | parameter: |
| 1466 | |
| 1467 | eval echo \"\$\{$#\}\" |
| 1468 | |
| 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, |
| 1472 | |
| 1473 | echo ${!#} |
| 1474 | |
| 1475 | does the same thing. |
| 1476 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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. |
| 1479 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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? |
| 1482 | |
| 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 |
| 1485 | timing statistics. |
| 1486 | |
| 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. |
| 1490 | |
| 1491 | If TIMEFORMAT is not set, bash acts as if the following assignment had |
| 1492 | been performed: |
| 1493 | |
| 1494 | TIMEFORMAT=$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS' |
| 1495 | |
| 1496 | The POSIX.2 default time format (used by `time -p command') is |
| 1497 | |
| 1498 | TIMEFORMAT=$'real %2R\nuser %2U\nsys %2S' |
| 1499 | |
| 1500 | The BSD /usr/bin/time format can be emulated with: |
| 1501 | |
| 1502 | TIMEFORMAT=$'\t%1R real\t%1U user\t%1S sys' |
| 1503 | |
| 1504 | The System V /usr/bin/time format can be emulated with: |
| 1505 | |
| 1506 | TIMEFORMAT=$'\nreal\t%1R\nuser\t%1U\nsys\t%1S' |
| 1507 | |
| 1508 | The ksh format can be emulated with: |
| 1509 | |
| 1510 | TIMEFORMAT=$'\nreal\t%2lR\nuser\t%2lU\nsys\t%2lS' |
| 1511 | |
| 1512 | G5) How do I get the current directory into my prompt? |
| 1513 | |
| 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 |
| 1516 | the manual page. |
| 1517 | |
| 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: |
| 1523 | |
| 1524 | PS1='\w$ ' # current directory with tilde |
| 1525 | PS1='\W$ ' # basename of current directory |
| 1526 | PS1='$PWD$ ' # full pathname of current directory |
| 1527 | |
| 1528 | The single quotes are important in the final example to prevent $PWD from |
| 1529 | being expanded when the assignment to PS1 is performed. |
| 1530 | |
| 1531 | G6) How can I rename "*.foo" to "*.bar"? |
| 1532 | |
| 1533 | Use the pattern removal functionality described in D3. The following `for' |
| 1534 | loop will do the trick: |
| 1535 | |
| 1536 | for f in *.foo; do |
| 1537 | mv $f ${f%foo}bar |
| 1538 | done |
| 1539 | |
| 1540 | G7) How can I translate a filename from uppercase to lowercase? |
| 1541 | |
| 1542 | The script examples/functions/lowercase, originally written by John DuBois, |
| 1543 | will do the trick. The converse is left as an exercise. |
| 1544 | |
| 1545 | G8) How can I write a filename expansion (globbing) pattern that will match |
| 1546 | all files in the current directory except "." and ".."? |
| 1547 | |
| 1548 | You must have set the `extglob' shell option using `shopt -s extglob' to use |
| 1549 | this: |
| 1550 | |
| 1551 | echo .!(.|) * |
| 1552 | |
| 1553 | A solution that works without extended globbing is given in the Unix Shell |
| 1554 | FAQ, posted periodically to comp.unix.shell. |
| 1555 | |
| 1556 | Section H: Where do I go from here? |
| 1557 | |
| 1558 | H1) How do I report bugs in bash, and where should I look for fixes and |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1559 | advice? |
| 1560 | |
| 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. |
| 1565 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1566 | `bashbug' sends its reports to bug-bash@gnu.org, which |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1567 | is a large mailing list gatewayed to the usenet newsgroup gnu.bash.bug. |
| 1568 | |
| 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. |
| 1572 | |
| 1573 | To reach the bash maintainers directly, send mail to |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1574 | bash-maintainers@gnu.org. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1575 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1576 | H2) What kind of bash documentation is there? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1577 | |
| 1578 | First, look in the doc directory in the bash distribution. It should |
| 1579 | contain at least the following files: |
| 1580 | |
| 1581 | bash.1 an extensive, thorough Unix-style manual page |
| 1582 | builtins.1 a manual page covering just bash builtin commands |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1583 | bashref.texi a reference manual in GNU tex`info format |
| 1584 | bashref.info an info version of the reference manual |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1585 | FAQ this file |
| 1586 | article.ms text of an article written for The Linux Journal |
| 1587 | readline.3 a man page describing readline |
| 1588 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1589 | Postscript, HTML, and ASCII files created from the above source are |
| 1590 | available in the documentation distribution. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1591 | |
| 1592 | There is additional documentation available for anonymous FTP from host |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1593 | ftp.cwru.edu in the `pub/bash' directory. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1594 | |
| 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 |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 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 |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1600 | in bash-2.0. |
| 1601 | |
| Jari Aalto | bc4cd23 | 1998-07-23 14:37:54 +0000 | [diff] [blame] | 1602 | A second edition of this book is available, published in January, 1998. |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1603 | The ISBN number is 1-56592-347-2. Look for it in the same fine bookstores |
| 1604 | or on the web. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1605 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1606 | H3) What's coming in future versions? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1607 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 1608 | These are features I hope to include in a future version of bash. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1609 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 1610 | a better bash debugger (a minimally-tested version is included with bash-2.05a) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1611 | associative arrays |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 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 |
| 1614 | versions) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 1615 | co-processes, but with a new-style syntax that looks like function declaration |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1616 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1617 | H4) What's on the bash `wish list' for future versions? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1618 | |
| 1619 | These are features that may or may not appear in a future version of bash. |
| 1620 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1621 | breaking some of the shell functionality into embeddable libraries |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1622 | a module system like zsh's, using dynamic loading like builtins |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1623 | better internationalization using GNU `gettext' |
| 1624 | an option to use external files for the long `help' text |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1625 | date-stamped command history |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 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 |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1634 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1635 | H5) When will the next release appear? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1636 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame^] | 1637 | The next version will appear sometime in 2002. Never make predictions. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1638 | |
| 1639 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1640 | This document is Copyright 1995-2001 by Chester Ramey. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1641 | |
| 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. |