The command echo {1..3}-{1,2}
prints 1-1 1-2 2-1 2-2 3-1 3-2
. I understand the way those curly braces can be used. But what actually are they?
Is it the job of sh
/ bash
to parse/expand them and deliver the expanded version to the executed program?
If so, what other tricks can it do and is there a specification?
Also, is there a name for it?
Is ls *.txt
handled in a similar way internally?
Is there a way to achieve an n-times repetition of an argument? Like (not working, of course, only a concept): cat test.pdf{*3}
⇒ cat test.pdf test.pdf test.pdf
?
Best Answer
They are called brace expansion.
It is one of several expansions done by
bash
,zsh
andksh
, filename expansion*.txt
being another one of them. Brace expansion is not covered by the POSIX standard and is thus not portable. You can read on this in bash manual.On @Arrow's suggestion: in order to get
cat test.pdf test.pdf test.pdf
with brace expansion alone, you would have to use this "hack":Some common uses:
Or another "hack" to print a string 10 times:
Be aware that brace expansion in
bash
is done before parameter expansion, therefore a common mistake is:(the
ksh93
shell copes with this though)