brace.txt 2.6 KB

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