]>
Commit | Line | Data |
---|---|---|
17345e5a | 1 | This is the Bash FAQ, version 4.01, for Bash version 4.0. |
ccc6cda3 JA |
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 | |
0628567a | 12 | chet.ramey@case.edu. |
ccc6cda3 JA |
13 | |
14 | This document is available for anonymous FTP with the URL | |
15 | ||
cce855bc | 16 | ftp://ftp.cwru.edu/pub/bash/FAQ |
ccc6cda3 | 17 | |
bb70624e JA |
18 | The Bash home page is http://cnswww.cns.cwru.edu/~chet/bash/bashtop.html |
19 | ||
ccc6cda3 JA |
20 | ---------- |
21 | Contents: | |
22 | ||
23 | Section A: The Basics | |
24 | ||
b72432fd JA |
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? | |
b80f6443 | 34 | A9) What's the `POSIX Shell and Utilities standard'? |
b72432fd | 35 | A10) What is the bash `posix mode'? |
ccc6cda3 JA |
36 | |
37 | Section B: The latest version | |
38 | ||
17345e5a JA |
39 | B1) What's new in version 4.0? |
40 | B2) Are there any user-visible incompatibilities between bash-4.0, | |
41 | bash-3.2, and bash-2.05b? | |
ccc6cda3 JA |
42 | |
43 | Section C: Differences from other Unix shells | |
44 | ||
b72432fd JA |
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? | |
ccc6cda3 JA |
48 | |
49 | Section D: Why does bash do some things differently than other Unix shells? | |
50 | ||
b72432fd | 51 | D1) Why does bash run a different version of `command' than |
ccc6cda3 | 52 | `which command' says it will? |
b72432fd JA |
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 | |
ccc6cda3 | 57 | another, like csh does with `|&'? |
b72432fd | 58 | D6) Now that I've converted from ksh to bash, are there equivalents to |
ccc6cda3 JA |
59 | ksh features like autoloaded functions and the `whence' command? |
60 | ||
bb70624e | 61 | Section E: Why does bash do certain things the way it does? |
ccc6cda3 | 62 | |
b72432fd JA |
63 | E1) Why is the bash builtin `test' slightly different from /bin/test? |
64 | E2) Why does bash sometimes say `Broken pipe'? | |
bb70624e | 65 | E3) When I have terminal escape sequences in my prompt, why does bash |
ccc6cda3 | 66 | wrap lines at the wrong column? |
bb70624e | 67 | E4) If I pipe the output of a command into `read variable', why doesn't |
ccc6cda3 | 68 | the output show up in $variable when the read command finishes? |
bb70624e | 69 | E5) I have a bunch of shell scripts that use backslash-escaped characters |
ccc6cda3 JA |
70 | in arguments to `echo'. Bash doesn't interpret these characters. Why |
71 | not, and how can I make it understand them? | |
bb70624e | 72 | E6) Why doesn't a while or for loop get suspended when I type ^Z? |
28ef6c31 JA |
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'? | |
7117c2d2 | 77 | E10) Why does `cd //' leave $PWD as `//'? |
b80f6443 JA |
78 | E11) If I resize my xterm while another program is running, why doesn't bash |
79 | notice the change? | |
80 | E12) Why don't negative offsets in substring expansion work like I expect? | |
0628567a | 81 | E13) Why does filename completion misbehave if a colon appears in the filename? |
3185942a JA |
82 | E14) Why does quoting the pattern argument to the regular expression matching |
83 | conditional operator (=~) cause matching to stop working? | |
ccc6cda3 JA |
84 | |
85 | Section F: Things to watch out for on certain Unix versions | |
86 | ||
b72432fd JA |
87 | F1) Why can't I use command line editing in my `cmdtool'? |
88 | F2) I built bash on Solaris 2. Why do globbing expansions and filename | |
ccc6cda3 | 89 | completion chop off the first few characters of each filename? |
b72432fd | 90 | F3) Why does bash dump core after I interrupt username completion or |
ccc6cda3 | 91 | `~user' tilde expansion on a machine running NIS? |
b72432fd JA |
92 | F4) I'm running SVR4.2. Why is the line erased every time I type `@'? |
93 | F5) Why does bash report syntax errors when my C News scripts use a | |
ccc6cda3 | 94 | redirection before a subshell command? |
bb70624e | 95 | F6) Why can't I use vi-mode editing on Red Hat Linux 6.1? |
7117c2d2 JA |
96 | F7) Why do bash-2.05a and bash-2.05b fail to compile `printf.def' on |
97 | HP/UX 11.x? | |
bb70624e JA |
98 | |
99 | Section G: How can I get bash to do certain common things? | |
100 | ||
101 | G1) How can I get bash to read and display eight-bit characters? | |
102 | G2) How do I write a function `x' to replace builtin command `x', but | |
103 | still invoke the command from within the function? | |
104 | G3) How can I find the value of a shell variable whose name is the value | |
105 | of another shell variable? | |
106 | G4) How can I make the bash `time' reserved word print timing output that | |
107 | looks like the output from my system's /usr/bin/time? | |
108 | G5) How do I get the current directory into my prompt? | |
109 | G6) How can I rename "*.foo" to "*.bar"? | |
110 | G7) How can I translate a filename from uppercase to lowercase? | |
111 | G8) How can I write a filename expansion (globbing) pattern that will match | |
112 | all files in the current directory except "." and ".."? | |
ccc6cda3 | 113 | |
bb70624e | 114 | Section H: Where do I go from here? |
ccc6cda3 | 115 | |
bb70624e | 116 | H1) How do I report bugs in bash, and where should I look for fixes and |
ccc6cda3 | 117 | advice? |
bb70624e JA |
118 | H2) What kind of bash documentation is there? |
119 | H3) What's coming in future versions? | |
120 | H4) What's on the bash `wish list'? | |
121 | H5) When will the next release appear? | |
ccc6cda3 JA |
122 | |
123 | ---------- | |
124 | Section A: The Basics | |
125 | ||
b72432fd | 126 | A1) What is it? |
ccc6cda3 JA |
127 | |
128 | Bash is a Unix command interpreter (shell). It is an implementation of | |
129 | the Posix 1003.2 shell standard, and resembles the Korn and System V | |
130 | shells. | |
131 | ||
132 | Bash contains a number of enhancements over those shells, both | |
133 | for interactive use and shell programming. Features geared | |
134 | toward interactive use include command line editing, command | |
135 | history, job control, aliases, and prompt expansion. Programming | |
136 | features include additional variable expansions, shell | |
137 | arithmetic, and a number of variables and options to control | |
138 | shell behavior. | |
139 | ||
140 | Bash was originally written by Brian Fox of the Free Software | |
141 | Foundation. The current developer and maintainer is Chet Ramey | |
142 | of Case Western Reserve University. | |
143 | ||
b72432fd | 144 | A2) What's the latest version? |
ccc6cda3 | 145 | |
17345e5a | 146 | The latest version is 4.0, first made available on 20 February, 2009. |
ccc6cda3 | 147 | |
b72432fd | 148 | A3) Where can I get it? |
ccc6cda3 JA |
149 | |
150 | Bash is the GNU project's shell, and so is available from the | |
b72432fd | 151 | master GNU archive site, ftp.gnu.org, and its mirrors. The |
cce855bc | 152 | latest version is also available for FTP from ftp.cwru.edu. |
17345e5a | 153 | The following URLs tell how to get version 4.0: |
ccc6cda3 | 154 | |
17345e5a JA |
155 | ftp://ftp.gnu.org/pub/gnu/bash/bash-4.0.tar.gz |
156 | ftp://ftp.cwru.edu/pub/bash/bash-4.0.tar.gz | |
ccc6cda3 JA |
157 | |
158 | Formatted versions of the documentation are available with the URLs: | |
159 | ||
17345e5a JA |
160 | ftp://ftp.gnu.org/pub/gnu/bash/bash-doc-4.0.tar.gz |
161 | ftp://ftp.cwru.edu/pub/bash/bash-doc-4.0.tar.gz | |
95732b49 JA |
162 | |
163 | Any patches for the current version are available with the URL: | |
164 | ||
17345e5a | 165 | ftp://ftp.cwru.edu/pub/bash/bash-4.0-patches/ |
ccc6cda3 | 166 | |
b72432fd | 167 | A4) On what machines will bash run? |
ccc6cda3 | 168 | |
b80f6443 | 169 | Bash has been ported to nearly every version of Unix. All you |
ccc6cda3 JA |
170 | should have to do to build it on a machine for which a port |
171 | exists is to type `configure' and then `make'. The build process | |
b80f6443 | 172 | will attempt to discover the version of Unix you have and tailor |
ccc6cda3 JA |
173 | itself accordingly, using a script created by GNU autoconf. |
174 | ||
175 | More information appears in the file `INSTALL' in the distribution. | |
176 | ||
f73dda09 JA |
177 | The Bash web page (http://cnswww.cns.cwru.edu/~chet/bash/bashtop.html) |
178 | explains how to obtain binary versions of bash for most of the major | |
179 | commercial Unix systems. | |
180 | ||
b72432fd | 181 | A5) Will bash run on operating systems other than Unix? |
d166f048 JA |
182 | |
183 | Configuration specifics for Unix-like systems such as QNX and | |
f73dda09 JA |
184 | LynxOS are included in the distribution. Bash-2.05 and later |
185 | versions should compile and run on Minix 2.0 (patches were | |
186 | contributed), but I don't believe anyone has built bash-2.x on | |
187 | earlier Minix versions yet. | |
d166f048 JA |
188 | |
189 | Bash has been ported to versions of Windows implementing the Win32 | |
190 | programming interface. This includes Windows 95 and Windows NT. | |
95732b49 JA |
191 | The port was done by Cygnus Solutions (now part of Red Hat) as part |
192 | of their CYGWIN project. For more information about the project, see | |
193 | http://www.cygwin.com/. | |
d166f048 | 194 | |
b72432fd | 195 | Cygnus originally ported bash-1.14.7, and that port was part of their |
95732b49 | 196 | early GNU-Win32 (the original name) releases. Cygnus has also done |
17345e5a JA |
197 | ports of bash-2.05b and bash-3.2 to the CYGWIN environment, and both |
198 | are available as part of their current release. | |
cce855bc | 199 | |
b80f6443 JA |
200 | Bash-2.05b and later versions should require no local Cygnus changes to |
201 | build and run under CYGWIN. | |
cce855bc | 202 | |
f73dda09 | 203 | DJ Delorie has a port of bash-2.x which runs under MS-DOS, as part |
28ef6c31 | 204 | of the DJGPP project. For more information on the project, see |
d166f048 JA |
205 | |
206 | http://www.delorie.com/djgpp/ | |
207 | ||
28ef6c31 JA |
208 | I have been told that the original DJGPP port was done by Daisuke Aoyama. |
209 | ||
f73dda09 JA |
210 | Mark Elbrecht <snowball3@bigfoot.com> has sent me notice that bash-2.04 |
211 | is available for DJGPP V2. The files are available as: | |
bb70624e | 212 | |
f73dda09 JA |
213 | ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh204b.zip binary |
214 | ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh204d.zip documentation | |
215 | ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh204s.zip source | |
bb70624e | 216 | |
b80f6443 | 217 | Mark began to work with bash-2.05, but I don't know the current status. |
d166f048 | 218 | |
b80f6443 | 219 | Bash-3.0 compiles and runs with no modifications under Microsoft's Services |
95732b49 | 220 | for Unix (SFU), once known as Interix. I do not anticipate any problems |
17345e5a | 221 | with building bash-4.0, but will gladly accept any patches that are needed. |
bb70624e | 222 | |
b72432fd | 223 | A6) How can I build bash with gcc? |
ccc6cda3 JA |
224 | |
225 | Bash configures to use gcc by default if it is available. Read the | |
226 | file INSTALL in the distribution for more information. | |
227 | ||
b72432fd | 228 | A7) How can I make bash my login shell? |
ccc6cda3 JA |
229 | |
230 | Some machines let you use `chsh' to change your login shell. Other | |
cce855bc JA |
231 | systems use `passwd -s' or `passwd -e'. If one of these works for |
232 | you, that's all you need. Note that many systems require the full | |
233 | pathname to a shell to appear in /etc/shells before you can make it | |
234 | your login shell. For this, you may need the assistance of your | |
235 | friendly local system administrator. | |
ccc6cda3 JA |
236 | |
237 | If you cannot do this, you can still use bash as your login shell, but | |
238 | you need to perform some tricks. The basic idea is to add a command | |
239 | to your login shell's startup file to replace your login shell with | |
240 | bash. | |
241 | ||
242 | For example, if your login shell is csh or tcsh, and you have installed | |
243 | bash in /usr/gnu/bin/bash, add the following line to ~/.login: | |
244 | ||
245 | if ( -f /usr/gnu/bin/bash ) exec /usr/gnu/bin/bash --login | |
246 | ||
247 | (the `--login' tells bash that it is a login shell). | |
248 | ||
249 | It's not a good idea to put this command into ~/.cshrc, because every | |
250 | csh you run without the `-f' option, even ones started to run csh scripts, | |
251 | reads that file. If you must put the command in ~/.cshrc, use something | |
252 | like | |
253 | ||
254 | if ( $?prompt ) exec /usr/gnu/bin/bash --login | |
255 | ||
256 | to ensure that bash is exec'd only when the csh is interactive. | |
257 | ||
d166f048 | 258 | If your login shell is sh or ksh, you have to do two things. |
ccc6cda3 | 259 | |
d166f048 | 260 | First, create an empty file in your home directory named `.bash_profile'. |
ccc6cda3 JA |
261 | The existence of this file will prevent the exec'd bash from trying to |
262 | read ~/.profile, and re-execing itself over and over again. ~/.bash_profile | |
d166f048 JA |
263 | is the first file bash tries to read initialization commands from when |
264 | it is invoked as a login shell. | |
265 | ||
266 | Next, add a line similar to the above to ~/.profile: | |
267 | ||
b80f6443 JA |
268 | [ -f /usr/gnu/bin/bash ] && [ -x /usr/gnu/bin/bash ] && \ |
269 | exec /usr/gnu/bin/bash --login | |
ccc6cda3 | 270 | |
d166f048 JA |
271 | This will cause login shells to replace themselves with bash running as |
272 | a login shell. Once you have this working, you can copy your initialization | |
273 | code from ~/.profile to ~/.bash_profile. | |
274 | ||
bb70624e JA |
275 | I have received word that the recipe supplied above is insufficient for |
276 | machines running CDE. CDE has a maze of twisty little startup files, all | |
277 | slightly different. | |
278 | ||
279 | If you cannot change your login shell in the password file to bash, you | |
280 | will have to (apparently) live with CDE using the shell in the password | |
281 | file to run its startup scripts. If you have changed your shell to bash, | |
f73dda09 JA |
282 | there is code in the CDE startup files (on Solaris, at least) that attempts |
283 | to do the right thing. It is, however, often broken, and may require that | |
284 | you use the $BASH_ENV trick described below. | |
bb70624e JA |
285 | |
286 | `dtterm' claims to use $SHELL as the default program to start, so if you | |
287 | can change $SHELL in the CDE startup files, you should be able to use bash | |
288 | in your terminal windows. | |
289 | ||
290 | Setting DTSOURCEPROFILE in ~/.dtprofile will cause the `Xsession' program | |
291 | to read your login shell's startup files. You may be able to use bash for | |
292 | the rest of the CDE programs by setting SHELL to bash in ~/.dtprofile as | |
293 | well, but I have not tried this. | |
294 | ||
295 | You can use the above `exec' recipe to start bash when not logging in with | |
296 | CDE by testing the value of the DT variable: | |
297 | ||
298 | if [ -n "$DT" ]; then | |
299 | [ -f /usr/gnu/bin/bash ] && exec /usr/gnu/bin/bash --login | |
300 | fi | |
301 | ||
f73dda09 JA |
302 | If CDE starts its shells non-interactively during login, the login shell |
303 | startup files (~/.profile, ~/.bash_profile) will not be sourced at login. | |
304 | To get around this problem, append a line similar to the following to your | |
305 | ~/.dtprofile: | |
306 | ||
307 | BASH_ENV=${HOME}/.bash_profile ; export BASH_ENV | |
308 | ||
309 | and add the following line to the beginning of ~/.bash_profile: | |
310 | ||
311 | unset BASH_ENV | |
bb70624e | 312 | |
b72432fd | 313 | A8) I just changed my login shell to bash, and now I can't FTP into my |
ccc6cda3 JA |
314 | machine. Why not? |
315 | ||
316 | You must add the full pathname to bash to the file /etc/shells. As | |
317 | noted in the answer to the previous question, many systems require | |
318 | this before you can make bash your login shell. | |
319 | ||
320 | Most versions of ftpd use this file to prohibit `special' users | |
321 | such as `uucp' and `news' from using FTP. | |
322 | ||
b80f6443 | 323 | A9) What's the `POSIX Shell and Utilities standard'? |
ccc6cda3 JA |
324 | |
325 | POSIX is a name originally coined by Richard Stallman for a | |
326 | family of open system standards based on UNIX. There are a | |
327 | number of aspects of UNIX under consideration for | |
328 | standardization, from the basic system services at the system | |
329 | call and C library level to applications and tools to system | |
330 | administration and management. Each area of standardization is | |
331 | assigned to a working group in the 1003 series. | |
332 | ||
b80f6443 JA |
333 | The POSIX Shell and Utilities standard was originally developed by |
334 | IEEE Working Group 1003.2 (POSIX.2). Today it has been merged with | |
335 | the original 1003.1 Working Group and is maintained by the Austin | |
336 | Group (a joint working group of the IEEE, The Open Group and | |
337 | ISO/IEC SC22/WG15). Today the Shell and Utilities are a volume | |
338 | within the set of documents that make up IEEE Std 1003.1-2001, and | |
339 | thus now the former POSIX.2 (from 1992) is now part of the current | |
340 | POSIX.1 standard (POSIX 1003.1-2001). | |
341 | ||
342 | The Shell and Utilities volume concentrates on the command | |
ccc6cda3 | 343 | interpreter interface and utility programs commonly executed from |
b80f6443 JA |
344 | the command line or by other programs. The standard is freely |
345 | available on the web at http://www.UNIX-systems.org/version3/ . | |
346 | Work continues at the Austin Group on maintenance issues; see | |
347 | http://www.opengroup.org/austin/ to join the discussions. | |
ccc6cda3 | 348 | |
b80f6443 JA |
349 | Bash is concerned with the aspects of the shell's behavior defined |
350 | by the POSIX Shell and Utilities volume. The shell command | |
351 | language has of course been standardized, including the basic flow | |
352 | control and program execution constructs, I/O redirection and | |
353 | pipelining, argument handling, variable expansion, and quoting. | |
ccc6cda3 JA |
354 | |
355 | The `special' builtins, which must be implemented as part of the | |
356 | shell to provide the desired functionality, are specified as | |
357 | being part of the shell; examples of these are `eval' and | |
b80f6443 | 358 | `export'. Other utilities appear in the sections of POSIX not |
ccc6cda3 JA |
359 | devoted to the shell which are commonly (and in some cases must |
360 | be) implemented as builtin commands, such as `read' and `test'. | |
b80f6443 | 361 | POSIX also specifies aspects of the shell's interactive |
ccc6cda3 JA |
362 | behavior as part of the UPE, including job control and command |
363 | line editing. Only vi-style line editing commands have been | |
364 | standardized; emacs editing commands were left out due to | |
365 | objections. | |
366 | ||
b80f6443 JA |
367 | The latest version of the POSIX Shell and Utilities standard is |
368 | available (now updated to the 2004 Edition) as part of the Single | |
369 | UNIX Specification Version 3 at | |
7117c2d2 | 370 | |
b80f6443 | 371 | http://www.UNIX-systems.org/version3/ |
7117c2d2 | 372 | |
b72432fd | 373 | A10) What is the bash `posix mode'? |
ccc6cda3 | 374 | |
b80f6443 | 375 | Although bash is an implementation of the POSIX shell |
ccc6cda3 JA |
376 | specification, there are areas where the bash default behavior |
377 | differs from that spec. The bash `posix mode' changes the bash | |
378 | behavior in these areas so that it obeys the spec more closely. | |
379 | ||
7117c2d2 JA |
380 | Posix mode is entered by starting bash with the --posix or |
381 | '-o posix' option or executing `set -o posix' after bash is running. | |
ccc6cda3 JA |
382 | |
383 | The specific aspects of bash which change when posix mode is | |
7117c2d2 JA |
384 | active are listed in the file POSIX in the bash distribution. |
385 | They are also listed in a section in the Bash Reference Manual | |
386 | (from which that file is generated). | |
ccc6cda3 JA |
387 | |
388 | Section B: The latest version | |
389 | ||
17345e5a | 390 | B1) What's new in version 4.0? |
95732b49 | 391 | |
17345e5a JA |
392 | Bash-4.0 is the fourth major release of bash. There are numerous new features, |
393 | some experimental. Depending on community reception, the experimental | |
394 | features will evolve. | |
395 | ||
396 | Bash-4.0 contains the following new features (see the manual page for | |
397 | complete descriptions and the CHANGES and NEWS files in the bash-4.0 | |
398 | distribution): | |
399 | ||
400 | o When using substring expansion on the positional parameters, a starting | |
401 | index of 0 now causes $0 to be prefixed to the list. | |
402 | ||
403 | o There is a new variable, $BASHPID, which always returns the process id of | |
404 | the current shell. | |
405 | ||
406 | o There is a new `autocd' option that, when enabled, causes bash to attempt | |
407 | to `cd' to a directory name that is supplied as the first word of a | |
408 | simple command. | |
409 | ||
410 | o There is a new `checkjobs' option that causes the shell to check for and | |
411 | report any running or stopped jobs at exit. | |
412 | ||
413 | o The programmable completion code exports a new COMP_TYPE variable, set to | |
414 | a character describing the type of completion being attempted. | |
415 | ||
416 | o The programmable completion code exports a new COMP_KEY variable, set to | |
417 | the character that caused the completion to be invoked (e.g., TAB). | |
418 | ||
419 | o The programmable completion code now uses the same set of characters as | |
420 | readline when breaking the command line into a list of words. | |
421 | ||
422 | o The block multiplier for the ulimit -c and -f options is now 512 when in | |
423 | Posix mode, as Posix specifies. | |
424 | ||
425 | o Changed the behavior of the read builtin to save any partial input received | |
426 | in the specified variable when the read builtin times out. This also | |
427 | results in variables specified as arguments to read to be set to the empty | |
428 | string when there is no input available. When the read builtin times out, | |
429 | it returns an exit status greater than 128. | |
430 | ||
431 | o The shell now has the notion of a `compatibility level', controlled by | |
432 | new variables settable by `shopt'. Setting this variable currently | |
433 | restores the bash-3.1 behavior when processing quoted strings on the rhs | |
434 | of the `=~' operator to the `[[' command. | |
435 | ||
436 | o The `ulimit' builtin now has new -b (socket buffer size) and -T (number | |
437 | of threads) options. | |
438 | ||
439 | o There is a new `compopt' builtin that allows completion functions to modify | |
440 | completion options for existing completions or the completion currently | |
441 | being executed. | |
442 | ||
443 | o The `read' builtin has a new -i option which inserts text into the reply | |
444 | buffer when using readline. | |
445 | ||
446 | o A new `-E' option to the complete builtin allows control of the default | |
447 | behavior for completion on an empty line. | |
448 | ||
449 | o There is now limited support for completing command name words containing | |
450 | globbing characters. | |
451 | ||
452 | o The `help' builtin now has a new -d option, to display a short description, | |
453 | and a -m option, to print help information in a man page-like format. | |
454 | ||
455 | o There is a new `mapfile' builtin to populate an array with lines from a | |
456 | given file. | |
457 | ||
458 | o If a command is not found, the shell attempts to execute a shell function | |
459 | named `command_not_found_handle', supplying the command words as the | |
460 | function arguments. | |
461 | ||
462 | o There is a new shell option: `globstar'. When enabled, the globbing code | |
463 | treats `**' specially -- it matches all directories (and files within | |
464 | them, when appropriate) recursively. | |
465 | ||
466 | o There is a new shell option: `dirspell'. When enabled, the filename | |
467 | completion code performs spelling correction on directory names during | |
468 | completion. | |
469 | ||
470 | o The `-t' option to the `read' builtin now supports fractional timeout | |
471 | values. | |
472 | ||
473 | o Brace expansion now allows zero-padding of expanded numeric values and | |
474 | will add the proper number of zeroes to make sure all values contain the | |
475 | same number of digits. | |
476 | ||
477 | o There is a new bash-specific bindable readline function: `dabbrev-expand'. | |
478 | It uses menu completion on a set of words taken from the history list. | |
479 | ||
480 | o The command assigned to a key sequence with `bind -x' now sets two new | |
481 | variables in the environment of the executed command: READLINE_LINE_BUFFER | |
482 | and READLINE_POINT. The command can change the current readline line | |
483 | and cursor position by modifying READLINE_LINE_BUFFER and READLINE_POINT, | |
484 | respectively. | |
485 | ||
486 | o There is a new >>& redirection operator, which appends the standard output | |
487 | and standard error to the named file. | |
488 | ||
489 | o The parser now understands `|&' as a synonym for `2>&1 |', which redirects | |
490 | the standard error for a command through a pipe. | |
491 | ||
492 | o The new `;&' case statement action list terminator causes execution to | |
493 | continue with the action associated with the next pattern in the | |
494 | statement rather than terminating the command. | |
495 | ||
496 | o The new `;;&' case statement action list terminator causes the shell to | |
497 | test the next set of patterns after completing execution of the current | |
498 | action, rather than terminating the command. | |
499 | ||
500 | o The shell understands a new variable: PROMPT_DIRTRIM. When set to an | |
501 | integer value greater than zero, prompt expansion of \w and \W will | |
502 | retain only that number of trailing pathname components and replace | |
503 | the intervening characters with `...'. | |
504 | ||
505 | o There are new case-modifying word expansions: uppercase (^[^]) and | |
506 | lowercase (,[,]). They can work on either the first character or | |
507 | array element, or globally. They accept an optional shell pattern | |
508 | that determines which characters to modify. There is an optionally- | |
509 | configured feature to include capitalization operators. | |
510 | ||
511 | o The shell provides associative array variables, with the appropriate | |
512 | support to create, delete, assign values to, and expand them. | |
513 | ||
514 | o The `declare' builtin now has new -l (convert value to lowercase upon | |
515 | assignment) and -u (convert value to uppercase upon assignment) options. | |
516 | There is an optionally-configurable -c option to capitalize a value at | |
517 | assignment. | |
518 | ||
519 | o There is a new `coproc' reserved word that specifies a coprocess: an | |
520 | asynchronous command run with two pipes connected to the creating shell. | |
521 | Coprocs can be named. The input and output file descriptors and the | |
522 | PID of the coprocess are available to the calling shell in variables | |
523 | with coproc-specific names. | |
524 | ||
525 | o A value of 0 for the -t option to `read' now returns success if there is | |
526 | input available to be read from the specified file descriptor. | |
527 | ||
528 | o CDPATH and GLOBIGNORE are ignored when the shell is running in privileged | |
529 | mode. | |
530 | ||
531 | o New bindable readline functions shell-forward-word and shell-backward-word, | |
532 | which move forward and backward words delimited by shell metacharacters | |
533 | and honor shell quoting. | |
534 | ||
535 | o New bindable readline functions shell-backward-kill-word and shell-kill-word | |
536 | which kill words backward and forward, but use the same word boundaries | |
537 | as shell-forward-word and shell-backward-word. | |
538 | ||
539 | A short feature history dating from Bash-2.0: | |
540 | ||
541 | Bash-3.2 contained the following new features: | |
0628567a JA |
542 | |
543 | o Bash-3.2 now checks shell scripts for NUL characters rather than non-printing | |
544 | characters when deciding whether or not a script is a binary file. | |
545 | ||
546 | o Quoting the string argument to the [[ command's =~ (regexp) operator now | |
547 | forces string matching, as with the other pattern-matching operators. | |
548 | ||
0628567a | 549 | Bash-3.1 contained the following new features: |
95732b49 JA |
550 | |
551 | o Bash-3.1 may now be configured and built in a mode that enforces strict | |
552 | POSIX compliance. | |
f73dda09 | 553 | |
95732b49 JA |
554 | o The `+=' assignment operator, which appends to the value of a string or |
555 | array variable, has been implemented. | |
bb70624e | 556 | |
95732b49 JA |
557 | o It is now possible to ignore case when matching in contexts other than |
558 | filename generation using the new `nocasematch' shell option. | |
559 | ||
95732b49 | 560 | Bash-3.0 contained the following new features: |
bb70624e | 561 | |
b80f6443 JA |
562 | o Features to support the bash debugger have been implemented, and there |
563 | is a new `extdebug' option to turn the non-default options on | |
564 | ||
565 | o HISTCONTROL is now a colon-separated list of options and has been | |
566 | extended with a new `erasedups' option that will result in only one | |
567 | copy of a command being kept in the history list | |
568 | ||
569 | o Brace expansion has been extended with a new {x..y} form, producing | |
570 | sequences of digits or characters | |
571 | ||
572 | o Timestamps are now kept with history entries, with an option to save | |
573 | and restore them from the history file; there is a new HISTTIMEFORMAT | |
574 | variable describing how to display the timestamps when listing history | |
575 | entries | |
576 | ||
577 | o The `[[' command can now perform extended regular expression (egrep-like) | |
578 | matching, with matched subexpressions placed in the BASH_REMATCH array | |
579 | variable | |
580 | ||
581 | o A new `pipefail' option causes a pipeline to return a failure status if | |
582 | any command in it fails | |
583 | ||
584 | o The `jobs', `kill', and `wait' builtins now accept job control notation | |
585 | in their arguments even if job control is not enabled | |
586 | ||
587 | o The `gettext' package and libintl have been integrated, and the shell | |
588 | messages may be translated into other languages | |
589 | ||
b80f6443 JA |
590 | Bash-2.05b introduced the following new features: |
591 | ||
7117c2d2 JA |
592 | o support for multibyte characters has been added to both bash and readline |
593 | ||
594 | o the DEBUG trap is now run *before* simple commands, ((...)) commands, | |
595 | [[...]] conditional commands, and for ((...)) loops | |
596 | ||
597 | o the shell now performs arithmetic in the largest integer size the machine | |
598 | supports (intmax_t) | |
599 | ||
600 | o there is a new \D{...} prompt expansion; passes the `...' to strftime(3) | |
601 | and inserts the result into the expanded prompt | |
602 | ||
603 | o there is a new `here-string' redirection operator: <<< word | |
604 | ||
605 | o when displaying variables, function attributes and definitions are shown | |
606 | separately, allowing them to be re-used as input (attempting to re-use | |
607 | the old output would result in syntax errors). | |
608 | ||
609 | o `read' has a new `-u fd' option to read from a specified file descriptor | |
610 | ||
611 | o the bash debugger in examples/bashdb has been modified to work with the | |
612 | new DEBUG trap semantics, the command set has been made more gdb-like, | |
613 | and the changes to $LINENO make debugging functions work better | |
614 | ||
615 | o the expansion of $LINENO inside a shell function is only relative to the | |
616 | function start if the shell is interactive -- if the shell is running a | |
617 | script, $LINENO expands to the line number in the script. This is as | |
618 | POSIX-2001 requires | |
619 | ||
7117c2d2 JA |
620 | Bash-2.05a introduced the following new features: |
621 | ||
f73dda09 JA |
622 | o The `printf' builtin has undergone major work |
623 | ||
624 | o There is a new read-only `shopt' option: login_shell, which is set by | |
625 | login shells and unset otherwise | |
626 | ||
627 | o New `\A' prompt string escape sequence; expanding to time in 24-hour | |
628 | HH:MM format | |
629 | ||
630 | o New `-A group/-g' option to complete and compgen; goes group name | |
631 | completion | |
632 | ||
633 | o New [+-]O invocation option to set and unset `shopt' options at startup | |
634 | ||
635 | o ksh-like `ERR' trap | |
636 | ||
637 | o `for' loops now allow empty word lists after the `in' reserved word | |
638 | ||
639 | o new `hard' and `soft' arguments for the `ulimit' builtin | |
640 | ||
641 | o Readline can be configured to place the user at the same point on the line | |
642 | when retrieving commands from the history list | |
643 | ||
644 | o Readline can be configured to skip `hidden' files (filenames with a leading | |
645 | `.' on Unix) when performing completion | |
646 | ||
f73dda09 JA |
647 | Bash-2.05 introduced the following new features: |
648 | ||
28ef6c31 JA |
649 | o This version has once again reverted to using locales and strcoll(3) when |
650 | processing pattern matching bracket expressions, as POSIX requires. | |
651 | o Added a new `--init-file' invocation argument as a synonym for `--rcfile', | |
652 | per the new GNU coding standards. | |
653 | o The /dev/tcp and /dev/udp redirections now accept service names as well as | |
654 | port numbers. | |
655 | o `complete' and `compgen' now take a `-o value' option, which controls some | |
656 | of the aspects of that compspec. Valid values are: | |
657 | ||
658 | default - perform bash default completion if programmable | |
659 | completion produces no matches | |
660 | dirnames - perform directory name completion if programmable | |
661 | completion produces no matches | |
662 | filenames - tell readline that the compspec produces filenames, | |
663 | so it can do things like append slashes to | |
664 | directory names and suppress trailing spaces | |
665 | o A new loadable builtin, realpath, which canonicalizes and expands symlinks | |
666 | in pathname arguments. | |
667 | o When `set' is called without options, it prints function defintions in a | |
668 | way that allows them to be reused as input. This affects `declare' and | |
669 | `declare -p' as well. This only happens when the shell is not in POSIX | |
670 | mode, since POSIX.2 forbids this behavior. | |
671 | ||
28ef6c31 JA |
672 | Bash-2.04 introduced the following new features: |
673 | ||
bb70624e JA |
674 | o Programmable word completion with the new `complete' and `compgen' builtins; |
675 | examples are provided in examples/complete/complete-examples | |
676 | o `history' has a new `-d' option to delete a history entry | |
677 | o `bind' has a new `-x' option to bind key sequences to shell commands | |
678 | o The prompt expansion code has new `\j' and `\l' escape sequences | |
28ef6c31 | 679 | o The `no_empty_cmd_completion' shell option, if enabled, inhibits |
bb70624e JA |
680 | command completion when TAB is typed on an empty line |
681 | o `help' has a new `-s' option to print a usage synopsis | |
682 | o New arithmetic operators: var++, var--, ++var, --var, expr1,expr2 (comma) | |
683 | o New ksh93-style arithmetic for command: | |
684 | for ((expr1 ; expr2; expr3 )); do list; done | |
685 | o `read' has new options: `-t', `-n', `-d', `-s' | |
686 | o The redirection code handles several filenames specially: /dev/fd/N, | |
687 | /dev/stdin, /dev/stdout, /dev/stderr | |
688 | o The redirection code now recognizes /dev/tcp/HOST/PORT and | |
689 | /dev/udp/HOST/PORT and tries to open a TCP or UDP socket, respectively, | |
690 | to the specified port on the specified host | |
691 | o The ${!prefix*} expansion has been implemented | |
692 | o A new FUNCNAME variable, which expands to the name of a currently-executing | |
693 | function | |
694 | o The GROUPS variable is no longer readonly | |
695 | o A new shopt `xpg_echo' variable, to control the behavior of echo with | |
696 | respect to backslash-escape sequences at runtime | |
697 | o The NON_INTERACTIVE_LOGIN_SHELLS #define has returned | |
698 | ||
28ef6c31 | 699 | The version of Readline released with Bash-2.04, Readline-4.1, had several |
bb70624e JA |
700 | new features as well: |
701 | ||
702 | o Parentheses matching is always compiled into readline, and controllable | |
703 | with the new `blink-matching-paren' variable | |
704 | o The history-search-forward and history-search-backward functions now leave | |
705 | point at the end of the line when the search string is empty, like | |
706 | reverse-search-history, and forward-search-history | |
707 | o A new function for applications: rl_on_new_line_with_prompt() | |
708 | o New variables for applications: rl_already_prompted, and rl_gnu_readline_p | |
709 | ||
710 | ||
bb70624e | 711 | Bash-2.03 had very few new features, in keeping with the convention |
b72432fd JA |
712 | that odd-numbered releases provide mainly bug fixes. A number of new |
713 | features were added to Readline, mostly at the request of the Cygnus | |
714 | folks. | |
cce855bc | 715 | |
bb70624e | 716 | A new shopt option, `restricted_shell', so that startup files can test |
b72432fd | 717 | whether or not the shell was started in restricted mode |
bb70624e | 718 | Filename generation is now performed on the words between ( and ) in |
b72432fd JA |
719 | compound array assignments (this is really a bug fix) |
720 | OLDPWD is now auto-exported, as POSIX.2 requires | |
721 | ENV and BASH_ENV are read-only variables in a restricted shell | |
722 | Bash may now be linked against an already-installed Readline library, | |
723 | as long as the Readline library is version 4 or newer | |
724 | All shells begun with the `--login' option will source the login shell | |
725 | startup files, even if the shell is not interactive | |
726 | ||
bb70624e | 727 | There were lots of changes to the version of the Readline library released |
b72432fd JA |
728 | along with Bash-2.03. For a complete list of the changes, read the file |
729 | CHANGES in the Bash-2.03 distribution. | |
730 | ||
731 | Bash-2.02 contained the following new features: | |
d166f048 | 732 | |
cce855bc JA |
733 | a new version of malloc (based on the old GNU malloc code in previous |
734 | bash versions) that is more page-oriented, more conservative | |
735 | with memory usage, does not `orphan' large blocks when they | |
736 | are freed, is usable on 64-bit machines, and has allocation | |
737 | checking turned on unconditionally | |
738 | POSIX.2-style globbing character classes ([:alpha:], [:alnum:], etc.) | |
739 | POSIX.2-style globbing equivalence classes | |
740 | POSIX.2-style globbing collating symbols | |
741 | the ksh [[...]] extended conditional command | |
742 | the ksh egrep-style extended pattern matching operators | |
743 | a new `printf' builtin | |
744 | the ksh-like $(<filename) command substitution, which is equivalent to | |
745 | $(cat filename) | |
746 | new tilde prefixes that expand to directories from the directory stack | |
747 | new `**' arithmetic operator to do exponentiation | |
748 | case-insensitive globbing (filename expansion) | |
749 | menu completion a la tcsh | |
750 | `magic-space' history expansion function like tcsh | |
751 | the readline inputrc `language' has a new file inclusion directive ($include) | |
752 | ||
753 | Bash-2.01 contained only a few new features: | |
d166f048 JA |
754 | |
755 | new `GROUPS' builtin array variable containing the user's group list | |
756 | new bindable readline commands: history-and-alias-expand-line and | |
757 | alias-expand-line | |
ccc6cda3 | 758 | |
cce855bc | 759 | Bash-2.0 contained extensive changes and new features from bash-1.14.7. |
d166f048 | 760 | Here's a short list: |
ccc6cda3 JA |
761 | |
762 | new `time' reserved word to time pipelines, shell builtins, and | |
763 | shell functions | |
764 | one-dimensional arrays with a new compound assignment statement, | |
765 | appropriate expansion constructs and modifications to some | |
766 | of the builtins (read, declare, etc.) to use them | |
767 | new quoting syntaxes for ANSI-C string expansion and locale-specific | |
768 | string translation | |
769 | new expansions to do substring extraction, pattern replacement, and | |
770 | indirect variable expansion | |
771 | new builtins: `disown' and `shopt' | |
772 | new variables: HISTIGNORE, SHELLOPTS, PIPESTATUS, DIRSTACK, GLOBIGNORE, | |
773 | MACHTYPE, BASH_VERSINFO | |
774 | special handling of many unused or redundant variables removed | |
775 | (e.g., $notify, $glob_dot_filenames, $no_exit_on_failed_exec) | |
776 | dynamic loading of new builtin commands; many loadable examples provided | |
777 | new prompt expansions: \a, \e, \n, \H, \T, \@, \v, \V | |
778 | history and aliases available in shell scripts | |
779 | new readline variables: enable-keypad, mark-directories, input-meta, | |
780 | visible-stats, disable-completion, comment-begin | |
781 | new readline commands to manipulate the mark and operate on the region | |
782 | new readline emacs mode commands and bindings for ksh-88 compatibility | |
783 | updated and extended builtins | |
784 | new DEBUG trap | |
785 | expanded (and now documented) restricted shell mode | |
786 | ||
787 | implementation stuff: | |
788 | autoconf-based configuration | |
789 | nearly all of the bugs reported since version 1.14 have been fixed | |
790 | most builtins converted to use builtin `getopt' for consistency | |
791 | most builtins use -p option to display output in a reusable form | |
792 | (for consistency) | |
793 | grammar tighter and smaller (66 reduce-reduce conflicts gone) | |
794 | lots of code now smaller and faster | |
795 | test suite greatly expanded | |
796 | ||
17345e5a JA |
797 | B2) Are there any user-visible incompatibilities between bash-4.0, bash-3.2, |
798 | and bash-2.05b? | |
ccc6cda3 | 799 | |
17345e5a | 800 | There are a few incompatibilities between version 4.0 and version 3.2. |
7117c2d2 JA |
801 | They are detailed in the file COMPAT in the bash distribution. That file |
802 | is not meant to be all-encompassing; send mail to bash-maintainers@gnu.org | |
803 | if if you find something that's not mentioned there. | |
ccc6cda3 JA |
804 | |
805 | Section C: Differences from other Unix shells | |
806 | ||
b72432fd | 807 | C1) How does bash differ from sh, the Bourne shell? |
ccc6cda3 JA |
808 | |
809 | This is a non-comprehensive list of features that differentiate bash | |
810 | from the SVR4.2 shell. The bash manual page explains these more | |
811 | completely. | |
812 | ||
813 | Things bash has that sh does not: | |
814 | long invocation options | |
f73dda09 | 815 | [+-]O invocation option |
7117c2d2 | 816 | -l invocation option |
ccc6cda3 JA |
817 | `!' reserved word to invert pipeline return value |
818 | `time' reserved word to time pipelines and shell builtins | |
819 | the `function' reserved word | |
bb70624e JA |
820 | the `select' compound command and reserved word |
821 | arithmetic for command: for ((expr1 ; expr2; expr3 )); do list; done | |
ccc6cda3 JA |
822 | new $'...' and $"..." quoting |
823 | the $(...) form of command substitution | |
bc4cd23c JA |
824 | the $(<filename) form of command substitution, equivalent to |
825 | $(cat filename) | |
ccc6cda3 JA |
826 | the ${#param} parameter value length operator |
827 | the ${!param} indirect parameter expansion operator | |
bb70624e | 828 | the ${!param*} prefix expansion operator |
7117c2d2 | 829 | the ${param:offset[:length]} parameter substring operator |
ccc6cda3 JA |
830 | the ${param/pat[/string]} parameter pattern substitution operator |
831 | expansions to perform substring removal (${p%[%]w}, ${p#[#]w}) | |
832 | expansion of positional parameters beyond $9 with ${num} | |
17345e5a | 833 | variables: BASH, BASHPID, BASH_VERSION, BASH_VERSINFO, UID, EUID, REPLY, |
ccc6cda3 JA |
834 | TIMEFORMAT, PPID, PWD, OLDPWD, SHLVL, RANDOM, SECONDS, |
835 | LINENO, HISTCMD, HOSTTYPE, OSTYPE, MACHTYPE, HOSTNAME, | |
836 | ENV, PS3, PS4, DIRSTACK, PIPESTATUS, HISTSIZE, HISTFILE, | |
bc4cd23c | 837 | HISTFILESIZE, HISTCONTROL, HISTIGNORE, GLOBIGNORE, GROUPS, |
ccc6cda3 | 838 | PROMPT_COMMAND, FCEDIT, FIGNORE, IGNOREEOF, INPUTRC, |
bb70624e | 839 | SHELLOPTS, OPTERR, HOSTFILE, TMOUT, FUNCNAME, histchars, |
17345e5a | 840 | auto_resume, PROMPT_DIRTRIM |
ccc6cda3 | 841 | DEBUG trap |
f73dda09 | 842 | ERR trap |
ccc6cda3 | 843 | variable arrays with new compound assignment syntax |
17345e5a | 844 | redirections: <>, &>, >|, <<<, [n]<&word-, [n]>&word-, >>& |
ccc6cda3 | 845 | prompt string special char translation and variable expansion |
bb70624e | 846 | auto-export of variables in initial environment |
ccc6cda3 JA |
847 | command search finds functions before builtins |
848 | bash return builtin will exit a file sourced with `.' | |
7117c2d2 | 849 | builtins: cd -/-L/-P, exec -l/-c/-a, echo -e/-E, hash -d/-l/-p/-t. |
bb70624e | 850 | export -n/-f/-p/name=value, pwd -L/-P, |
17345e5a | 851 | read -e/-p/-a/-t/-n/-d/-s/-u/-i, |
ccc6cda3 JA |
852 | readonly -a/-f/name=value, trap -l, set +o, |
853 | set -b/-m/-o option/-h/-p/-B/-C/-H/-P, | |
0628567a | 854 | unset -f/-v, ulimit -i/-m/-p/-q/-u/-x, |
7117c2d2 | 855 | type -a/-p/-t/-f/-P, suspend -f, kill -n, |
ccc6cda3 JA |
856 | test -o optname/s1 == s2/s1 < s2/s1 > s2/-nt/-ot/-ef/-O/-G/-S |
857 | bash reads ~/.bashrc for interactive shells, $ENV for non-interactive | |
858 | bash restricted shell mode is more extensive | |
859 | bash allows functions and variables with the same name | |
860 | brace expansion | |
861 | tilde expansion | |
862 | arithmetic expansion with $((...)) and `let' builtin | |
bc4cd23c | 863 | the `[[...]]' extended conditional command |
ccc6cda3 JA |
864 | process substitution |
865 | aliases and alias/unalias builtins | |
866 | local variables in functions and `local' builtin | |
bb70624e | 867 | readline and command-line editing with programmable completion |
ccc6cda3 JA |
868 | command history and history/fc builtins |
869 | csh-like history expansion | |
bb70624e JA |
870 | other new bash builtins: bind, command, compgen, complete, builtin, |
871 | declare/typeset, dirs, enable, fc, help, | |
872 | history, logout, popd, pushd, disown, shopt, | |
17345e5a | 873 | printf, compopt, mapfile |
ccc6cda3 JA |
874 | exported functions |
875 | filename generation when using output redirection (command >a*) | |
bc4cd23c JA |
876 | POSIX.2-style globbing character classes |
877 | POSIX.2-style globbing equivalence classes | |
878 | POSIX.2-style globbing collating symbols | |
879 | egrep-like extended pattern matching operators | |
880 | case-insensitive pattern matching and globbing | |
ccc6cda3 JA |
881 | variable assignments preceding commands affect only that command, |
882 | even for builtins and functions | |
95732b49 | 883 | posix mode and strict posix conformance |
bb70624e JA |
884 | redirection to /dev/fd/N, /dev/stdin, /dev/stdout, /dev/stderr, |
885 | /dev/tcp/host/port, /dev/udp/host/port | |
b80f6443 JA |
886 | debugger support, including `caller' builtin and new variables |
887 | RETURN trap | |
95732b49 | 888 | the `+=' assignment operator |
17345e5a JA |
889 | autocd shell option and behavior |
890 | command-not-found hook with command_not_found_handle shell function | |
891 | globstar shell option and `**' globbing behavior | |
892 | |& synonym for `2>&1 |' | |
893 | ;& and ;;& case action list terminators | |
894 | case-modifying word expansions and variable attributes | |
895 | associative arrays | |
896 | coprocesses using the `coproc' reserved word and variables | |
ccc6cda3 JA |
897 | |
898 | Things sh has that bash does not: | |
899 | uses variable SHACCT to do shell accounting | |
900 | includes `stop' builtin (bash can use alias stop='kill -s STOP') | |
901 | `newgrp' builtin | |
902 | turns on job control if called as `jsh' | |
ccc6cda3 JA |
903 | $TIMEOUT (like bash $TMOUT) |
904 | `^' is a synonym for `|' | |
905 | new SVR4.2 sh builtins: mldmode, priv | |
906 | ||
907 | Implementation differences: | |
908 | redirection to/from compound commands causes sh to create a subshell | |
909 | bash does not allow unbalanced quotes; sh silently inserts them at EOF | |
910 | bash does not mess with signal 11 | |
911 | sh sets (euid, egid) to (uid, gid) if -p not supplied and uid < 100 | |
912 | bash splits only the results of expansions on IFS, using POSIX.2 | |
913 | field splitting rules; sh splits all words on IFS | |
914 | sh does not allow MAILCHECK to be unset (?) | |
915 | sh does not allow traps on SIGALRM or SIGCHLD | |
916 | bash allows multiple option arguments when invoked (e.g. -x -v); | |
917 | sh allows only a single option argument (`sh -x -v' attempts | |
d166f048 | 918 | to open a file named `-v', and, on SunOS 4.1.4, dumps core. |
b72432fd JA |
919 | On Solaris 2.4 and earlier versions, sh goes into an infinite |
920 | loop.) | |
ccc6cda3 JA |
921 | sh exits a script if any builtin fails; bash exits only if one of |
922 | the POSIX.2 `special' builtins fails | |
923 | ||
b72432fd | 924 | C2) How does bash differ from the Korn shell, version ksh88? |
ccc6cda3 JA |
925 | |
926 | Things bash has or uses that ksh88 does not: | |
927 | long invocation options | |
f73dda09 | 928 | [-+]O invocation option |
7117c2d2 | 929 | -l invocation option |
ccc6cda3 | 930 | `!' reserved word |
bb70624e | 931 | arithmetic for command: for ((expr1 ; expr2; expr3 )); do list; done |
7117c2d2 | 932 | arithmetic in largest machine-supported size (intmax_t) |
ccc6cda3 JA |
933 | posix mode and posix conformance |
934 | command hashing | |
935 | tilde expansion for assignment statements that look like $PATH | |
936 | process substitution with named pipes if /dev/fd is not available | |
937 | the ${!param} indirect parameter expansion operator | |
bb70624e | 938 | the ${!param*} prefix expansion operator |
7117c2d2 | 939 | the ${param:offset[:length]} parameter substring operator |
ccc6cda3 | 940 | the ${param/pat[/string]} parameter pattern substitution operator |
17345e5a | 941 | variables: BASH, BASH_VERSION, BASH_VERSINFO, BASHPID, UID, EUID, SHLVL, |
ccc6cda3 JA |
942 | TIMEFORMAT, HISTCMD, HOSTTYPE, OSTYPE, MACHTYPE, |
943 | HISTFILESIZE, HISTIGNORE, HISTCONTROL, PROMPT_COMMAND, | |
944 | IGNOREEOF, FIGNORE, INPUTRC, HOSTFILE, DIRSTACK, | |
945 | PIPESTATUS, HOSTNAME, OPTERR, SHELLOPTS, GLOBIGNORE, | |
17345e5a | 946 | GROUPS, FUNCNAME, histchars, auto_resume, PROMPT_DIRTRIM |
ccc6cda3 | 947 | prompt expansion with backslash escapes and command substitution |
17345e5a | 948 | redirection: &> (stdout and stderr), <<<, [n]<&word-, [n]>&word-, >>& |
bb70624e | 949 | more extensive and extensible editing and programmable completion |
ccc6cda3 JA |
950 | builtins: bind, builtin, command, declare, dirs, echo -e/-E, enable, |
951 | exec -l/-c/-a, fc -s, export -n/-f/-p, hash, help, history, | |
952 | jobs -x/-r/-s, kill -s/-n/-l, local, logout, popd, pushd, | |
bb70624e JA |
953 | read -e/-p/-a/-t/-n/-d/-s, readonly -a/-n/-f/-p, |
954 | set -o braceexpand/-o histexpand/-o interactive-comments/ | |
955 | -o notify/-o physical/-o posix/-o hashall/-o onecmd/ | |
956 | -h/-B/-C/-b/-H/-P, set +o, suspend, trap -l, type, | |
0628567a | 957 | typeset -a/-F/-p, ulimit -i/-q/-u/-x, umask -S, alias -p, |
17345e5a | 958 | shopt, disown, printf, complete, compgen, compopt, mapfile |
ccc6cda3 | 959 | `!' csh-style history expansion |
bc4cd23c JA |
960 | POSIX.2-style globbing character classes |
961 | POSIX.2-style globbing equivalence classes | |
962 | POSIX.2-style globbing collating symbols | |
963 | egrep-like extended pattern matching operators | |
964 | case-insensitive pattern matching and globbing | |
965 | `**' arithmetic operator to do exponentiation | |
bb70624e | 966 | redirection to /dev/fd/N, /dev/stdin, /dev/stdout, /dev/stderr |
f73dda09 | 967 | arrays of unlimited size |
7117c2d2 | 968 | TMOUT is default timeout for `read' and `select' |
b80f6443 JA |
969 | debugger support, including the `caller' builtin |
970 | RETURN trap | |
971 | Timestamps in history entries | |
972 | {x..y} brace expansion | |
95732b49 | 973 | The `+=' assignment operator |
17345e5a JA |
974 | autocd shell option and behavior |
975 | command-not-found hook with command_not_found_handle shell function | |
976 | globstar shell option and `**' globbing behavior | |
977 | |& synonym for `2>&1 |' | |
978 | ;& and ;;& case action list terminators | |
979 | case-modifying word expansions and variable attributes | |
980 | associative arrays | |
981 | coprocesses using the `coproc' reserved word and variables | |
ccc6cda3 JA |
982 | |
983 | Things ksh88 has or uses that bash does not: | |
f73dda09 | 984 | tracked aliases (alias -t) |
bb70624e | 985 | variables: ERRNO, FPATH, EDITOR, VISUAL |
17345e5a | 986 | co-processes (bash uses different syntax) |
ccc6cda3 JA |
987 | weirdly-scoped functions |
988 | typeset +f to list all function names without definitions | |
989 | text of command history kept in a file, not memory | |
b80f6443 | 990 | builtins: alias -x, cd old new, newgrp, print, |
7117c2d2 | 991 | read -p/-s/var?prompt, set -A/-o gmacs/ |
b80f6443 | 992 | -o bgnice/-o markdirs/-o trackall/-o viraw/-s, |
17345e5a | 993 | typeset -H/-L/-R/-Z/-A/-ft/-fu/-fx/-t, whence |
f73dda09 JA |
994 | using environment to pass attributes of exported variables |
995 | arithmetic evaluation done on arguments to some builtins | |
996 | reads .profile from $PWD when invoked as login shell | |
ccc6cda3 JA |
997 | |
998 | Implementation differences: | |
999 | ksh runs last command of a pipeline in parent shell context | |
ccc6cda3 JA |
1000 | bash has brace expansion by default (ksh88 compile-time option) |
1001 | bash has fixed startup file for all interactive shells; ksh reads $ENV | |
1002 | bash has exported functions | |
1003 | bash command search finds functions before builtins | |
f73dda09 JA |
1004 | bash waits for all commands in pipeline to exit before returning status |
1005 | emacs-mode editing has some slightly different key bindings | |
ccc6cda3 | 1006 | |
b72432fd | 1007 | C3) Which new features in ksh-93 are not in bash, and which are? |
ccc6cda3 | 1008 | |
17345e5a JA |
1009 | This list is current through ksh93t (11/04/2008) |
1010 | ||
1011 | New things in ksh-93 not in bash-4.0: | |
f73dda09 | 1012 | floating point arithmetic and variables |
ccc6cda3 JA |
1013 | math library functions |
1014 | ${!name[sub]} name of subscript for associative array | |
ccc6cda3 JA |
1015 | `.' is allowed in variable names to create a hierarchical namespace |
1016 | more extensive compound assignment syntax | |
1017 | discipline functions | |
ccc6cda3 | 1018 | KEYBD trap |
bb70624e | 1019 | variables: .sh.edchar, .sh.edmode, .sh.edcol, .sh.edtext, .sh.version, |
f73dda09 JA |
1020 | .sh.name, .sh.subscript, .sh.value, .sh.match, HISTEDIT |
1021 | backreferences in pattern matching (\N) | |
17345e5a | 1022 | `&' operator in pattern lists for matching (match all instead of any) |
f73dda09 | 1023 | exit statuses between 0 and 255 |
f73dda09 | 1024 | FPATH and PATH mixing |
b80f6443 JA |
1025 | lexical scoping for local variables in `ksh' functions |
1026 | no scoping for local variables in `POSIX' functions | |
17345e5a JA |
1027 | $'' \C[.collating-element.] escape sequence |
1028 | -C/-I invocation options | |
1029 | print -f (bash uses printf) | |
1030 | `fc' has been renamed to `hist' | |
1031 | `.' can execute shell functions | |
1032 | getopts -a | |
1033 | printf %B, %H, %P, %R, %T, %Z modifiers, output base for %d, `=' flag | |
1034 | read -N (read -n differs, too)/-v | |
1035 | set -o showme/-o multiline (bash default) | |
1036 | `sleep' and `getconf' builtins (bash has loadable versions) | |
1037 | typeset -n and `nameref' variables | |
1038 | typeset -C/-S/-T/-X/-h/-s | |
1039 | experimental `type' definitions (a la typedef) using typeset | |
1040 | negative subscripts for indexed array variables | |
1041 | array expansions ${array[sub1..sub2]} and ${!array[sub1..sub2]} | |
1042 | associative array assignments using `;' as element separator | |
1043 | command substitution $(n<#) expands to current byte offset for fd N | |
1044 | new '${ ' form of command substitution, executed in current shell | |
1045 | new >;/<#pat/<##pat/<#/># redirections | |
1046 | redirection operators preceded with {varname} to store fd number in varname | |
1047 | brace expansion printf-like formats | |
1048 | ||
1049 | New things in ksh-93 present in bash-4.0: | |
1050 | associative arrays | |
7117c2d2 | 1051 | [n]<&word- and [n]>&word- redirections (combination dup and close) |
bb70624e JA |
1052 | for (( expr1; expr2; expr3 )) ; do list; done - arithmetic for command |
1053 | ?:, ++, --, `expr1 , expr2' arithmetic operators | |
1054 | expansions: ${!param}, ${param:offset[:len]}, ${param/pat[/str]}, | |
1055 | ${!param*} | |
ccc6cda3 JA |
1056 | compound array assignment |
1057 | the `!' reserved word | |
1058 | loadable builtins -- but ksh uses `builtin' while bash uses `enable' | |
ccc6cda3 JA |
1059 | new $'...' and $"..." quoting |
1060 | FIGNORE (but bash uses GLOBIGNORE), HISTCMD | |
17345e5a | 1061 | brace expansion and set -B |
ccc6cda3 | 1062 | changes to kill builtin |
17345e5a JA |
1063 | `command', `builtin', `disown' builtins |
1064 | echo -e | |
1065 | exec -c/-a | |
ccc6cda3 | 1066 | read -A (bash uses read -a) |
bb70624e | 1067 | read -t/-d |
ccc6cda3 | 1068 | trap -p |
ccc6cda3 | 1069 | `.' restores the positional parameters when it completes |
17345e5a JA |
1070 | set -o notify/-C |
1071 | set -o pipefail | |
1072 | set -G (-o globstar) and ** | |
ccc6cda3 JA |
1073 | POSIX.2 `test' |
1074 | umask -S | |
1075 | unalias -a | |
1076 | command and arithmetic substitution performed on PS1, PS4, and ENV | |
17345e5a | 1077 | command name completion, TAB displaying possible completions |
d166f048 | 1078 | ENV processed only for interactive shells |
95732b49 | 1079 | The `+=' assignment operator |
17345e5a JA |
1080 | the `;&' case statement "fallthrough" pattern list terminator |
1081 | csh-style history expansion and set -H | |
1082 | negative offsets in ${param:offset:length} | |
ccc6cda3 JA |
1083 | |
1084 | Section D: Why does bash do some things differently than other Unix shells? | |
1085 | ||
b72432fd | 1086 | D1) Why does bash run a different version of `command' than |
ccc6cda3 JA |
1087 | `which command' says it will? |
1088 | ||
b72432fd JA |
1089 | On many systems, `which' is actually a csh script that assumes |
1090 | you're running csh. In tcsh, `which' and its cousin `where' | |
1091 | are builtins. On other Unix systems, `which' is a perl script | |
3185942a JA |
1092 | that uses the PATH environment variable. Many Linux distributions |
1093 | use GNU `which', which is a C program that can understand shell | |
1094 | aliases. | |
b72432fd JA |
1095 | |
1096 | The csh script version reads the csh startup files from your | |
1097 | home directory and uses those to determine which `command' will | |
1098 | be invoked. Since bash doesn't use any of those startup files, | |
1099 | there's a good chance that your bash environment differs from | |
1100 | your csh environment. The bash `type' builtin does everything | |
1101 | `which' does, and will report correct results for the running | |
1102 | shell. If you're really wedded to the name `which', try adding | |
1103 | the following function definition to your .bashrc: | |
1104 | ||
1105 | which() | |
1106 | { | |
bb70624e | 1107 | builtin type "$@" |
b72432fd JA |
1108 | } |
1109 | ||
1110 | If you're moving from tcsh and would like to bring `where' along | |
1111 | as well, use this function: | |
1112 | ||
1113 | where() | |
1114 | { | |
1115 | builtin type -a "$@" | |
1116 | } | |
ccc6cda3 | 1117 | |
b72432fd | 1118 | D2) Why doesn't bash treat brace expansions exactly like csh? |
ccc6cda3 JA |
1119 | |
1120 | The only difference between bash and csh brace expansion is that | |
1121 | bash requires a brace expression to contain at least one unquoted | |
1122 | comma if it is to be expanded. Any brace-surrounded word not | |
1123 | containing an unquoted comma is left unchanged by the brace | |
1124 | expansion code. This affords the greatest degree of sh | |
1125 | compatibility. | |
1126 | ||
1127 | Bash, ksh, zsh, and pd-ksh all implement brace expansion this way. | |
1128 | ||
b72432fd | 1129 | D3) Why doesn't bash have csh variable modifiers? |
ccc6cda3 JA |
1130 | |
1131 | Posix has specified a more powerful, albeit somewhat more cryptic, | |
1132 | mechanism cribbed from ksh, and bash implements it. | |
1133 | ||
1134 | ${parameter%word} | |
1135 | Remove smallest suffix pattern. The WORD is expanded to produce | |
1136 | a pattern. It then expands to the value of PARAMETER, with the | |
1137 | smallest portion of the suffix matched by the pattern deleted. | |
1138 | ||
1139 | x=file.c | |
1140 | echo ${x%.c}.o | |
1141 | -->file.o | |
1142 | ||
1143 | ${parameter%%word} | |
1144 | ||
1145 | Remove largest suffix pattern. The WORD is expanded to produce | |
1146 | a pattern. It then expands to the value of PARAMETER, with the | |
1147 | largest portion of the suffix matched by the pattern deleted. | |
1148 | ||
1149 | x=posix/src/std | |
1150 | echo ${x%%/*} | |
1151 | -->posix | |
1152 | ||
1153 | ${parameter#word} | |
1154 | Remove smallest prefix pattern. The WORD is expanded to produce | |
1155 | a pattern. It then expands to the value of PARAMETER, with the | |
1156 | smallest portion of the prefix matched by the pattern deleted. | |
1157 | ||
1158 | x=$HOME/src/cmd | |
1159 | echo ${x#$HOME} | |
1160 | -->/src/cmd | |
1161 | ||
1162 | ${parameter##word} | |
1163 | Remove largest prefix pattern. The WORD is expanded to produce | |
1164 | a pattern. It then expands to the value of PARAMETER, with the | |
1165 | largest portion of the prefix matched by the pattern deleted. | |
1166 | ||
1167 | x=/one/two/three | |
1168 | echo ${x##*/} | |
1169 | -->three | |
1170 | ||
1171 | ||
1172 | Given | |
1173 | a=/a/b/c/d | |
1174 | b=b.xxx | |
1175 | ||
1176 | csh bash result | |
1177 | --- ---- ------ | |
1178 | $a:h ${a%/*} /a/b/c | |
1179 | $a:t ${a##*/} d | |
1180 | $b:r ${b%.*} b | |
1181 | $b:e ${b##*.} xxx | |
1182 | ||
1183 | ||
b72432fd | 1184 | D4) How can I make my csh aliases work when I convert to bash? |
ccc6cda3 JA |
1185 | |
1186 | Bash uses a different syntax to support aliases than csh does. | |
1187 | The details can be found in the documentation. We have provided | |
1188 | a shell script which does most of the work of conversion for you; | |
28ef6c31 | 1189 | this script can be found in ./examples/misc/aliasconv.sh. Here is |
ccc6cda3 JA |
1190 | how you use it: |
1191 | ||
1192 | Start csh in the normal way for you. (e.g., `csh') | |
1193 | ||
28ef6c31 | 1194 | Pipe the output of `alias' through `aliasconv.sh', saving the |
ccc6cda3 JA |
1195 | results into `bash_aliases': |
1196 | ||
28ef6c31 | 1197 | alias | bash aliasconv.sh >bash_aliases |
ccc6cda3 JA |
1198 | |
1199 | Edit `bash_aliases', carefully reading through any created | |
1200 | functions. You will need to change the names of some csh specific | |
1201 | variables to the bash equivalents. The script converts $cwd to | |
1202 | $PWD, $term to $TERM, $home to $HOME, $user to $USER, and $prompt | |
1203 | to $PS1. You may also have to add quotes to avoid unwanted | |
1204 | expansion. | |
1205 | ||
1206 | For example, the csh alias: | |
1207 | ||
1208 | alias cd 'cd \!*; echo $cwd' | |
1209 | ||
1210 | is converted to the bash function: | |
1211 | ||
1212 | cd () { command cd "$@"; echo $PWD ; } | |
1213 | ||
1214 | The only thing that needs to be done is to quote $PWD: | |
1215 | ||
1216 | cd () { command cd "$@"; echo "$PWD" ; } | |
1217 | ||
1218 | Merge the edited file into your ~/.bashrc. | |
1219 | ||
1220 | There is an additional, more ambitious, script in | |
1221 | examples/misc/cshtobash that attempts to convert your entire csh | |
1222 | environment to its bash equivalent. This script can be run as | |
1223 | simply `cshtobash' to convert your normal interactive | |
1224 | environment, or as `cshtobash ~/.login' to convert your login | |
1225 | environment. | |
1226 | ||
b72432fd | 1227 | D5) How can I pipe standard output and standard error from one command to |
ccc6cda3 JA |
1228 | another, like csh does with `|&'? |
1229 | ||
1230 | Use | |
1231 | command 2>&1 | command2 | |
1232 | ||
1233 | The key is to remember that piping is performed before redirection, so | |
1234 | file descriptor 1 points to the pipe when it is duplicated onto file | |
1235 | descriptor 2. | |
1236 | ||
b72432fd | 1237 | D6) Now that I've converted from ksh to bash, are there equivalents to |
ccc6cda3 JA |
1238 | ksh features like autoloaded functions and the `whence' command? |
1239 | ||
bb70624e JA |
1240 | There are features in ksh-88 and ksh-93 that do not have direct bash |
1241 | equivalents. Most, however, can be emulated with very little trouble. | |
ccc6cda3 JA |
1242 | |
1243 | ksh-88 feature Bash equivalent | |
1244 | -------------- --------------- | |
ccc6cda3 JA |
1245 | compiled-in aliases set up aliases in .bashrc; some ksh aliases are |
1246 | bash builtins (hash, history, type) | |
ccc6cda3 JA |
1247 | coprocesses named pipe pairs (one for read, one for write) |
1248 | typeset +f declare -F | |
1249 | cd, print, whence function substitutes in examples/functions/kshenv | |
1250 | autoloaded functions examples/functions/autoload is the same as typeset -fu | |
1251 | read var?prompt read -p prompt var | |
1252 | ||
bb70624e JA |
1253 | ksh-93 feature Bash equivalent |
1254 | -------------- --------------- | |
1255 | sleep, getconf Bash has loadable versions in examples/loadables | |
1256 | ${.sh.version} $BASH_VERSION | |
1257 | print -f printf | |
7117c2d2 | 1258 | hist alias hist=fc |
bb70624e JA |
1259 | $HISTEDIT $FCEDIT |
1260 | ||
ccc6cda3 JA |
1261 | Section E: How can I get bash to do certain things, and why does bash do |
1262 | things the way it does? | |
1263 | ||
b72432fd | 1264 | E1) Why is the bash builtin `test' slightly different from /bin/test? |
ccc6cda3 JA |
1265 | |
1266 | The specific example used here is [ ! x -o x ], which is false. | |
1267 | ||
1268 | Bash's builtin `test' implements the Posix.2 spec, which can be | |
1269 | summarized as follows (the wording is due to David Korn): | |
1270 | ||
1271 | Here is the set of rules for processing test arguments. | |
1272 | ||
1273 | 0 Args: False | |
1274 | 1 Arg: True iff argument is not null. | |
1275 | 2 Args: If first arg is !, True iff second argument is null. | |
1276 | If first argument is unary, then true if unary test is true | |
1277 | Otherwise error. | |
1278 | 3 Args: If second argument is a binary operator, do binary test of $1 $3 | |
1279 | If first argument is !, negate two argument test of $2 $3 | |
1280 | If first argument is `(' and third argument is `)', do the | |
1281 | one-argument test of the second argument. | |
1282 | Otherwise error. | |
1283 | 4 Args: If first argument is !, negate three argument test of $2 $3 $4. | |
1284 | Otherwise unspecified | |
1285 | 5 or more Args: unspecified. (Historical shells would use their | |
1286 | current algorithm). | |
1287 | ||
1288 | The operators -a and -o are considered binary operators for the purpose | |
1289 | of the 3 Arg case. | |
1290 | ||
1291 | As you can see, the test becomes (not (x or x)), which is false. | |
1292 | ||
b72432fd | 1293 | E2) Why does bash sometimes say `Broken pipe'? |
ccc6cda3 JA |
1294 | |
1295 | If a sequence of commands appears in a pipeline, and one of the | |
1296 | reading commands finishes before the writer has finished, the | |
1297 | writer receives a SIGPIPE signal. Many other shells special-case | |
1298 | SIGPIPE as an exit status in the pipeline and do not report it. | |
1299 | For example, in: | |
1300 | ||
1301 | ps -aux | head | |
1302 | ||
1303 | `head' can finish before `ps' writes all of its output, and ps | |
1304 | will try to write on a pipe without a reader. In that case, bash | |
1305 | will print `Broken pipe' to stderr when ps is killed by a | |
1306 | SIGPIPE. | |
1307 | ||
0628567a | 1308 | As of bash-3.1, bash does not report SIGPIPE errors by default. You |
95732b49 | 1309 | can build a version of bash that will report such errors. |
b72432fd | 1310 | |
bb70624e | 1311 | E3) When I have terminal escape sequences in my prompt, why does bash |
ccc6cda3 JA |
1312 | wrap lines at the wrong column? |
1313 | ||
1314 | Readline, the line editing library that bash uses, does not know | |
1315 | that the terminal escape sequences do not take up space on the | |
1316 | screen. The redisplay code assumes, unless told otherwise, that | |
1317 | each character in the prompt is a `printable' character that | |
1318 | takes up one character position on the screen. | |
1319 | ||
1320 | You can use the bash prompt expansion facility (see the PROMPTING | |
1321 | section in the manual page) to tell readline that sequences of | |
1322 | characters in the prompt strings take up no screen space. | |
1323 | ||
1324 | Use the \[ escape to begin a sequence of non-printing characters, | |
1325 | and the \] escape to signal the end of such a sequence. | |
1326 | ||
bb70624e | 1327 | E4) If I pipe the output of a command into `read variable', why doesn't |
ccc6cda3 JA |
1328 | the output show up in $variable when the read command finishes? |
1329 | ||
1330 | This has to do with the parent-child relationship between Unix | |
28ef6c31 JA |
1331 | processes. It affects all commands run in pipelines, not just |
1332 | simple calls to `read'. For example, piping a command's output | |
1333 | into a `while' loop that repeatedly calls `read' will result in | |
1334 | the same behavior. | |
ccc6cda3 | 1335 | |
95732b49 JA |
1336 | Each element of a pipeline, even a builtin or shell function, |
1337 | runs in a separate process, a child of the shell running the | |
1338 | pipeline. A subprocess cannot affect its parent's environment. | |
1339 | When the `read' command sets the variable to the input, that | |
1340 | variable is set only in the subshell, not the parent shell. When | |
1341 | the subshell exits, the value of the variable is lost. | |
ccc6cda3 JA |
1342 | |
1343 | Many pipelines that end with `read variable' can be converted | |
1344 | into command substitutions, which will capture the output of | |
1345 | a specified command. The output can then be assigned to a | |
1346 | variable: | |
1347 | ||
1348 | grep ^gnu /usr/lib/news/active | wc -l | read ngroup | |
1349 | ||
1350 | can be converted into | |
1351 | ||
1352 | ngroup=$(grep ^gnu /usr/lib/news/active | wc -l) | |
1353 | ||
1354 | This does not, unfortunately, work to split the text among | |
1355 | multiple variables, as read does when given multiple variable | |
1356 | arguments. If you need to do this, you can either use the | |
1357 | command substitution above to read the output into a variable | |
1358 | and chop up the variable using the bash pattern removal | |
1359 | expansion operators or use some variant of the following | |
1360 | approach. | |
1361 | ||
1362 | Say /usr/local/bin/ipaddr is the following shell script: | |
1363 | ||
1364 | #! /bin/sh | |
1365 | host `hostname` | awk '/address/ {print $NF}' | |
1366 | ||
1367 | Instead of using | |
1368 | ||
1369 | /usr/local/bin/ipaddr | read A B C D | |
1370 | ||
1371 | to break the local machine's IP address into separate octets, use | |
1372 | ||
1373 | OIFS="$IFS" | |
1374 | IFS=. | |
1375 | set -- $(/usr/local/bin/ipaddr) | |
1376 | IFS="$OIFS" | |
1377 | A="$1" B="$2" C="$3" D="$4" | |
1378 | ||
1379 | Beware, however, that this will change the shell's positional | |
1380 | parameters. If you need them, you should save them before doing | |
1381 | this. | |
1382 | ||
1383 | This is the general approach -- in most cases you will not need to | |
1384 | set $IFS to a different value. | |
1385 | ||
f73dda09 JA |
1386 | Some other user-supplied alternatives include: |
1387 | ||
1388 | read A B C D << HERE | |
1389 | $(IFS=.; echo $(/usr/local/bin/ipaddr)) | |
1390 | HERE | |
1391 | ||
1392 | and, where process substitution is available, | |
1393 | ||
1394 | read A B C D < <(IFS=.; echo $(/usr/local/bin/ipaddr)) | |
1395 | ||
bb70624e | 1396 | E5) I have a bunch of shell scripts that use backslash-escaped characters |
ccc6cda3 JA |
1397 | in arguments to `echo'. Bash doesn't interpret these characters. Why |
1398 | not, and how can I make it understand them? | |
1399 | ||
1400 | This is the behavior of echo on most Unix System V machines. | |
1401 | ||
bb70624e | 1402 | The bash builtin `echo' is modeled after the 9th Edition |
ccc6cda3 JA |
1403 | Research Unix version of `echo'. It does not interpret |
1404 | backslash-escaped characters in its argument strings by default; | |
1405 | it requires the use of the -e option to enable the | |
1406 | interpretation. The System V echo provides no way to disable the | |
1407 | special characters; the bash echo has a -E option to disable | |
1408 | them. | |
1409 | ||
1410 | There is a configuration option that will make bash behave like | |
1411 | the System V echo and interpret things like `\t' by default. Run | |
f73dda09 | 1412 | configure with the --enable-xpg-echo-default option to turn this |
ccc6cda3 JA |
1413 | on. Be aware that this will cause some of the tests run when you |
1414 | type `make tests' to fail. | |
1415 | ||
7117c2d2 | 1416 | There is a shell option, `xpg_echo', settable with `shopt', that will |
bb70624e JA |
1417 | change the behavior of echo at runtime. Enabling this option turns |
1418 | on expansion of backslash-escape sequences. | |
1419 | ||
1420 | E6) Why doesn't a while or for loop get suspended when I type ^Z? | |
ccc6cda3 JA |
1421 | |
1422 | This is a consequence of how job control works on Unix. The only | |
1423 | thing that can be suspended is the process group. This is a single | |
1424 | command or pipeline of commands that the shell forks and executes. | |
1425 | ||
1426 | When you run a while or for loop, the only thing that the shell forks | |
1427 | and executes are any commands in the while loop test and commands in | |
1428 | the loop bodies. These, therefore, are the only things that can be | |
1429 | suspended when you type ^Z. | |
1430 | ||
1431 | If you want to be able to stop the entire loop, you need to put it | |
1432 | within parentheses, which will force the loop into a subshell that | |
1433 | may be stopped (and subsequently restarted) as a single unit. | |
1434 | ||
28ef6c31 JA |
1435 | E7) What about empty for loops in Makefiles? |
1436 | ||
1437 | It's fairly common to see constructs like this in automatically-generated | |
1438 | Makefiles: | |
1439 | ||
1440 | SUBDIRS = @SUBDIRS@ | |
1441 | ||
1442 | ... | |
1443 | ||
1444 | subdirs-clean: | |
1445 | for d in ${SUBDIRS}; do \ | |
1446 | ( cd $$d && ${MAKE} ${MFLAGS} clean ) \ | |
1447 | done | |
1448 | ||
1449 | When SUBDIRS is empty, this results in a command like this being passed to | |
1450 | bash: | |
1451 | ||
1452 | for d in ; do | |
1453 | ( cd $d && ${MAKE} ${MFLAGS} clean ) | |
1454 | done | |
1455 | ||
f73dda09 JA |
1456 | In versions of bash before bash-2.05a, this was a syntax error. If the |
1457 | reserved word `in' was present, a word must follow it before the semicolon | |
1458 | or newline. The language in the manual page referring to the list of words | |
1459 | being empty referred to the list after it is expanded. These versions of | |
1460 | bash required that there be at least one word following the `in' when the | |
1461 | construct was parsed. | |
28ef6c31 JA |
1462 | |
1463 | The idiomatic Makefile solution is something like: | |
1464 | ||
1465 | SUBDIRS = @SUBDIRS@ | |
1466 | ||
1467 | subdirs-clean: | |
1468 | subdirs=$SUBDIRS ; for d in $$subdirs; do \ | |
1469 | ( cd $$d && ${MAKE} ${MFLAGS} clean ) \ | |
1470 | done | |
1471 | ||
b80f6443 JA |
1472 | The latest updated POSIX standard has changed this: the word list |
1473 | is no longer required. Bash versions 2.05a and later accept the | |
1474 | new syntax. | |
28ef6c31 JA |
1475 | |
1476 | E8) Why does the arithmetic evaluation code complain about `08'? | |
1477 | ||
1478 | The bash arithmetic evaluation code (used for `let', $(()), (()), and in | |
1479 | other places), interprets a leading `0' in numeric constants as denoting | |
1480 | an octal number, and a leading `0x' as denoting hexadecimal. This is | |
1481 | in accordance with the POSIX.2 spec, section 2.9.2.1, which states that | |
1482 | arithmetic constants should be handled as signed long integers as defined | |
1483 | by the ANSI/ISO C standard. | |
1484 | ||
1485 | The POSIX.2 interpretation committee has confirmed this: | |
1486 | ||
1487 | http://www.pasc.org/interps/unofficial/db/p1003.2/pasc-1003.2-173.html | |
1488 | ||
1489 | E9) Why does the pattern matching expression [A-Z]* match files beginning | |
1490 | with every letter except `z'? | |
1491 | ||
7117c2d2 JA |
1492 | Bash-2.03, Bash-2.05 and later versions honor the current locale setting |
1493 | when processing ranges within pattern matching bracket expressions ([A-Z]). | |
1494 | This is what POSIX.2 and SUSv3/XPG6 specify. | |
28ef6c31 | 1495 | |
f73dda09 JA |
1496 | The behavior of the matcher in bash-2.05 and later versions depends on the |
1497 | current LC_COLLATE setting. Setting this variable to `C' or `POSIX' will | |
1498 | result in the traditional behavior ([A-Z] matches all uppercase ASCII | |
1499 | characters). Many other locales, including the en_US locale (the default | |
1500 | on many US versions of Linux) collate the upper and lower case letters like | |
1501 | this: | |
28ef6c31 JA |
1502 | |
1503 | AaBb...Zz | |
1504 | ||
7117c2d2 JA |
1505 | which means that [A-Z] matches every letter except `z'. Others collate like |
1506 | ||
1507 | aAbBcC...zZ | |
1508 | ||
1509 | which means that [A-Z] matches every letter except `a'. | |
28ef6c31 JA |
1510 | |
1511 | The portable way to specify upper case letters is [:upper:] instead of | |
1512 | A-Z; lower case may be specified as [:lower:] instead of a-z. | |
1513 | ||
1514 | Look at the manual pages for setlocale(3), strcoll(3), and, if it is | |
1515 | present, locale(1). If you have locale(1), you can use it to find | |
1516 | your current locale information even if you do not have any of the | |
1517 | LC_ variables set. | |
1518 | ||
1519 | My advice is to put | |
1520 | ||
1521 | export LC_COLLATE=C | |
1522 | ||
1523 | into /etc/profile and inspect any shell scripts run from cron for | |
1524 | constructs like [A-Z]. This will prevent things like | |
1525 | ||
1526 | rm [A-Z]* | |
1527 | ||
1528 | from removing every file in the current directory except those beginning | |
1529 | with `z' and still allow individual users to change the collation order. | |
1530 | Users may put the above command into their own profiles as well, of course. | |
1531 | ||
7117c2d2 JA |
1532 | E10) Why does `cd //' leave $PWD as `//'? |
1533 | ||
1534 | POSIX.2, in its description of `cd', says that *three* or more leading | |
1535 | slashes may be replaced with a single slash when canonicalizing the | |
1536 | current working directory. | |
1537 | ||
1538 | This is, I presume, for historical compatibility. Certain versions of | |
1539 | Unix, and early network file systems, used paths of the form | |
1540 | //hostname/path to access `path' on server `hostname'. | |
1541 | ||
b80f6443 JA |
1542 | E11) If I resize my xterm while another program is running, why doesn't bash |
1543 | notice the change? | |
1544 | ||
1545 | This is another issue that deals with job control. | |
1546 | ||
1547 | The kernel maintains a notion of a current terminal process group. Members | |
1548 | of this process group (processes whose process group ID is equal to the | |
1549 | current terminal process group ID) receive terminal-generated signals like | |
1550 | SIGWINCH. (For more details, see the JOB CONTROL section of the bash | |
1551 | man page.) | |
1552 | ||
1553 | If a terminal is resized, the kernel sends SIGWINCH to each member of | |
1554 | the terminal's current process group (the `foreground' process group). | |
1555 | ||
1556 | When bash is running with job control enabled, each pipeline (which may be | |
1557 | a single command) is run in its own process group, different from bash's | |
1558 | process group. This foreground process group receives the SIGWINCH; bash | |
1559 | does not. Bash has no way of knowing that the terminal has been resized. | |
1560 | ||
1561 | There is a `checkwinsize' option, settable with the `shopt' builtin, that | |
1562 | will cause bash to check the window size and adjust its idea of the | |
1563 | terminal's dimensions each time a process stops or exits and returns control | |
1564 | of the terminal to bash. Enable it with `shopt -s checkwinsize'. | |
1565 | ||
1566 | E12) Why don't negative offsets in substring expansion work like I expect? | |
1567 | ||
1568 | When substring expansion of the form ${param:offset[:length} is used, | |
1569 | an `offset' that evaluates to a number less than zero counts back from | |
1570 | the end of the expanded value of $param. | |
1571 | ||
1572 | When a negative `offset' begins with a minus sign, however, unexpected things | |
1573 | can happen. Consider | |
1574 | ||
1575 | a=12345678 | |
1576 | echo ${a:-4} | |
1577 | ||
1578 | intending to print the last four characters of $a. The problem is that | |
1579 | ${param:-word} already has a well-defined meaning: expand to word if the | |
1580 | expanded value of param is unset or null, and $param otherwise. | |
1581 | ||
1582 | To use negative offsets that begin with a minus sign, separate the | |
1583 | minus sign and the colon with a space. | |
1584 | ||
0628567a JA |
1585 | E13) Why does filename completion misbehave if a colon appears in the filename? |
1586 | ||
1587 | Filename completion (and word completion in general) may appear to behave | |
1588 | improperly if there is a colon in the word to be completed. | |
1589 | ||
1590 | The colon is special to readline's word completion code: it is one of the | |
1591 | characters that breaks words for the completer. Readline uses these characters | |
1592 | in sort of the same way that bash uses $IFS: they break or separate the words | |
1593 | the completion code hands to the application-specific or default word | |
1594 | completion functions. The original intent was to make it easy to edit | |
1595 | colon-separated lists (such as $PATH in bash) in various applications using | |
1596 | readline for input. | |
1597 | ||
1598 | This is complicated by the fact that some versions of the popular | |
1599 | `bash-completion' programmable completion package have problems with the | |
1600 | default completion behavior in the presence of colons. | |
1601 | ||
1602 | The current set of completion word break characters is available in bash as | |
1603 | the value of the COMP_WORDBREAKS variable. Removing `:' from that value is | |
1604 | enough to make the colon not special to completion: | |
1605 | ||
1606 | COMP_WORDBREAKS=${COMP_WORDBREAKS//:} | |
1607 | ||
1608 | You can also quote the colon with a backslash to achieve the same result | |
1609 | temporarily. | |
1610 | ||
3185942a JA |
1611 | E14) Why does quoting the pattern argument to the regular expression matching |
1612 | conditional operator (=~) cause regexp matching to stop working? | |
1613 | ||
1614 | In versions of bash prior to bash-3.2, the effect of quoting the regular | |
1615 | expression argument to the [[ command's =~ operator was not specified. | |
1616 | The practical effect was that double-quoting the pattern argument required | |
1617 | backslashes to quote special pattern characters, which interfered with the | |
1618 | backslash processing performed by double-quoted word expansion and was | |
1619 | inconsistent with how the == shell pattern matching operator treated | |
1620 | quoted characters. | |
1621 | ||
1622 | In bash-3.2, the shell was changed to internally quote characters in single- | |
1623 | and double-quoted string arguments to the =~ operator, which suppresses the | |
1624 | special meaning of the characters special to regular expression processing | |
1625 | (`.', `[', `\', `(', `), `*', `+', `?', `{', `|', `^', and `$') and forces | |
1626 | them to be matched literally. This is consistent with how the `==' pattern | |
1627 | matching operator treats quoted portions of its pattern argument. | |
1628 | ||
1629 | Since the treatment of quoted string arguments was changed, several issues | |
1630 | have arisen, chief among them the problem of white space in pattern arguments | |
1631 | and the differing treatment of quoted strings between bash-3.1 and bash-3.2. | |
1632 | Both problems may be solved by using a shell variable to hold the pattern. | |
1633 | Since word splitting is not performed when expanding shell variables in all | |
1634 | operands of the [[ command, this allows users to quote patterns as they wish | |
1635 | when assigning the variable, then expand the values to a single string that | |
1636 | may contain whitespace. The first problem may be solved by using backslashes | |
1637 | or any other quoting mechanism to escape the white space in the patterns. | |
1638 | ||
17345e5a JA |
1639 | Bash-4.0 introduces the concept of a `compatibility level', controlled by |
1640 | several options to the `shopt' builtin. If the `compat31' option is enabled, | |
1641 | bash reverts to the bash-3.1 behavior with respect to quoting the rhs of | |
1642 | the =~ operator. | |
1643 | ||
ccc6cda3 JA |
1644 | Section F: Things to watch out for on certain Unix versions |
1645 | ||
b72432fd | 1646 | F1) Why can't I use command line editing in my `cmdtool'? |
ccc6cda3 JA |
1647 | |
1648 | The problem is `cmdtool' and bash fighting over the input. When | |
1649 | scrolling is enabled in a cmdtool window, cmdtool puts the tty in | |
1650 | `raw mode' to permit command-line editing using the mouse for | |
1651 | applications that cannot do it themselves. As a result, bash and | |
1652 | cmdtool each try to read keyboard input immediately, with neither | |
1653 | getting enough of it to be useful. | |
1654 | ||
1655 | This mode also causes cmdtool to not implement many of the | |
1656 | terminal functions and control sequences appearing in the | |
1657 | `sun-cmd' termcap entry. For a more complete explanation, see | |
1658 | that file examples/suncmd.termcap in the bash distribution. | |
1659 | ||
1660 | `xterm' is a better choice, and gets along with bash much more | |
1661 | smoothly. | |
1662 | ||
1663 | If you must use cmdtool, you can use the termcap description in | |
1664 | examples/suncmd.termcap. Set the TERMCAP variable to the terminal | |
1665 | description contained in that file, i.e. | |
1666 | ||
1667 | TERMCAP='Mu|sun-cmd:am:bs:km:pt:li#34:co#80:cl=^L:ce=\E[K:cd=\E[J:rs=\E[s:' | |
1668 | ||
1669 | Then export TERMCAP and start a new cmdtool window from that shell. | |
1670 | The bash command-line editing should behave better in the new | |
1671 | cmdtool. If this works, you can put the assignment to TERMCAP | |
1672 | in your bashrc file. | |
1673 | ||
b72432fd | 1674 | F2) I built bash on Solaris 2. Why do globbing expansions and filename |
ccc6cda3 JA |
1675 | completion chop off the first few characters of each filename? |
1676 | ||
1677 | This is the consequence of building bash on SunOS 5 and linking | |
1678 | with the libraries in /usr/ucblib, but using the definitions | |
1679 | and structures from files in /usr/include. | |
1680 | ||
1681 | The actual conflict is between the dirent structure in | |
1682 | /usr/include/dirent.h and the struct returned by the version of | |
1683 | `readdir' in libucb.a (a 4.3-BSD style `struct direct'). | |
1684 | ||
1685 | Make sure you've got /usr/ccs/bin ahead of /usr/ucb in your $PATH | |
1686 | when configuring and building bash. This will ensure that you | |
1687 | use /usr/ccs/bin/cc or acc instead of /usr/ucb/cc and that you | |
1688 | link with libc before libucb. | |
1689 | ||
1690 | If you have installed the Sun C compiler, you may also need to | |
1691 | put /usr/ccs/bin and /opt/SUNWspro/bin into your $PATH before | |
1692 | /usr/ucb. | |
1693 | ||
b72432fd | 1694 | F3) Why does bash dump core after I interrupt username completion or |
ccc6cda3 JA |
1695 | `~user' tilde expansion on a machine running NIS? |
1696 | ||
1697 | This is a famous and long-standing bug in the SunOS YP (sorry, NIS) | |
1698 | client library, which is part of libc. | |
1699 | ||
1700 | The YP library code keeps static state -- a pointer into the data | |
1701 | returned from the server. When YP initializes itself (setpwent), | |
1702 | it looks at this pointer and calls free on it if it's non-null. | |
1703 | So far, so good. | |
1704 | ||
1705 | If one of the YP functions is interrupted during getpwent (the | |
1706 | exact function is interpretwithsave()), and returns NULL, the | |
1707 | pointer is freed without being reset to NULL, and the function | |
1708 | returns. The next time getpwent is called, it sees that this | |
1709 | pointer is non-null, calls free, and the bash free() blows up | |
1710 | because it's being asked to free freed memory. | |
1711 | ||
1712 | The traditional Unix mallocs allow memory to be freed multiple | |
1713 | times; that's probably why this has never been fixed. You can | |
1714 | run configure with the `--without-gnu-malloc' option to use | |
1715 | the C library malloc and avoid the problem. | |
1716 | ||
b72432fd | 1717 | F4) I'm running SVR4.2. Why is the line erased every time I type `@'? |
ccc6cda3 JA |
1718 | |
1719 | The `@' character is the default `line kill' character in most | |
1720 | versions of System V, including SVR4.2. You can change this | |
1721 | character to whatever you want using `stty'. For example, to | |
1722 | change the line kill character to control-u, type | |
1723 | ||
1724 | stty kill ^U | |
1725 | ||
1726 | where the `^' and `U' can be two separate characters. | |
1727 | ||
b72432fd | 1728 | F5) Why does bash report syntax errors when my C News scripts use a |
ccc6cda3 JA |
1729 | redirection before a subshell command? |
1730 | ||
1731 | The actual command in question is something like | |
1732 | ||
1733 | < file ( command ) | |
1734 | ||
1735 | According to the grammar given in the POSIX.2 standard, this construct | |
1736 | is, in fact, a syntax error. Redirections may only precede `simple | |
1737 | commands'. A subshell construct such as the above is one of the shell's | |
1738 | `compound commands'. A redirection may only follow a compound command. | |
1739 | ||
bb70624e JA |
1740 | This affects the mechanical transformation of commands that use `cat' |
1741 | to pipe a file into a command (a favorite Useless-Use-Of-Cat topic on | |
1742 | comp.unix.shell). While most commands of the form | |
1743 | ||
1744 | cat file | command | |
1745 | ||
1746 | can be converted to `< file command', shell control structures such as | |
1747 | loops and subshells require `command < file'. | |
1748 | ||
b80f6443 | 1749 | The file CWRU/sh-redir-hack in the bash distribution is an |
ccc6cda3 JA |
1750 | (unofficial) patch to parse.y that will modify the grammar to |
1751 | support this construct. It will not apply with `patch'; you must | |
1752 | modify parse.y by hand. Note that if you apply this, you must | |
1753 | recompile with -DREDIRECTION_HACK. This introduces a large | |
1754 | number of reduce/reduce conflicts into the shell grammar. | |
1755 | ||
bb70624e JA |
1756 | F6) Why can't I use vi-mode editing on Red Hat Linux 6.1? |
1757 | ||
1758 | The short answer is that Red Hat screwed up. | |
1759 | ||
1760 | The long answer is that they shipped an /etc/inputrc that only works | |
1761 | for emacs mode editing, and then screwed all the vi users by setting | |
1762 | INPUTRC to /etc/inputrc in /etc/profile. | |
1763 | ||
1764 | The short fix is to do one of the following: remove or rename | |
1765 | /etc/inputrc, set INPUTRC=~/.inputrc in ~/.bashrc (or .bash_profile, | |
1766 | but make sure you export it if you do), remove the assignment to | |
1767 | INPUTRC from /etc/profile, add | |
1768 | ||
1769 | set keymap emacs | |
1770 | ||
1771 | to the beginning of /etc/inputrc, or bracket the key bindings in | |
1772 | /etc/inputrc with these lines | |
1773 | ||
1774 | $if mode=emacs | |
1775 | [...] | |
1776 | $endif | |
1777 | ||
7117c2d2 JA |
1778 | F7) Why do bash-2.05a and bash-2.05b fail to compile `printf.def' on |
1779 | HP/UX 11.x? | |
1780 | ||
1781 | HP/UX's support for long double is imperfect at best. | |
1782 | ||
1783 | GCC will support it without problems, but the HP C library functions | |
1784 | like strtold(3) and printf(3) don't actually work with long doubles. | |
1785 | HP implemented a `long_double' type as a 4-element array of 32-bit | |
1786 | ints, and that is what the library functions use. The ANSI C | |
1787 | `long double' type is a 128-bit floating point scalar. | |
1788 | ||
1789 | The easiest fix, until HP fixes things up, is to edit the generated | |
1790 | config.h and #undef the HAVE_LONG_DOUBLE line. After doing that, | |
1791 | the compilation should complete successfully. | |
1792 | ||
bb70624e JA |
1793 | Section G: How can I get bash to do certain common things? |
1794 | ||
1795 | G1) How can I get bash to read and display eight-bit characters? | |
1796 | ||
1797 | This is a process requiring several steps. | |
1798 | ||
1799 | First, you must ensure that the `physical' data path is a full eight | |
1800 | bits. For xterms, for example, the `vt100' resources `eightBitInput' | |
1801 | and `eightBitOutput' should be set to `true'. | |
1802 | ||
1803 | Once you have set up an eight-bit path, you must tell the kernel and | |
1804 | tty driver to leave the eighth bit of characters alone when processing | |
1805 | keyboard input. Use `stty' to do this: | |
1806 | ||
1807 | stty cs8 -istrip -parenb | |
1808 | ||
1809 | For old BSD-style systems, you can use | |
1810 | ||
1811 | stty pass8 | |
1812 | ||
1813 | You may also need | |
1814 | ||
1815 | stty even odd | |
1816 | ||
1817 | Finally, you need to tell readline that you will be inputting and | |
1818 | displaying eight-bit characters. You use readline variables to do | |
1819 | this. These variables can be set in your .inputrc or using the bash | |
1820 | `bind' builtin. Here's an example using `bind': | |
1821 | ||
1822 | bash$ bind 'set convert-meta off' | |
1823 | bash$ bind 'set meta-flag on' | |
1824 | bash$ bind 'set output-meta on' | |
1825 | ||
1826 | The `set' commands between the single quotes may also be placed | |
1827 | in ~/.inputrc. | |
1828 | ||
0628567a JA |
1829 | The script examples/scripts.noah/meta.bash encapsulates the bind |
1830 | commands in a shell function. | |
1831 | ||
bb70624e JA |
1832 | G2) How do I write a function `x' to replace builtin command `x', but |
1833 | still invoke the command from within the function? | |
1834 | ||
1835 | This is why the `command' and `builtin' builtins exist. The | |
1836 | `command' builtin executes the command supplied as its first | |
1837 | argument, skipping over any function defined with that name. The | |
1838 | `builtin' builtin executes the builtin command given as its first | |
1839 | argument directly. | |
1840 | ||
1841 | For example, to write a function to replace `cd' that writes the | |
1842 | hostname and current directory to an xterm title bar, use | |
1843 | something like the following: | |
1844 | ||
1845 | cd() | |
1846 | { | |
1847 | builtin cd "$@" && xtitle "$HOST: $PWD" | |
1848 | } | |
1849 | ||
1850 | This could also be written using `command' instead of `builtin'; | |
1851 | the version above is marginally more efficient. | |
1852 | ||
1853 | G3) How can I find the value of a shell variable whose name is the value | |
1854 | of another shell variable? | |
1855 | ||
1856 | Versions of Bash newer than Bash-2.0 support this directly. You can use | |
1857 | ||
1858 | ${!var} | |
1859 | ||
1860 | For example, the following sequence of commands will echo `z': | |
1861 | ||
1862 | var1=var2 | |
1863 | var2=z | |
1864 | echo ${!var1} | |
1865 | ||
1866 | For sh compatibility, use the `eval' builtin. The important | |
1867 | thing to remember is that `eval' expands the arguments you give | |
1868 | it again, so you need to quote the parts of the arguments that | |
1869 | you want `eval' to act on. | |
1870 | ||
1871 | For example, this expression prints the value of the last positional | |
1872 | parameter: | |
1873 | ||
1874 | eval echo \"\$\{$#\}\" | |
1875 | ||
1876 | The expansion of the quoted portions of this expression will be | |
1877 | deferred until `eval' runs, while the `$#' will be expanded | |
1878 | before `eval' is executed. In versions of bash later than bash-2.0, | |
1879 | ||
1880 | echo ${!#} | |
1881 | ||
1882 | does the same thing. | |
1883 | ||
f73dda09 JA |
1884 | This is not the same thing as ksh93 `nameref' variables, though the syntax |
1885 | is similar. I may add namerefs in a future bash version. | |
1886 | ||
bb70624e JA |
1887 | G4) How can I make the bash `time' reserved word print timing output that |
1888 | looks like the output from my system's /usr/bin/time? | |
1889 | ||
1890 | The bash command timing code looks for a variable `TIMEFORMAT' and | |
1891 | uses its value as a format string to decide how to display the | |
1892 | timing statistics. | |
1893 | ||
1894 | The value of TIMEFORMAT is a string with `%' escapes expanded in a | |
1895 | fashion similar in spirit to printf(3). The manual page explains | |
1896 | the meanings of the escape sequences in the format string. | |
1897 | ||
1898 | If TIMEFORMAT is not set, bash acts as if the following assignment had | |
1899 | been performed: | |
1900 | ||
1901 | TIMEFORMAT=$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS' | |
1902 | ||
1903 | The POSIX.2 default time format (used by `time -p command') is | |
1904 | ||
1905 | TIMEFORMAT=$'real %2R\nuser %2U\nsys %2S' | |
1906 | ||
1907 | The BSD /usr/bin/time format can be emulated with: | |
1908 | ||
1909 | TIMEFORMAT=$'\t%1R real\t%1U user\t%1S sys' | |
1910 | ||
1911 | The System V /usr/bin/time format can be emulated with: | |
1912 | ||
1913 | TIMEFORMAT=$'\nreal\t%1R\nuser\t%1U\nsys\t%1S' | |
1914 | ||
1915 | The ksh format can be emulated with: | |
1916 | ||
1917 | TIMEFORMAT=$'\nreal\t%2lR\nuser\t%2lU\nsys\t%2lS' | |
1918 | ||
1919 | G5) How do I get the current directory into my prompt? | |
1920 | ||
1921 | Bash provides a number of backslash-escape sequences which are expanded | |
1922 | when the prompt string (PS1 or PS2) is displayed. The full list is in | |
1923 | the manual page. | |
1924 | ||
1925 | The \w expansion gives the full pathname of the current directory, with | |
1926 | a tilde (`~') substituted for the current value of $HOME. The \W | |
1927 | expansion gives the basename of the current directory. To put the full | |
1928 | pathname of the current directory into the path without any tilde | |
1929 | subsitution, use $PWD. Here are some examples: | |
1930 | ||
1931 | PS1='\w$ ' # current directory with tilde | |
1932 | PS1='\W$ ' # basename of current directory | |
1933 | PS1='$PWD$ ' # full pathname of current directory | |
1934 | ||
1935 | The single quotes are important in the final example to prevent $PWD from | |
1936 | being expanded when the assignment to PS1 is performed. | |
1937 | ||
1938 | G6) How can I rename "*.foo" to "*.bar"? | |
1939 | ||
1940 | Use the pattern removal functionality described in D3. The following `for' | |
1941 | loop will do the trick: | |
1942 | ||
1943 | for f in *.foo; do | |
1944 | mv $f ${f%foo}bar | |
1945 | done | |
1946 | ||
1947 | G7) How can I translate a filename from uppercase to lowercase? | |
1948 | ||
1949 | The script examples/functions/lowercase, originally written by John DuBois, | |
1950 | will do the trick. The converse is left as an exercise. | |
1951 | ||
1952 | G8) How can I write a filename expansion (globbing) pattern that will match | |
1953 | all files in the current directory except "." and ".."? | |
1954 | ||
1955 | You must have set the `extglob' shell option using `shopt -s extglob' to use | |
1956 | this: | |
1957 | ||
1958 | echo .!(.|) * | |
1959 | ||
1960 | A solution that works without extended globbing is given in the Unix Shell | |
3185942a JA |
1961 | FAQ, posted periodically to comp.unix.shell. It's a variant of |
1962 | ||
1963 | echo .[!.]* ..?* * | |
1964 | ||
1965 | (The ..?* catches files with names of three or more characters beginning | |
1966 | with `..') | |
bb70624e JA |
1967 | |
1968 | Section H: Where do I go from here? | |
ccc6cda3 | 1969 | |
bb70624e | 1970 | H1) How do I report bugs in bash, and where should I look for fixes and |
ccc6cda3 JA |
1971 | advice? |
1972 | ||
1973 | Use the `bashbug' script to report bugs. It is built and | |
1974 | installed at the same time as bash. It provides a standard | |
1975 | template for reporting a problem and automatically includes | |
1976 | information about your configuration and build environment. | |
1977 | ||
b72432fd | 1978 | `bashbug' sends its reports to bug-bash@gnu.org, which |
ccc6cda3 JA |
1979 | is a large mailing list gatewayed to the usenet newsgroup gnu.bash.bug. |
1980 | ||
1981 | Bug fixes, answers to questions, and announcements of new releases | |
1982 | are all posted to gnu.bash.bug. Discussions concerning bash features | |
1983 | and problems also take place there. | |
1984 | ||
1985 | To reach the bash maintainers directly, send mail to | |
b72432fd | 1986 | bash-maintainers@gnu.org. |
ccc6cda3 | 1987 | |
bb70624e | 1988 | H2) What kind of bash documentation is there? |
ccc6cda3 JA |
1989 | |
1990 | First, look in the doc directory in the bash distribution. It should | |
1991 | contain at least the following files: | |
1992 | ||
1993 | bash.1 an extensive, thorough Unix-style manual page | |
1994 | builtins.1 a manual page covering just bash builtin commands | |
b72432fd JA |
1995 | bashref.texi a reference manual in GNU tex`info format |
1996 | bashref.info an info version of the reference manual | |
ccc6cda3 JA |
1997 | FAQ this file |
1998 | article.ms text of an article written for The Linux Journal | |
1999 | readline.3 a man page describing readline | |
2000 | ||
b72432fd JA |
2001 | Postscript, HTML, and ASCII files created from the above source are |
2002 | available in the documentation distribution. | |
ccc6cda3 JA |
2003 | |
2004 | There is additional documentation available for anonymous FTP from host | |
cce855bc | 2005 | ftp.cwru.edu in the `pub/bash' directory. |
ccc6cda3 JA |
2006 | |
2007 | Cameron Newham and Bill Rosenblatt have written a book on bash, published | |
2008 | by O'Reilly and Associates. The book is based on Bill Rosenblatt's Korn | |
d166f048 | 2009 | Shell book. The title is ``Learning the Bash Shell'', and the ISBN number |
0628567a JA |
2010 | of the third edition, published in March, 2005, is 0-596-00965-8. Look for |
2011 | it in fine bookstores near you. This edition of the book has been updated | |
2012 | to cover bash-3.0. | |
ccc6cda3 | 2013 | |
b80f6443 | 2014 | The GNU Bash Reference Manual has been published as a printed book by |
3185942a JA |
2015 | Network Theory Ltd (Paperback, ISBN: 0-9541617-7-7, Nov. 2006). It covers |
2016 | bash-3.2 and is available from most online bookstores (see | |
b80f6443 JA |
2017 | http://www.network-theory.co.uk/bash/manual/ for details). The publisher |
2018 | will donate $1 to the Free Software Foundation for each copy sold. | |
2019 | ||
0628567a JA |
2020 | Arnold Robbins and Nelson Beebe have written ``Classic Shell Scripting'', |
2021 | published by O'Reilly. The first edition, with ISBN number 0-596-00595-4, | |
2022 | was published in May, 2005. | |
2023 | ||
2024 | Chris F. A. Johnson, a frequent contributor to comp.unix.shell and | |
2025 | gnu.bash.bug, has written ``Shell Scripting Recipes: A Problem-Solution | |
2026 | Approach,'' a new book on shell scripting, concentrating on features of | |
2027 | the POSIX standard helpful to shell script writers. The first edition from | |
2028 | Apress, with ISBN number 1-59059-471-1, was published in May, 2005. | |
2029 | ||
bb70624e | 2030 | H3) What's coming in future versions? |
ccc6cda3 | 2031 | |
f73dda09 | 2032 | These are features I hope to include in a future version of bash. |
ccc6cda3 | 2033 | |
17345e5a | 2034 | Rocky Bernstein's bash debugger (support is included with bash-4.0) |
ccc6cda3 | 2035 | |
bb70624e | 2036 | H4) What's on the bash `wish list' for future versions? |
ccc6cda3 JA |
2037 | |
2038 | These are features that may or may not appear in a future version of bash. | |
2039 | ||
ccc6cda3 | 2040 | breaking some of the shell functionality into embeddable libraries |
bb70624e | 2041 | a module system like zsh's, using dynamic loading like builtins |
bb70624e JA |
2042 | a bash programmer's guide with a chapter on creating loadable builtins |
2043 | a better loadable interface to perl with access to the shell builtins and | |
2044 | variables (contributions gratefully accepted) | |
f73dda09 | 2045 | ksh93-like `nameref' variables |
f73dda09 JA |
2046 | ksh93-like `xx.yy' variables (including some of the .sh.* variables) and |
2047 | associated disipline functions | |
2048 | Some of the new ksh93 pattern matching operators, like backreferencing | |
ccc6cda3 | 2049 | |
bb70624e | 2050 | H5) When will the next release appear? |
ccc6cda3 | 2051 | |
17345e5a | 2052 | The next version will appear sometime in 2009. Never make predictions. |
ccc6cda3 | 2053 | |
17345e5a | 2054 | This document is Copyright 1995-2009 by Chester Ramey. |
ccc6cda3 JA |
2055 | |
2056 | Permission is hereby granted, without written agreement and | |
2057 | without license or royalty fees, to use, copy, and distribute | |
2058 | this document for any purpose, provided that the above copyright | |
2059 | notice appears in all copies of this document and that the | |
2060 | contents of this document remain unaltered. |