Monday, 14 August 2017

Bash: Brace Expansion In Command Arguments

Cited from the Book "Pro Bash Programming"

The first expansion performed, brace expansion, is nonstandard (that is, it is not included in the POSIX specification). It operates on unquoted braces containing either a comma-separated list or a sequence. Each element becomes a separate argument.

A string before or after the brace expression will be included in each expanded argument.

pre{d,l}ate

Braces may be nested.

printf "%s\n" {{1..3},{a..d}}

Multiple braces within the same word are expanded recursively. The first brace expression is expanded, and then each of the resulting words is processed for the next brace expression. With the word {1..3}{a..c}, the first term is expanded, giving the following: 
1{a..c} 2{a..c} 3{a..c}

In version 4 of bash, further capabilities have been added to brace expansion. Numerical sequences
can be padded with zeros, and the increment in a sequence can be specified.

printf  "%s\n" {01..13..2}

Increments can also be used with alphabetic sequences.

printf  "%s\n" {a..f..2}



No comments:

Post a Comment