| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 1 | This is the Bash FAQ, version 2.5, for Bash version 2.01. |
| 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 | |
| 16 | ftp://slc2.ins.cwru.edu/pub/bash/FAQ |
| 17 | |
| 18 | ---------- |
| 19 | Contents: |
| 20 | |
| 21 | Section A: The Basics |
| 22 | |
| 23 | 1) What is it? |
| 24 | 2) What's the latest version? |
| 25 | 3) Where can I get it? |
| 26 | 4) On what machines will bash run? |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 27 | 5) Will bash run on operating systems other than Unix? |
| 28 | 6) How can I build bash with gcc? |
| 29 | 7) How can I make bash my login shell? |
| 30 | 8) 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] | 31 | machine. Why not? |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 32 | 9) What's the `POSIX 1003.2 standard'? |
| 33 | 10) What is the bash `posix mode'? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 34 | |
| 35 | Section B: The latest version |
| 36 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 37 | 11) What's new in version 2.01? |
| 38 | 12) Are there any user-visible incompatibilities between bash-2.01 and |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 39 | bash-1.14.7? |
| 40 | |
| 41 | Section C: Differences from other Unix shells |
| 42 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 43 | 13) How does bash differ from sh, the Bourne shell? |
| 44 | 14) How does bash differ from the Korn shell, version ksh88? |
| 45 | 15) 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] | 46 | |
| 47 | Section D: Why does bash do some things differently than other Unix shells? |
| 48 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 49 | 16) Why does bash run a different version of `command' than |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 50 | `which command' says it will? |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 51 | 17) Why doesn't bash treat brace expansions exactly like csh? |
| 52 | 18) Why doesn't bash have csh variable modifiers? |
| 53 | 19) How can I make my csh aliases work when I convert to bash? |
| 54 | 20) 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] | 55 | another, like csh does with `|&'? |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 56 | 21) 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] | 57 | ksh features like autoloaded functions and the `whence' command? |
| 58 | |
| 59 | Section E: How can I get bash to do certain things, and why does bash do |
| 60 | things the way it does? |
| 61 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 62 | 22) Why is the bash builtin `test' slightly different from /bin/test? |
| 63 | 23) Why does bash sometimes say `Broken pipe'? |
| 64 | 24) How can I get bash to read and display eight-bit characters? |
| 65 | 25) How do I write a function `x' to replace builtin command `x', but |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 66 | still invoke the command from within the function? |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 67 | 26) When I have terminal escape sequences in my prompt, why does bash |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 68 | wrap lines at the wrong column? |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 69 | 27) How can I find the value of a shell variable whose name is the value |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 70 | of another shell variable? |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 71 | 28) 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] | 72 | the output show up in $variable when the read command finishes? |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 73 | 29) I have a bunch of shell scripts that use backslash-escaped characters |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 74 | in arguments to `echo'. Bash doesn't interpret these characters. Why |
| 75 | not, and how can I make it understand them? |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 76 | 30) Why doesn't a while or for loop get suspended when I type ^Z? |
| 77 | 31) How can I make the bash `time' reserved word print timing output that |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 78 | looks like the output from my system's /usr/bin/time? |
| 79 | |
| 80 | Section F: Things to watch out for on certain Unix versions |
| 81 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 82 | 32) Why can't I use command line editing in my `cmdtool'? |
| 83 | 33) I built bash on Solaris 2. Why do globbing expansions and filename |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 84 | completion chop off the first few characters of each filename? |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 85 | 34) Why does bash dump core after I interrupt username completion or |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 86 | `~user' tilde expansion on a machine running NIS? |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 87 | 35) I'm running SVR4.2. Why is the line erased every time I type `@'? |
| 88 | 36) 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] | 89 | redirection before a subshell command? |
| 90 | |
| 91 | Section G: Where do I go from here? |
| 92 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 93 | 37) 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] | 94 | advice? |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 95 | 38) What kind of bash documentation is there? |
| 96 | 39) What's coming in future versions? |
| 97 | 40) What's on the bash `wish list'? |
| 98 | 41) When will the next release appear? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 99 | |
| 100 | ---------- |
| 101 | Section A: The Basics |
| 102 | |
| 103 | 1) What is it? |
| 104 | |
| 105 | Bash is a Unix command interpreter (shell). It is an implementation of |
| 106 | the Posix 1003.2 shell standard, and resembles the Korn and System V |
| 107 | shells. |
| 108 | |
| 109 | Bash contains a number of enhancements over those shells, both |
| 110 | for interactive use and shell programming. Features geared |
| 111 | toward interactive use include command line editing, command |
| 112 | history, job control, aliases, and prompt expansion. Programming |
| 113 | features include additional variable expansions, shell |
| 114 | arithmetic, and a number of variables and options to control |
| 115 | shell behavior. |
| 116 | |
| 117 | Bash was originally written by Brian Fox of the Free Software |
| 118 | Foundation. The current developer and maintainer is Chet Ramey |
| 119 | of Case Western Reserve University. |
| 120 | |
| 121 | 2) What's the latest version? |
| 122 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 123 | The latest version is 2.01, first made available on June 6, 1997. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 124 | |
| 125 | 3) Where can I get it? |
| 126 | |
| 127 | Bash is the GNU project's shell, and so is available from the |
| 128 | master GNU archive site, prep.ai.mit.edu, and its mirrors. The |
| 129 | latest version is also available for FTP from slc2.ins.cwru.edu, |
| 130 | the maintainer's machine. The following URLs tell how to get |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 131 | version 2.01: |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 132 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 133 | ftp://prep.ai.mit.edu/pub/gnu/bash-2.01.tar.gz |
| 134 | ftp://slc2.ins.cwru.edu/pub/dist/bash-2.01.tar.gz |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 135 | |
| 136 | Formatted versions of the documentation are available with the URLs: |
| 137 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 138 | ftp://prep.ai.mit.edu/pub/gnu/bash-doc-2.01.tar.gz |
| 139 | ftp://slc2.ins.cwru.edu/pub/dist/bash-doc-2.01.tar.gz |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 140 | |
| 141 | 4) On what machines will bash run? |
| 142 | |
| 143 | Bash has been ported to nearly every version of UNIX. All you |
| 144 | should have to do to build it on a machine for which a port |
| 145 | exists is to type `configure' and then `make'. The build process |
| 146 | will attempt to discover the version of UNIX you have and tailor |
| 147 | itself accordingly, using a script created by GNU autoconf. |
| 148 | |
| 149 | More information appears in the file `INSTALL' in the distribution. |
| 150 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 151 | 5) Will bash run on operating systems other than Unix? |
| 152 | |
| 153 | Configuration specifics for Unix-like systems such as QNX and |
| 154 | LynxOS are included in the distribution. Previous versions of |
| 155 | bash have been ported to Minix, but I don't believe anyone has |
| 156 | built bash-2.x on Minix yet. |
| 157 | |
| 158 | Bash has been ported to versions of Windows implementing the Win32 |
| 159 | programming interface. This includes Windows 95 and Windows NT. |
| 160 | The port was done by Cygnus Solutions as part of their GNU-Win32 |
| 161 | project. For more information about the project, look at the URL |
| 162 | |
| 163 | http://www.cygnus.com/misc/gnu-win32 |
| 164 | |
| 165 | Cygnus has ported bash-1.14.7. Maybe someday they (or I) will port |
| 166 | bash-2.01 (or later) to the GNU-Win32 environment. |
| 167 | |
| 168 | D. J. Delorie has ported bash-1.14.7 to run under MS-DOS, as part of |
| 169 | the DJGPP project. For more information on the project, see |
| 170 | |
| 171 | http://www.delorie.com/djgpp/ |
| 172 | |
| 173 | I picked up a binary of bash-1.14.7 that is purported to work with |
| 174 | the DJGPP V2 environment from |
| 175 | |
| 176 | ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh1147b.zip |
| 177 | |
| 178 | The corresponding source is |
| 179 | |
| 180 | ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh1147s.zip |
| 181 | |
| 182 | A port of bash-1.12 is available for OS/2 from |
| 183 | |
| 184 | ftp://hobbes.nmsu.edu/os2/unix/bash_112.zip |
| 185 | |
| 186 | I haven't looked at it. |
| 187 | |
| 188 | 6) How can I build bash with gcc? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 189 | |
| 190 | Bash configures to use gcc by default if it is available. Read the |
| 191 | file INSTALL in the distribution for more information. |
| 192 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 193 | 7) How can I make bash my login shell? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 194 | |
| 195 | Some machines let you use `chsh' to change your login shell. Other |
| 196 | systems use `passwd -s'. If one of these works for you, that's all |
| 197 | you need. Note that many systems require the full pathname to a shell |
| 198 | to appear in /etc/shells before you can make it your login shell. For |
| 199 | this, you may need the assistance of your friendly local system |
| 200 | administrator. |
| 201 | |
| 202 | If you cannot do this, you can still use bash as your login shell, but |
| 203 | you need to perform some tricks. The basic idea is to add a command |
| 204 | to your login shell's startup file to replace your login shell with |
| 205 | bash. |
| 206 | |
| 207 | For example, if your login shell is csh or tcsh, and you have installed |
| 208 | bash in /usr/gnu/bin/bash, add the following line to ~/.login: |
| 209 | |
| 210 | if ( -f /usr/gnu/bin/bash ) exec /usr/gnu/bin/bash --login |
| 211 | |
| 212 | (the `--login' tells bash that it is a login shell). |
| 213 | |
| 214 | It's not a good idea to put this command into ~/.cshrc, because every |
| 215 | csh you run without the `-f' option, even ones started to run csh scripts, |
| 216 | reads that file. If you must put the command in ~/.cshrc, use something |
| 217 | like |
| 218 | |
| 219 | if ( $?prompt ) exec /usr/gnu/bin/bash --login |
| 220 | |
| 221 | to ensure that bash is exec'd only when the csh is interactive. |
| 222 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 223 | 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] | 224 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 225 | First, create an empty file in your home directory named `.bash_profile'. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 226 | The existence of this file will prevent the exec'd bash from trying to |
| 227 | read ~/.profile, and re-execing itself over and over again. ~/.bash_profile |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 228 | is the first file bash tries to read initialization commands from when |
| 229 | it is invoked as a login shell. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 230 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 231 | Next, add a line similar to the above to ~/.profile: |
| 232 | |
| 233 | [ -f /usr/gnu/bin/bash ] && exec /usr/gnu/bin/bash --login |
| 234 | |
| 235 | This will cause login shells to replace themselves with bash running as |
| 236 | a login shell. Once you have this working, you can copy your initialization |
| 237 | code from ~/.profile to ~/.bash_profile. |
| 238 | |
| 239 | 8) 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] | 240 | machine. Why not? |
| 241 | |
| 242 | You must add the full pathname to bash to the file /etc/shells. As |
| 243 | noted in the answer to the previous question, many systems require |
| 244 | this before you can make bash your login shell. |
| 245 | |
| 246 | Most versions of ftpd use this file to prohibit `special' users |
| 247 | such as `uucp' and `news' from using FTP. |
| 248 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 249 | 9) What's the `POSIX 1003.2 standard'? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 250 | |
| 251 | POSIX is a name originally coined by Richard Stallman for a |
| 252 | family of open system standards based on UNIX. There are a |
| 253 | number of aspects of UNIX under consideration for |
| 254 | standardization, from the basic system services at the system |
| 255 | call and C library level to applications and tools to system |
| 256 | administration and management. Each area of standardization is |
| 257 | assigned to a working group in the 1003 series. |
| 258 | |
| 259 | The POSIX Shell and Utilities standard has been developed by IEEE |
| 260 | Working Group 1003.2 (POSIX.2). It concentrates on the command |
| 261 | interpreter interface and utility programs commonly executed from |
| 262 | the command line or by other programs. An initial version of the |
| 263 | standard has been approved and published by the IEEE, and work is |
| 264 | currently underway to update it. |
| 265 | |
| 266 | Bash is concerned with the aspects of the shell's behavior |
| 267 | defined by POSIX.2. The shell command language has of course |
| 268 | been standardized, including the basic flow control and program |
| 269 | execution constructs, I/O redirection and pipelining, argument |
| 270 | handling, variable expansion, and quoting. |
| 271 | |
| 272 | The `special' builtins, which must be implemented as part of the |
| 273 | shell to provide the desired functionality, are specified as |
| 274 | being part of the shell; examples of these are `eval' and |
| 275 | `export'. Other utilities appear in the sections of POSIX.2 not |
| 276 | devoted to the shell which are commonly (and in some cases must |
| 277 | be) implemented as builtin commands, such as `read' and `test'. |
| 278 | POSIX.2 also specifies aspects of the shell's interactive |
| 279 | behavior as part of the UPE, including job control and command |
| 280 | line editing. Only vi-style line editing commands have been |
| 281 | standardized; emacs editing commands were left out due to |
| 282 | objections. |
| 283 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 284 | 10) What is the bash `posix mode'? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 285 | |
| 286 | Although bash is an implementation of the POSIX.2 shell |
| 287 | specification, there are areas where the bash default behavior |
| 288 | differs from that spec. The bash `posix mode' changes the bash |
| 289 | behavior in these areas so that it obeys the spec more closely. |
| 290 | |
| 291 | Posix mode is entered by starting bash with the --posix option or |
| 292 | executing `set -o posix' after bash is running. |
| 293 | |
| 294 | The specific aspects of bash which change when posix mode is |
| 295 | active are listed in the file CWRU/POSIX.NOTES in the bash |
| 296 | distribution. They are also listed in a section in the Bash |
| 297 | Reference Manual. |
| 298 | |
| 299 | Section B: The latest version |
| 300 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 301 | 11) What's new in version 2.01? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 302 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 303 | Bash-2.01 contains only a few new features. |
| 304 | |
| 305 | new `GROUPS' builtin array variable containing the user's group list |
| 306 | new bindable readline commands: history-and-alias-expand-line and |
| 307 | alias-expand-line |
| 308 | |
| 309 | Bash-2.0 contains extensive changes and new features from bash-1.14.7. |
| 310 | Here's a short list: |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 311 | |
| 312 | new `time' reserved word to time pipelines, shell builtins, and |
| 313 | shell functions |
| 314 | one-dimensional arrays with a new compound assignment statement, |
| 315 | appropriate expansion constructs and modifications to some |
| 316 | of the builtins (read, declare, etc.) to use them |
| 317 | new quoting syntaxes for ANSI-C string expansion and locale-specific |
| 318 | string translation |
| 319 | new expansions to do substring extraction, pattern replacement, and |
| 320 | indirect variable expansion |
| 321 | new builtins: `disown' and `shopt' |
| 322 | new variables: HISTIGNORE, SHELLOPTS, PIPESTATUS, DIRSTACK, GLOBIGNORE, |
| 323 | MACHTYPE, BASH_VERSINFO |
| 324 | special handling of many unused or redundant variables removed |
| 325 | (e.g., $notify, $glob_dot_filenames, $no_exit_on_failed_exec) |
| 326 | dynamic loading of new builtin commands; many loadable examples provided |
| 327 | new prompt expansions: \a, \e, \n, \H, \T, \@, \v, \V |
| 328 | history and aliases available in shell scripts |
| 329 | new readline variables: enable-keypad, mark-directories, input-meta, |
| 330 | visible-stats, disable-completion, comment-begin |
| 331 | new readline commands to manipulate the mark and operate on the region |
| 332 | new readline emacs mode commands and bindings for ksh-88 compatibility |
| 333 | updated and extended builtins |
| 334 | new DEBUG trap |
| 335 | expanded (and now documented) restricted shell mode |
| 336 | |
| 337 | implementation stuff: |
| 338 | autoconf-based configuration |
| 339 | nearly all of the bugs reported since version 1.14 have been fixed |
| 340 | most builtins converted to use builtin `getopt' for consistency |
| 341 | most builtins use -p option to display output in a reusable form |
| 342 | (for consistency) |
| 343 | grammar tighter and smaller (66 reduce-reduce conflicts gone) |
| 344 | lots of code now smaller and faster |
| 345 | test suite greatly expanded |
| 346 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 347 | 12) Are there any user-visible incompatibilities between bash-2.01 and |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 348 | bash-1.14.7? |
| 349 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 350 | There are a few incompatibilities between version 1.14.7 and version 2.01. |
| 351 | They are detailed in the file COMPAT in the bash-2.01 distribution. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 352 | |
| 353 | Section C: Differences from other Unix shells |
| 354 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 355 | 13) How does bash differ from sh, the Bourne shell? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 356 | |
| 357 | This is a non-comprehensive list of features that differentiate bash |
| 358 | from the SVR4.2 shell. The bash manual page explains these more |
| 359 | completely. |
| 360 | |
| 361 | Things bash has that sh does not: |
| 362 | long invocation options |
| 363 | `!' reserved word to invert pipeline return value |
| 364 | `time' reserved word to time pipelines and shell builtins |
| 365 | the `function' reserved word |
| 366 | the select compound command and reserved word |
| 367 | new $'...' and $"..." quoting |
| 368 | the $(...) form of command substitution |
| 369 | the ${#param} parameter value length operator |
| 370 | the ${!param} indirect parameter expansion operator |
| 371 | the ${param:length[:offset]} parameter substring operator |
| 372 | the ${param/pat[/string]} parameter pattern substitution operator |
| 373 | expansions to perform substring removal (${p%[%]w}, ${p#[#]w}) |
| 374 | expansion of positional parameters beyond $9 with ${num} |
| 375 | variables: BASH, BASH_VERSION, BASH_VERSINFO, UID, EUID, REPLY, |
| 376 | TIMEFORMAT, PPID, PWD, OLDPWD, SHLVL, RANDOM, SECONDS, |
| 377 | LINENO, HISTCMD, HOSTTYPE, OSTYPE, MACHTYPE, HOSTNAME, |
| 378 | ENV, PS3, PS4, DIRSTACK, PIPESTATUS, HISTSIZE, HISTFILE, |
| 379 | HISTFILESIZE, HISTCONTROL, HISTIGNORE, GLOBIGNORE, |
| 380 | PROMPT_COMMAND, FCEDIT, FIGNORE, IGNOREEOF, INPUTRC, |
| 381 | SHELLOPTS, OPTERR, HOSTFILE, TMOUT, histchars, auto_resume |
| 382 | DEBUG trap |
| 383 | variable arrays with new compound assignment syntax |
| 384 | redirections: <>, &>, >| |
| 385 | prompt string special char translation and variable expansion |
| 386 | auto-export of modified values of variables in initial environment |
| 387 | command search finds functions before builtins |
| 388 | bash return builtin will exit a file sourced with `.' |
| 389 | builtins: cd -/-L/-P, exec -l/-c/-a, echo -e/-E, hash -p. |
| 390 | export -n/-f/-p/name=value, pwd -L/-P, read -e/-p/-a, |
| 391 | readonly -a/-f/name=value, trap -l, set +o, |
| 392 | set -b/-m/-o option/-h/-p/-B/-C/-H/-P, |
| 393 | unset -f/-v, ulimit -m/-p/-u, |
| 394 | type -a/-p/-t, suspend -f, kill -n, |
| 395 | test -o optname/s1 == s2/s1 < s2/s1 > s2/-nt/-ot/-ef/-O/-G/-S |
| 396 | bash reads ~/.bashrc for interactive shells, $ENV for non-interactive |
| 397 | bash restricted shell mode is more extensive |
| 398 | bash allows functions and variables with the same name |
| 399 | brace expansion |
| 400 | tilde expansion |
| 401 | arithmetic expansion with $((...)) and `let' builtin |
| 402 | process substitution |
| 403 | aliases and alias/unalias builtins |
| 404 | local variables in functions and `local' builtin |
| 405 | readline and command-line editing |
| 406 | command history and history/fc builtins |
| 407 | csh-like history expansion |
| 408 | other new bash builtins: bind, command, builtin, declare/typeset, |
| 409 | dirs, enable, fc, help, history, logout, |
| 410 | popd, pushd, disown, shopt |
| 411 | exported functions |
| 412 | filename generation when using output redirection (command >a*) |
| 413 | variable assignments preceding commands affect only that command, |
| 414 | even for builtins and functions |
| 415 | posix mode |
| 416 | |
| 417 | Things sh has that bash does not: |
| 418 | uses variable SHACCT to do shell accounting |
| 419 | includes `stop' builtin (bash can use alias stop='kill -s STOP') |
| 420 | `newgrp' builtin |
| 421 | turns on job control if called as `jsh' |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 422 | $TIMEOUT (like bash $TMOUT) |
| 423 | `^' is a synonym for `|' |
| 424 | new SVR4.2 sh builtins: mldmode, priv |
| 425 | |
| 426 | Implementation differences: |
| 427 | redirection to/from compound commands causes sh to create a subshell |
| 428 | bash does not allow unbalanced quotes; sh silently inserts them at EOF |
| 429 | bash does not mess with signal 11 |
| 430 | sh sets (euid, egid) to (uid, gid) if -p not supplied and uid < 100 |
| 431 | bash splits only the results of expansions on IFS, using POSIX.2 |
| 432 | field splitting rules; sh splits all words on IFS |
| 433 | sh does not allow MAILCHECK to be unset (?) |
| 434 | sh does not allow traps on SIGALRM or SIGCHLD |
| 435 | bash allows multiple option arguments when invoked (e.g. -x -v); |
| 436 | sh allows only a single option argument (`sh -x -v' attempts |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 437 | to open a file named `-v', and, on SunOS 4.1.4, dumps core. |
| 438 | On Solaris 2, sh goes into an infinite loop.) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 439 | sh exits a script if any builtin fails; bash exits only if one of |
| 440 | the POSIX.2 `special' builtins fails |
| 441 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 442 | 14) How does bash differ from the Korn shell, version ksh88? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 443 | |
| 444 | Things bash has or uses that ksh88 does not: |
| 445 | long invocation options |
| 446 | `!' reserved word |
| 447 | posix mode and posix conformance |
| 448 | command hashing |
| 449 | tilde expansion for assignment statements that look like $PATH |
| 450 | process substitution with named pipes if /dev/fd is not available |
| 451 | the ${!param} indirect parameter expansion operator |
| 452 | the ${param:length[:offset]} parameter substring operator |
| 453 | the ${param/pat[/string]} parameter pattern substitution operator |
| 454 | variables: BASH, BASH_VERSION, BASH_VERSINFO, UID, EUID, SHLVL, |
| 455 | TIMEFORMAT, HISTCMD, HOSTTYPE, OSTYPE, MACHTYPE, |
| 456 | HISTFILESIZE, HISTIGNORE, HISTCONTROL, PROMPT_COMMAND, |
| 457 | IGNOREEOF, FIGNORE, INPUTRC, HOSTFILE, DIRSTACK, |
| 458 | PIPESTATUS, HOSTNAME, OPTERR, SHELLOPTS, GLOBIGNORE, |
| 459 | histchars, auto_resume |
| 460 | prompt expansion with backslash escapes and command substitution |
| 461 | redirection: &> (stdout and stderr) |
| 462 | more extensive and extensible editing and completion |
| 463 | builtins: bind, builtin, command, declare, dirs, echo -e/-E, enable, |
| 464 | exec -l/-c/-a, fc -s, export -n/-f/-p, hash, help, history, |
| 465 | jobs -x/-r/-s, kill -s/-n/-l, local, logout, popd, pushd, |
| 466 | read -e/-p/-a, readonly -a/-n/-f/-p, set -o braceexpand/ |
| 467 | -o histexpand/-o interactive-comments/-o notify/-o physical/ |
| 468 | -o posix/-o hashall/-o onecmd/-h/-B/-C/-b/-H/-P, set +o, |
| 469 | suspend, trap -l, type, typeset -a/-F/-p, ulimit -u, |
| 470 | umask -S, alias -p, shopt, disown |
| 471 | `!' csh-style history expansion |
| 472 | |
| 473 | Things ksh88 has or uses that bash does not: |
| 474 | new version of test: [[...]] |
| 475 | tracked aliases |
| 476 | $(<file) |
| 477 | variables: ERRNO, FPATH, COLUMNS, LINES, EDITOR, VISUAL |
| 478 | extended pattern matching with egrep-style pattern lists |
| 479 | co-processes (|&, >&p, <&p) |
| 480 | weirdly-scoped functions |
| 481 | typeset +f to list all function names without definitions |
| 482 | text of command history kept in a file, not memory |
| 483 | builtins: alias -x, cd old new, fc -e -, newgrp, print, |
| 484 | read -p/-s/-u/var?prompt, set -A/-o gmacs/ |
| 485 | -o bgnice/-o markdirs/-o nolog/-o trackall/-o viraw/-s, |
| 486 | typeset -H/-L/-R/-A/-ft/-fu/-fx/-l/-u/-t, whence |
| 487 | |
| 488 | Implementation differences: |
| 489 | ksh runs last command of a pipeline in parent shell context |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 490 | bash has brace expansion by default (ksh88 compile-time option) |
| 491 | bash has fixed startup file for all interactive shells; ksh reads $ENV |
| 492 | bash has exported functions |
| 493 | bash command search finds functions before builtins |
| 494 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 495 | 15) 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] | 496 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 497 | New things in ksh-93 not in bash-2.01: |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 498 | associative arrays |
| 499 | floating point arithmetic |
| 500 | ++, --, comma arithmetic operators |
| 501 | math library functions |
| 502 | ${!name[sub]} name of subscript for associative array |
| 503 | ${!prefix*} and {!prefix@} variable name prefix expansions |
| 504 | `.' is allowed in variable names to create a hierarchical namespace |
| 505 | more extensive compound assignment syntax |
| 506 | discipline functions |
| 507 | `sleep' and `getconf' builtins (bash has loadable versions) |
| 508 | typeset -n and `nameref' variables |
| 509 | KEYBD trap |
| 510 | variables: .sh.edchar, .sh.edmode, .sh.edcol, .sh.edtext, HISTEDIT, |
| 511 | .sh.version, .sh.name, .sh.subscript, .sh.value |
| 512 | backreferences in pattern matching |
| 513 | print -f and printf (bash has loadable versions) |
| 514 | `fc' has been renamed to `hist' |
| 515 | read -t/-d |
| 516 | `.' can execute shell functions |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 517 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 518 | New things in ksh-93 present in bash-2.01: |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 519 | ?: arithmetic operator |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 520 | expansions: ${!param}, ${param:offset[:len]}, ${param/pat[/str]} |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 521 | compound array assignment |
| 522 | the `!' reserved word |
| 523 | loadable builtins -- but ksh uses `builtin' while bash uses `enable' |
| 524 | `command', `builtin', `disown' builtins |
| 525 | new $'...' and $"..." quoting |
| 526 | FIGNORE (but bash uses GLOBIGNORE), HISTCMD |
| 527 | set -o notify/-C |
| 528 | changes to kill builtin |
| 529 | read -A (bash uses read -a) |
| 530 | trap -p |
| 531 | exec -c/-a |
| 532 | `.' restores the positional parameters when it completes |
| 533 | POSIX.2 `test' |
| 534 | umask -S |
| 535 | unalias -a |
| 536 | command and arithmetic substitution performed on PS1, PS4, and ENV |
| 537 | command name completion |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 538 | ENV processed only for interactive shells |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 539 | |
| 540 | Section D: Why does bash do some things differently than other Unix shells? |
| 541 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 542 | 16) Why does bash run a different version of `command' than |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 543 | `which command' says it will? |
| 544 | |
| 545 | `which' is actually a csh script that assumes you're running csh. |
| 546 | It reads the csh startup files from your home directory and uses |
| 547 | those to determine which `command' will be invoked. Since bash |
| 548 | doesn't use any of those startup files, there's a good chance |
| 549 | that your bash environment differs from your csh environment. |
| 550 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 551 | 17) Why doesn't bash treat brace expansions exactly like csh? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 552 | |
| 553 | The only difference between bash and csh brace expansion is that |
| 554 | bash requires a brace expression to contain at least one unquoted |
| 555 | comma if it is to be expanded. Any brace-surrounded word not |
| 556 | containing an unquoted comma is left unchanged by the brace |
| 557 | expansion code. This affords the greatest degree of sh |
| 558 | compatibility. |
| 559 | |
| 560 | Bash, ksh, zsh, and pd-ksh all implement brace expansion this way. |
| 561 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 562 | 18) Why doesn't bash have csh variable modifiers? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 563 | |
| 564 | Posix has specified a more powerful, albeit somewhat more cryptic, |
| 565 | mechanism cribbed from ksh, and bash implements it. |
| 566 | |
| 567 | ${parameter%word} |
| 568 | Remove smallest suffix pattern. The WORD is expanded to produce |
| 569 | a pattern. It then expands to the value of PARAMETER, with the |
| 570 | smallest portion of the suffix matched by the pattern deleted. |
| 571 | |
| 572 | x=file.c |
| 573 | echo ${x%.c}.o |
| 574 | -->file.o |
| 575 | |
| 576 | ${parameter%%word} |
| 577 | |
| 578 | Remove largest suffix pattern. The WORD is expanded to produce |
| 579 | a pattern. It then expands to the value of PARAMETER, with the |
| 580 | largest portion of the suffix matched by the pattern deleted. |
| 581 | |
| 582 | x=posix/src/std |
| 583 | echo ${x%%/*} |
| 584 | -->posix |
| 585 | |
| 586 | ${parameter#word} |
| 587 | Remove smallest prefix pattern. The WORD is expanded to produce |
| 588 | a pattern. It then expands to the value of PARAMETER, with the |
| 589 | smallest portion of the prefix matched by the pattern deleted. |
| 590 | |
| 591 | x=$HOME/src/cmd |
| 592 | echo ${x#$HOME} |
| 593 | -->/src/cmd |
| 594 | |
| 595 | ${parameter##word} |
| 596 | Remove largest prefix pattern. The WORD is expanded to produce |
| 597 | a pattern. It then expands to the value of PARAMETER, with the |
| 598 | largest portion of the prefix matched by the pattern deleted. |
| 599 | |
| 600 | x=/one/two/three |
| 601 | echo ${x##*/} |
| 602 | -->three |
| 603 | |
| 604 | |
| 605 | Given |
| 606 | a=/a/b/c/d |
| 607 | b=b.xxx |
| 608 | |
| 609 | csh bash result |
| 610 | --- ---- ------ |
| 611 | $a:h ${a%/*} /a/b/c |
| 612 | $a:t ${a##*/} d |
| 613 | $b:r ${b%.*} b |
| 614 | $b:e ${b##*.} xxx |
| 615 | |
| 616 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 617 | 19) 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] | 618 | |
| 619 | Bash uses a different syntax to support aliases than csh does. |
| 620 | The details can be found in the documentation. We have provided |
| 621 | a shell script which does most of the work of conversion for you; |
| 622 | this script can be found in ./examples/misc/alias-conv.sh. Here is |
| 623 | how you use it: |
| 624 | |
| 625 | Start csh in the normal way for you. (e.g., `csh') |
| 626 | |
| 627 | Pipe the output of `alias' through `alias-conv.sh', saving the |
| 628 | results into `bash_aliases': |
| 629 | |
| 630 | alias | alias-conv.sh >bash_aliases |
| 631 | |
| 632 | Edit `bash_aliases', carefully reading through any created |
| 633 | functions. You will need to change the names of some csh specific |
| 634 | variables to the bash equivalents. The script converts $cwd to |
| 635 | $PWD, $term to $TERM, $home to $HOME, $user to $USER, and $prompt |
| 636 | to $PS1. You may also have to add quotes to avoid unwanted |
| 637 | expansion. |
| 638 | |
| 639 | For example, the csh alias: |
| 640 | |
| 641 | alias cd 'cd \!*; echo $cwd' |
| 642 | |
| 643 | is converted to the bash function: |
| 644 | |
| 645 | cd () { command cd "$@"; echo $PWD ; } |
| 646 | |
| 647 | The only thing that needs to be done is to quote $PWD: |
| 648 | |
| 649 | cd () { command cd "$@"; echo "$PWD" ; } |
| 650 | |
| 651 | Merge the edited file into your ~/.bashrc. |
| 652 | |
| 653 | There is an additional, more ambitious, script in |
| 654 | examples/misc/cshtobash that attempts to convert your entire csh |
| 655 | environment to its bash equivalent. This script can be run as |
| 656 | simply `cshtobash' to convert your normal interactive |
| 657 | environment, or as `cshtobash ~/.login' to convert your login |
| 658 | environment. |
| 659 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 660 | 20) 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] | 661 | another, like csh does with `|&'? |
| 662 | |
| 663 | Use |
| 664 | command 2>&1 | command2 |
| 665 | |
| 666 | The key is to remember that piping is performed before redirection, so |
| 667 | file descriptor 1 points to the pipe when it is duplicated onto file |
| 668 | descriptor 2. |
| 669 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 670 | 21) 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] | 671 | ksh features like autoloaded functions and the `whence' command? |
| 672 | |
| 673 | There are features in ksh-88 that do not have direct bash equivalents. |
| 674 | Most, however, can be emulated with very little trouble. |
| 675 | |
| 676 | ksh-88 feature Bash equivalent |
| 677 | -------------- --------------- |
| 678 | [[...]] can usually use [...]; minor differences (no |
| 679 | pattern matching, for one) |
| 680 | compiled-in aliases set up aliases in .bashrc; some ksh aliases are |
| 681 | bash builtins (hash, history, type) |
| 682 | $(<file) $(cat file) |
| 683 | extended patterns no good substitute |
| 684 | coprocesses named pipe pairs (one for read, one for write) |
| 685 | typeset +f declare -F |
| 686 | cd, print, whence function substitutes in examples/functions/kshenv |
| 687 | autoloaded functions examples/functions/autoload is the same as typeset -fu |
| 688 | read var?prompt read -p prompt var |
| 689 | |
| 690 | Section E: How can I get bash to do certain things, and why does bash do |
| 691 | things the way it does? |
| 692 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 693 | 22) Why is the bash builtin `test' slightly different from /bin/test? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 694 | |
| 695 | The specific example used here is [ ! x -o x ], which is false. |
| 696 | |
| 697 | Bash's builtin `test' implements the Posix.2 spec, which can be |
| 698 | summarized as follows (the wording is due to David Korn): |
| 699 | |
| 700 | Here is the set of rules for processing test arguments. |
| 701 | |
| 702 | 0 Args: False |
| 703 | 1 Arg: True iff argument is not null. |
| 704 | 2 Args: If first arg is !, True iff second argument is null. |
| 705 | If first argument is unary, then true if unary test is true |
| 706 | Otherwise error. |
| 707 | 3 Args: If second argument is a binary operator, do binary test of $1 $3 |
| 708 | If first argument is !, negate two argument test of $2 $3 |
| 709 | If first argument is `(' and third argument is `)', do the |
| 710 | one-argument test of the second argument. |
| 711 | Otherwise error. |
| 712 | 4 Args: If first argument is !, negate three argument test of $2 $3 $4. |
| 713 | Otherwise unspecified |
| 714 | 5 or more Args: unspecified. (Historical shells would use their |
| 715 | current algorithm). |
| 716 | |
| 717 | The operators -a and -o are considered binary operators for the purpose |
| 718 | of the 3 Arg case. |
| 719 | |
| 720 | As you can see, the test becomes (not (x or x)), which is false. |
| 721 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 722 | 23) Why does bash sometimes say `Broken pipe'? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 723 | |
| 724 | If a sequence of commands appears in a pipeline, and one of the |
| 725 | reading commands finishes before the writer has finished, the |
| 726 | writer receives a SIGPIPE signal. Many other shells special-case |
| 727 | SIGPIPE as an exit status in the pipeline and do not report it. |
| 728 | For example, in: |
| 729 | |
| 730 | ps -aux | head |
| 731 | |
| 732 | `head' can finish before `ps' writes all of its output, and ps |
| 733 | will try to write on a pipe without a reader. In that case, bash |
| 734 | will print `Broken pipe' to stderr when ps is killed by a |
| 735 | SIGPIPE. |
| 736 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 737 | 24) How can I get bash to read and display eight-bit characters? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 738 | |
| 739 | This is a process requiring several steps. |
| 740 | |
| 741 | First, you must ensure that the `physical' data path is a full eight |
| 742 | bits. For xterms, for example, the `vt100' resources `eightBitInput' |
| 743 | and `eightBitOutput' should be set to `true'. |
| 744 | |
| 745 | Once you have set up an eight-bit path, you must tell the kernel and |
| 746 | tty driver to leave the eighth bit of characters alone when processing |
| 747 | keyboard input. Use `stty' to do this: |
| 748 | |
| 749 | stty cs8 -istrip -parenb |
| 750 | |
| 751 | For old BSD-style systems, you can use |
| 752 | |
| 753 | stty pass8 |
| 754 | |
| 755 | You may also need |
| 756 | |
| 757 | stty even odd |
| 758 | |
| 759 | Finally, you need to tell readline that you will be inputting and |
| 760 | displaying eight-bit characters. You use readline variables to do |
| 761 | this. These variables can be set in your .inputrc or using the bash |
| 762 | `bind' builtin. Here's an example using `bind': |
| 763 | |
| 764 | bash$ bind 'set convert-meta off' |
| 765 | bash$ bind 'set meta-flag on' |
| 766 | bash$ bind 'set output-meta on' |
| 767 | |
| 768 | The `set' commands between the single quotes may also be placed |
| 769 | in ~/.inputrc. |
| 770 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 771 | 25) How do I write a function `x' to replace builtin command `x', but |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 772 | still invoke the command from within the function? |
| 773 | |
| 774 | This is why the `command' and `builtin' builtins exist. The |
| 775 | `command' builtin executes the command supplied as its first |
| 776 | argument, skipping over any function defined with that name. The |
| 777 | `builtin' builtin executes the builtin command given as its first |
| 778 | argument directly. |
| 779 | |
| 780 | For example, to write a function to replace `cd' that writes the |
| 781 | hostname and current directory to an xterm title bar, use |
| 782 | something like the following: |
| 783 | |
| 784 | cd() |
| 785 | { |
| 786 | builtin cd "$@" && xtitle "$HOST: $PWD" |
| 787 | } |
| 788 | |
| 789 | This could also be written using `command' instead of `builtin'; |
| 790 | the version above is marginally more efficient. |
| 791 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 792 | 26) When I have terminal escape sequences in my prompt, why does bash |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 793 | wrap lines at the wrong column? |
| 794 | |
| 795 | Readline, the line editing library that bash uses, does not know |
| 796 | that the terminal escape sequences do not take up space on the |
| 797 | screen. The redisplay code assumes, unless told otherwise, that |
| 798 | each character in the prompt is a `printable' character that |
| 799 | takes up one character position on the screen. |
| 800 | |
| 801 | You can use the bash prompt expansion facility (see the PROMPTING |
| 802 | section in the manual page) to tell readline that sequences of |
| 803 | characters in the prompt strings take up no screen space. |
| 804 | |
| 805 | Use the \[ escape to begin a sequence of non-printing characters, |
| 806 | and the \] escape to signal the end of such a sequence. |
| 807 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 808 | 27) How can I find the value of a shell variable whose name is the value |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 809 | of another shell variable? |
| 810 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 811 | Bash-2.01 supports this directly. You can use |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 812 | |
| 813 | ${!var} |
| 814 | |
| 815 | For example, the following sequence of commands will echo `z': |
| 816 | |
| 817 | var1=var2 |
| 818 | var2=z |
| 819 | echo ${!var1} |
| 820 | |
| 821 | For sh compatibility, use the `eval' builtin. The important |
| 822 | thing to remember is that `eval' expands the arguments you give |
| 823 | it again, so you need to quote the parts of the arguments that |
| 824 | you want `eval' to act on. |
| 825 | |
| 826 | For example, this expression prints the value of the last positional |
| 827 | parameter: |
| 828 | |
| 829 | eval echo \"\$\{$#\}\" |
| 830 | |
| 831 | The expansion of the quoted portions of this expression will be |
| 832 | deferred until `eval' runs, while the `$#' will be expanded |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 833 | before `eval' is executed. In bash-2.01, |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 834 | |
| 835 | echo ${!#} |
| 836 | |
| 837 | does the same thing. |
| 838 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 839 | 28) 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] | 840 | the output show up in $variable when the read command finishes? |
| 841 | |
| 842 | This has to do with the parent-child relationship between Unix |
| 843 | processes. |
| 844 | |
| 845 | Each element of a pipeline runs in a separate process, a child of |
| 846 | the shell running the pipeline. A subprocess cannot affect its |
| 847 | parent's environment. When the `read' command sets the variable |
| 848 | to the input, that variable is set only in the subshell, not the |
| 849 | parent shell. When the subshell exits, the value of the variable |
| 850 | is lost. |
| 851 | |
| 852 | Many pipelines that end with `read variable' can be converted |
| 853 | into command substitutions, which will capture the output of |
| 854 | a specified command. The output can then be assigned to a |
| 855 | variable: |
| 856 | |
| 857 | grep ^gnu /usr/lib/news/active | wc -l | read ngroup |
| 858 | |
| 859 | can be converted into |
| 860 | |
| 861 | ngroup=$(grep ^gnu /usr/lib/news/active | wc -l) |
| 862 | |
| 863 | This does not, unfortunately, work to split the text among |
| 864 | multiple variables, as read does when given multiple variable |
| 865 | arguments. If you need to do this, you can either use the |
| 866 | command substitution above to read the output into a variable |
| 867 | and chop up the variable using the bash pattern removal |
| 868 | expansion operators or use some variant of the following |
| 869 | approach. |
| 870 | |
| 871 | Say /usr/local/bin/ipaddr is the following shell script: |
| 872 | |
| 873 | #! /bin/sh |
| 874 | host `hostname` | awk '/address/ {print $NF}' |
| 875 | |
| 876 | Instead of using |
| 877 | |
| 878 | /usr/local/bin/ipaddr | read A B C D |
| 879 | |
| 880 | to break the local machine's IP address into separate octets, use |
| 881 | |
| 882 | OIFS="$IFS" |
| 883 | IFS=. |
| 884 | set -- $(/usr/local/bin/ipaddr) |
| 885 | IFS="$OIFS" |
| 886 | A="$1" B="$2" C="$3" D="$4" |
| 887 | |
| 888 | Beware, however, that this will change the shell's positional |
| 889 | parameters. If you need them, you should save them before doing |
| 890 | this. |
| 891 | |
| 892 | This is the general approach -- in most cases you will not need to |
| 893 | set $IFS to a different value. |
| 894 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 895 | 29) I have a bunch of shell scripts that use backslash-escaped characters |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 896 | in arguments to `echo'. Bash doesn't interpret these characters. Why |
| 897 | not, and how can I make it understand them? |
| 898 | |
| 899 | This is the behavior of echo on most Unix System V machines. |
| 900 | |
| 901 | The bash builtin `echo' is modelled after the 9th Edition |
| 902 | Research Unix version of `echo'. It does not interpret |
| 903 | backslash-escaped characters in its argument strings by default; |
| 904 | it requires the use of the -e option to enable the |
| 905 | interpretation. The System V echo provides no way to disable the |
| 906 | special characters; the bash echo has a -E option to disable |
| 907 | them. |
| 908 | |
| 909 | There is a configuration option that will make bash behave like |
| 910 | the System V echo and interpret things like `\t' by default. Run |
| 911 | configure with the --enable-usg-echo-default option to turn this |
| 912 | on. Be aware that this will cause some of the tests run when you |
| 913 | type `make tests' to fail. |
| 914 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 915 | 30) 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] | 916 | |
| 917 | This is a consequence of how job control works on Unix. The only |
| 918 | thing that can be suspended is the process group. This is a single |
| 919 | command or pipeline of commands that the shell forks and executes. |
| 920 | |
| 921 | When you run a while or for loop, the only thing that the shell forks |
| 922 | and executes are any commands in the while loop test and commands in |
| 923 | the loop bodies. These, therefore, are the only things that can be |
| 924 | suspended when you type ^Z. |
| 925 | |
| 926 | If you want to be able to stop the entire loop, you need to put it |
| 927 | within parentheses, which will force the loop into a subshell that |
| 928 | may be stopped (and subsequently restarted) as a single unit. |
| 929 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 930 | 31) How can I make the bash `time' reserved word print timing output that |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 931 | looks like the output from my system's /usr/bin/time? |
| 932 | |
| 933 | The bash command timing code looks for a variable `TIMEFORMAT' and |
| 934 | uses its value as a format string to decide how to display the |
| 935 | timing statistics. |
| 936 | |
| 937 | The value of TIMEFORMAT is a string with `%' escapes expanded in a |
| 938 | fashion similar in spirit to printf(3). The manual page explains |
| 939 | the meanings of the escape sequences in the format string. |
| 940 | |
| 941 | If TIMEFORMAT is not set, bash acts as if the following assignment had |
| 942 | been performed: |
| 943 | |
| 944 | TIMEFORMAT=$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS' |
| 945 | |
| 946 | The POSIX.2 default time format (used by `time -p command') is |
| 947 | |
| 948 | TIMEFORMAT=$'real %2R\nuser %2U\nsys %2S' |
| 949 | |
| 950 | The BSD /usr/bin/time format can be emulated with: |
| 951 | |
| 952 | TIMEFORMAT=$'\t%1R real\t%1U user\t%1S sys' |
| 953 | |
| 954 | The System V /usr/bin/time format can be emulated with: |
| 955 | |
| 956 | TIMEFORMAT=$'\nreal\t%1R\nuser\t%1U\nsys\t%1S' |
| 957 | |
| 958 | The ksh format can be emulated with: |
| 959 | |
| 960 | TIMEFORMAT=$'\nreal\t%2lR\nuser\t%2lU\nsys\t%2lS' |
| 961 | |
| 962 | Section F: Things to watch out for on certain Unix versions |
| 963 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 964 | 32) Why can't I use command line editing in my `cmdtool'? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 965 | |
| 966 | The problem is `cmdtool' and bash fighting over the input. When |
| 967 | scrolling is enabled in a cmdtool window, cmdtool puts the tty in |
| 968 | `raw mode' to permit command-line editing using the mouse for |
| 969 | applications that cannot do it themselves. As a result, bash and |
| 970 | cmdtool each try to read keyboard input immediately, with neither |
| 971 | getting enough of it to be useful. |
| 972 | |
| 973 | This mode also causes cmdtool to not implement many of the |
| 974 | terminal functions and control sequences appearing in the |
| 975 | `sun-cmd' termcap entry. For a more complete explanation, see |
| 976 | that file examples/suncmd.termcap in the bash distribution. |
| 977 | |
| 978 | `xterm' is a better choice, and gets along with bash much more |
| 979 | smoothly. |
| 980 | |
| 981 | If you must use cmdtool, you can use the termcap description in |
| 982 | examples/suncmd.termcap. Set the TERMCAP variable to the terminal |
| 983 | description contained in that file, i.e. |
| 984 | |
| 985 | TERMCAP='Mu|sun-cmd:am:bs:km:pt:li#34:co#80:cl=^L:ce=\E[K:cd=\E[J:rs=\E[s:' |
| 986 | |
| 987 | Then export TERMCAP and start a new cmdtool window from that shell. |
| 988 | The bash command-line editing should behave better in the new |
| 989 | cmdtool. If this works, you can put the assignment to TERMCAP |
| 990 | in your bashrc file. |
| 991 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 992 | 33) I built bash on Solaris 2. Why do globbing expansions and filename |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 993 | completion chop off the first few characters of each filename? |
| 994 | |
| 995 | This is the consequence of building bash on SunOS 5 and linking |
| 996 | with the libraries in /usr/ucblib, but using the definitions |
| 997 | and structures from files in /usr/include. |
| 998 | |
| 999 | The actual conflict is between the dirent structure in |
| 1000 | /usr/include/dirent.h and the struct returned by the version of |
| 1001 | `readdir' in libucb.a (a 4.3-BSD style `struct direct'). |
| 1002 | |
| 1003 | Make sure you've got /usr/ccs/bin ahead of /usr/ucb in your $PATH |
| 1004 | when configuring and building bash. This will ensure that you |
| 1005 | use /usr/ccs/bin/cc or acc instead of /usr/ucb/cc and that you |
| 1006 | link with libc before libucb. |
| 1007 | |
| 1008 | If you have installed the Sun C compiler, you may also need to |
| 1009 | put /usr/ccs/bin and /opt/SUNWspro/bin into your $PATH before |
| 1010 | /usr/ucb. |
| 1011 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 1012 | 34) Why does bash dump core after I interrupt username completion or |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1013 | `~user' tilde expansion on a machine running NIS? |
| 1014 | |
| 1015 | This is a famous and long-standing bug in the SunOS YP (sorry, NIS) |
| 1016 | client library, which is part of libc. |
| 1017 | |
| 1018 | The YP library code keeps static state -- a pointer into the data |
| 1019 | returned from the server. When YP initializes itself (setpwent), |
| 1020 | it looks at this pointer and calls free on it if it's non-null. |
| 1021 | So far, so good. |
| 1022 | |
| 1023 | If one of the YP functions is interrupted during getpwent (the |
| 1024 | exact function is interpretwithsave()), and returns NULL, the |
| 1025 | pointer is freed without being reset to NULL, and the function |
| 1026 | returns. The next time getpwent is called, it sees that this |
| 1027 | pointer is non-null, calls free, and the bash free() blows up |
| 1028 | because it's being asked to free freed memory. |
| 1029 | |
| 1030 | The traditional Unix mallocs allow memory to be freed multiple |
| 1031 | times; that's probably why this has never been fixed. You can |
| 1032 | run configure with the `--without-gnu-malloc' option to use |
| 1033 | the C library malloc and avoid the problem. |
| 1034 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 1035 | 35) 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] | 1036 | |
| 1037 | The `@' character is the default `line kill' character in most |
| 1038 | versions of System V, including SVR4.2. You can change this |
| 1039 | character to whatever you want using `stty'. For example, to |
| 1040 | change the line kill character to control-u, type |
| 1041 | |
| 1042 | stty kill ^U |
| 1043 | |
| 1044 | where the `^' and `U' can be two separate characters. |
| 1045 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 1046 | 36) 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] | 1047 | redirection before a subshell command? |
| 1048 | |
| 1049 | The actual command in question is something like |
| 1050 | |
| 1051 | < file ( command ) |
| 1052 | |
| 1053 | According to the grammar given in the POSIX.2 standard, this construct |
| 1054 | is, in fact, a syntax error. Redirections may only precede `simple |
| 1055 | commands'. A subshell construct such as the above is one of the shell's |
| 1056 | `compound commands'. A redirection may only follow a compound command. |
| 1057 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 1058 | The file CWRU/sh-redir-hack in the bash-2.01 distribution is an |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1059 | (unofficial) patch to parse.y that will modify the grammar to |
| 1060 | support this construct. It will not apply with `patch'; you must |
| 1061 | modify parse.y by hand. Note that if you apply this, you must |
| 1062 | recompile with -DREDIRECTION_HACK. This introduces a large |
| 1063 | number of reduce/reduce conflicts into the shell grammar. |
| 1064 | |
| 1065 | Section G: Where do I go from here? |
| 1066 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 1067 | 37) 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] | 1068 | advice? |
| 1069 | |
| 1070 | Use the `bashbug' script to report bugs. It is built and |
| 1071 | installed at the same time as bash. It provides a standard |
| 1072 | template for reporting a problem and automatically includes |
| 1073 | information about your configuration and build environment. |
| 1074 | |
| 1075 | `bashbug' sends its reports to bug-bash@prep.ai.mit.edu, which |
| 1076 | is a large mailing list gatewayed to the usenet newsgroup gnu.bash.bug. |
| 1077 | |
| 1078 | Bug fixes, answers to questions, and announcements of new releases |
| 1079 | are all posted to gnu.bash.bug. Discussions concerning bash features |
| 1080 | and problems also take place there. |
| 1081 | |
| 1082 | To reach the bash maintainers directly, send mail to |
| 1083 | bash-maintainers@prep.ai.mit.edu. |
| 1084 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 1085 | 38) What kind of bash documentation is there? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1086 | |
| 1087 | First, look in the doc directory in the bash distribution. It should |
| 1088 | contain at least the following files: |
| 1089 | |
| 1090 | bash.1 an extensive, thorough Unix-style manual page |
| 1091 | builtins.1 a manual page covering just bash builtin commands |
| 1092 | bashref.texi a reference manual in GNU info format |
| 1093 | bash.html an HTML version of the manual page |
| 1094 | bashref.html an HTML version of the reference manual |
| 1095 | FAQ this file |
| 1096 | article.ms text of an article written for The Linux Journal |
| 1097 | readline.3 a man page describing readline |
| 1098 | |
| 1099 | Postscript files created from the above source are available in |
| 1100 | the documentation distribution. |
| 1101 | |
| 1102 | There is additional documentation available for anonymous FTP from host |
| 1103 | slc2.ins.cwru.edu in the `pub/bash' directory. |
| 1104 | |
| 1105 | Cameron Newham and Bill Rosenblatt have written a book on bash, published |
| 1106 | 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^] | 1107 | Shell book. The title is ``Learning the Bash Shell'', and the ISBN number |
| 1108 | is 1-56592-147-X. Look for it in fine bookstores near you. This book |
| 1109 | covers bash-1.14, but has an appendix describing some of the new features |
| 1110 | in bash-2.0. There are rumors of a second edition of this book, describing |
| 1111 | bash-2.0 (and 2.01). I do not know what ORA's publication schedule for |
| 1112 | this edition is. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1113 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 1114 | 39) What's coming in future versions? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1115 | |
| 1116 | These are features I plan to include in a future version of bash. |
| 1117 | |
| 1118 | POSIX.2-style globbing character classes ([:alpha:], [:alnum:], etc.) |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 1119 | POSIX.2-style globbing equivalence classes |
| 1120 | POSIX.2-style globbing collating symbols |
| 1121 | a bash debugger (a minimally-tested version is included with bash-2.01) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1122 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 1123 | 40) What's on the bash `wish list' for future versions? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1124 | |
| 1125 | These are features that may or may not appear in a future version of bash. |
| 1126 | |
| 1127 | Programmable completion a la zsh |
| 1128 | menu completion a la tcsh |
| 1129 | the ksh [[...]] extended test command |
| 1130 | the ksh egrep-style extended pattern matching operators |
| 1131 | associative arrays (not really all that hard) |
| 1132 | breaking some of the shell functionality into embeddable libraries |
| 1133 | better internationalization using GNU `gettext' |
| 1134 | an option to use external files for the long `help' text |
| 1135 | timeouts for the `read' builtin |
| 1136 | the ksh-93 ${!prefix*} and ${!prefix@} operators |
| 1137 | arithmetic ++ and -- prefix and postfix operators |
| 1138 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame^] | 1139 | 41) When will the next release appear? |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1140 | |
| 1141 | The next version will appear sometime in 1997. Never make predictions. |
| 1142 | |
| 1143 | |
| 1144 | This document is Copyright 1995, 1996 by Chester Ramey. |
| 1145 | |
| 1146 | Permission is hereby granted, without written agreement and |
| 1147 | without license or royalty fees, to use, copy, and distribute |
| 1148 | this document for any purpose, provided that the above copyright |
| 1149 | notice appears in all copies of this document and that the |
| 1150 | contents of this document remain unaltered. |