Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 1 | Brace Expansion |
| 2 | |
| 3 | Brace expansion is a mechanism by which arbitrary strings may be gener- |
| 4 | ated. This mechanism is similar to pathname expansion, but the file- |
| 5 | names generated need not exist. Patterns to be brace expanded take the |
| 6 | form of an optional preamble, followed by either a series of comma-sep- |
| 7 | arated strings or a sequence expression between a pair of braces, fol- |
| 8 | lowed by an optional postscript. The preamble is prefixed to each |
| 9 | string contained within the braces, and the postscript is then appended |
| 10 | to each resulting string, expanding left to right. |
| 11 | |
| 12 | Brace expansions may be nested. The results of each expanded string |
| 13 | are not sorted; left to right order is preserved. For example, |
| 14 | a{d,c,b}e expands into `ade ace abe'. |
| 15 | |
| 16 | A sequence expression takes the form {x..y}, where x and y are either |
| 17 | integers or single characters. When integers are supplied, the expres- |
| 18 | sion expands to each number between x and y, inclusive. When charac- |
| 19 | ters are supplied, the expression expands to each character lexico- |
| 20 | graphically between x and y, inclusive. Note that both x and y must be |
| 21 | of the same type. |
| 22 | |
| 23 | Brace expansion is performed before any other expansions, and any char- |
| 24 | acters special to other expansions are preserved in the result. It is |
| 25 | strictly textual. Bash does not apply any syntactic interpretation to |
| 26 | the context of the expansion or the text between the braces. |
| 27 | |
| 28 | A correctly-formed brace expansion must contain unquoted opening and |
| 29 | closing braces, and at least one unquoted comma or a valid sequence |
| 30 | expression. Any incorrectly formed brace expansion is left unchanged. |
| 31 | A { or , may be quoted with a backslash to prevent its being considered |
| 32 | part of a brace expression. To avoid conflicts with parameter expan- |
| 33 | sion, the string ${ is not considered eligible for brace expansion. |
| 34 | |
| 35 | This construct is typically used as shorthand when the common prefix of |
| 36 | the strings to be generated is longer than in the above example: |
| 37 | |
| 38 | mkdir /usr/local/src/bash/{old,new,dist,bugs} |
| 39 | or |
| 40 | chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}} |
| 41 | |
| 42 | Brace expansion introduces a slight incompatibility with historical |
| 43 | versions of sh. sh does not treat opening or closing braces specially |
| 44 | when they appear as part of a word, and preserves them in the output. |
| 45 | Bash removes braces from words as a consequence of brace expansion. |
| 46 | For example, a word entered to sh as file{1,2} appears identically in |
| 47 | the output. The same word is output as file1 file2 after expansion by |
| 48 | bash. If strict compatibility with sh is desired, start bash with the |
| 49 | +B option or disable brace expansion with the +B option to the set com- |
| 50 | mand |