A shell mechanism which is used to generate arbitrary strings.
Questions tagged [brace-expansion]
152 questions
68
votes
6 answers
How to create a sequence with leading zeroes using brace expansion
When I use the following, I get a result as expected:
$ echo {8..10}
8 9 10
How can I use this brace expansion in an easy way, to get the following output?
$ echo {8..10}
08 09 10
I now that this may be obtained using seq (didn't try), but that…
Bernhard
- 11,992
- 4
- 59
- 69
59
votes
7 answers
When do you use brace expansion?
I understand what brace expansion is, but I don't know how best to use it.
When do you use it?
Please teach me some convenient and remarkable examples if you have your own tip.
Benjamin
- 1,505
- 2
- 18
- 25
52
votes
5 answers
How can I use $variable in a shell brace expansion of a sequence?
I want to use $var in a shell brace expansion with a range, in bash.
Simply putting {$var1..$var2} doesn't work, so I went "lateral"...
The following works, but it's a bit kludgey.
# remove the split files
echo rm foo.{$ext0..$extN} rm-segments >…
Peter.O
- 32,426
- 28
- 115
- 163
29
votes
2 answers
What is the difference between `a[bc]d` (brackets) and `a{b,c}d` (braces)?
What is the difference between a[bc]d and a{b,c}d? Why do people use a{b,c}d when there is already a[bc]d?
Weijun Zhou
- 3,338
- 16
- 42
27
votes
6 answers
cat a very large number of files together in correct order
I have about 15,000 files that are named file_1.pdb, file_2.pdb, etc. I can cat about a few thousand of these in order by doing:
cat file_{1..2000}.pdb >> file_all.pdb
However, if I do this for 15,000 files, I get the error
-bash: /bin/cat:…
sodiumnitrate
- 955
- 3
- 10
- 15
22
votes
8 answers
Apply brace expansion in "reverse order"
For example {a..c}{1..3} expands to a1 a2 a3 b1 b2 b3 c1 c2 c3.
If I wanted to print a1 b1 c1 a2 b2 c2 a3 b3 c3, is there an analogous way to do that? What's the simplest way?
CcVHKakalLLOOPPOkKkkKk
- 585
- 1
- 4
- 15
21
votes
3 answers
Nested brace expansion mystery in Bash
This:
$ echo {{a..c},{1..3}}
produces this:
a b c 1 2 3
Which is nice, but hard to explain given that
$ echo {a..c},{1..3}
gives
a,1 a,2 a,3 b,1 b,2 b,3 c,1 c,2 c,3
Is this documented somewhere? The Bash Reference doesn't mention it (even…
xenoid
- 8,648
- 1
- 24
- 47
20
votes
5 answers
bash: Use a variable to store stderr|stdout redirection
Is there any way to redirect stdout and stderr via variable like adding command options in script?
For example I have a script:
#!/bin/bash -x
TEST=">/dev/null 2>&1"
OPT='-p -v'
mkdir $OPT 123/123/123 $TEST
I can see that OPT is replaced with -p…
rush
- 27,055
- 7
- 87
- 112
18
votes
3 answers
Using curly brackets (braces) to create folder structure with `mkdir -p`
As man mkdir states
-p, --parents
no error if existing, make parent directories as needed
When I ran this command
mkdir -p work/{F1,F2,F3}/{temp1,temp2}
It creates a folder structure like this work parent folder then F1,F2,F3 child…
Vishwanath Dalvi
- 4,346
- 5
- 20
- 17
15
votes
3 answers
Why doesn't systemctl\ {restart,status}\ sshd\; work?
The output of the above command when passed through echo is:
# echo systemctl\ {restart,status}\ sshd\;
systemctl restart sshd; systemctl status sshd;
Even if I paste the output to the terminal, the command works. But when I try to directly run the…
Somenath Sinha
- 403
- 5
- 14
14
votes
3 answers
Possible to use brace (permutation) and array expansion simultaneously?
Bash's {} brace expansion syntax allows for creating easy permutations
# echo {b,c,d}{a,e,i,o,u}
ba be bi bo bu ca ce ci co cu da de di do du
However it's not clear to me if/how it's possible to use this along with arrays except in very awkward use…
Catskul
- 1,898
- 1
- 14
- 19
14
votes
2 answers
bash combining wildcard expansion with brace expansion
I'm trying to expand a string involving a wildcard and a collection of extensions specified within braces. Nothing seems to work as the example below illustrates. the variable firstList expands fine, but neither secondList, thirdList or…
Leo Simon
- 443
- 1
- 5
- 11
13
votes
3 answers
Bash brace expansion after a path slash
I'm trying to copy a file to a different name into the same directory using brace expansion. I'm using bash 4.4.18.
Here's what I did:
cp ~/some/dir/{my-file-to-rename.bin, new-name-of-file.bin}
but I get this error:
cp: cannot stat…
nanangarsyad
- 265
- 3
- 8
13
votes
4 answers
When is {a,b,c} expanded in bash, when is it not?
A bash script that contains
for i in {a,b}-{1,2}; do
echo $i;
done
prints
a-1
a-2
b-1
b-2
when executed. This is what I expected - as the {a,b} construct is expanded.
However, when (another) script contains
v={a,b}-{1,2}
echo $v
it…
René Nyffenegger
- 2,201
- 2
- 23
- 28
12
votes
3 answers
Why does "cat {foo}" not output foo, but "cat {foo,bar}" does?
I was trying to concatenate text files in sub-folders and tried:
cat ./{mainfolder1,mainfolder2,mainfolder3}/{subfolder1}/book.txt > out$var
However this did not return anything. So, tried adding a non existing 'subfolder2'
cat…
CDPF
- 121
- 4