Brace expansion is one of the substitutions that Bash performs on its commands. It is entirely syntactic in nature (it has no understanding of context). Essentially, it is used as a typographic shortcut, to make commands more concise:

mv foobar.{o,exe} obj

# equivalent to:
mv foobar.o foobar.exe obj

A brace expansion results in one or more words. Any characters outside of the braces are repeated in every word. The braces determine the content that varies across the words.

Brace expansions can use comma-delimited strings, as shown in the example above. As of Bash 3.0, they can also use ranges of letters or numbers:

for i in {1..10}; do
  sleep 1
  echo $i
done

Brace expansion happens before parameter expansion, which means something like this will not do what you expect:

# won't work!
for i in {1..$n}; do

Brace expansion's range feature only works when the start and end of the range are constants, directly stated in the command.


CategoryShell