Questions tagged [brace-expansion]

Brace expansion converts "{a,b}{c,d}" to "ac ad bc bd" as an evaluation phase of a command in most Bourne-derived shells.

Brace expansion is one of the series of transformations that commands go through when evaluated in a shell.

It allows to easily multiply strings that share a common format. For example, in bash:

  • {a,b}{c,d} expands to ac ad bc bd
  • /dir{A,B}/file{1..3} expands to /dirA/file1 /dirA/file2 /dirA/file3 /dirB/file1 /dirB/file2 /dirB/file3, whether or not such paths exist.
134 questions
49
votes
5 answers

Using a variable in brace expansion range fed to a for loop

Here is myscript.sh #!/bin/bash for i in {1..$1}; do echo $1 $i; done If I run myscript.sh 3 the output is 3 {1..3} instead of 3 1 3 2 3 3 Clearly $3 contains the right value, so why doesn't for i in {1..$1} behave the same as if I had…
spraff
  • 32,570
  • 22
  • 121
  • 229
37
votes
1 answer

Brace expansion with range in fish shell

In bash, I can do the following $ echo bunny{1..6} bunny1 bunny2 bunny3 bunny4 bunny5 bunny6 Is there a way to achieve the same result in fish?
workflow
  • 2,536
  • 2
  • 14
  • 11
26
votes
2 answers

Bash brace expansion not working on Dockerfile RUN command

I'm running the following RUN command in my Dockerfile, expecting a "logs" directory to be created under each of the listed subdirectories: RUN mkdir -p…
Ates Goral
  • 137,716
  • 26
  • 137
  • 190
23
votes
3 answers

How to handle shell expansions in GNU Make under Ubuntu?

Given this very simple Makefile: all: @mkdir -pv test/{a,b} I get this output on OS X 10.6.8 and CentOS 5.5: mkdir: created directory `test' mkdir: created directory `test/a' mkdir: created directory `test/b' But on Ubuntu 11.04 I get…
Adam Lindberg
  • 16,447
  • 6
  • 65
  • 85
19
votes
3 answers

Powershell equivalent of Bash Brace Expansion for generating lists/arrays

When writing a Bash script you can use brace expansion to quickly generate lists: What is the simplest way to generate a similar list in Powershell? I can use the .. or , operators to generate an array, but how can I prefix the items with a static…
Greg Bray
  • 14,929
  • 12
  • 80
  • 104
15
votes
5 answers

Brace expansion with a Bash variable - {0..$foo}

WEEKS_TO_SAVE=4 mkdir -p weekly.{0..$WEEKS_TO_SAVE} gives me a folder called weekly.{0..4} Is there a secret to curly brace expansion while creating folders I'm missing?
xref
  • 1,707
  • 5
  • 19
  • 41
14
votes
2 answers

(zsh brace expansion | seq) for character lists - how?

Bash allows me to write the statement, $ for i in {h..k} ; do echo $i ; done but zsh only allows number list expansion such as {8..13}. What's the best workaround? Something like seq for characters...
nidi
  • 270
  • 4
  • 8
12
votes
3 answers

Execute command multiple times with curly brackets arguments list

When I needed to run a command multiple times with a different argument I used this approach (without understanding it fully): touch {a,b,c} Which is equivalent of: touch a touch b touch c I think the same could be accomplished with the following…
10
votes
9 answers

Bash: Brace expansion in scripts not working due to unwanted escaping

I want to do something like this in a bash script. I'm using bash 4.1.10. # rm -rf /some/path/{folder1,folder2,folder3} Works nicely (and as expected) from the shell itself. It deletes the 3 desired folders leaving all others untouched. When I put…
Roland
  • 125
  • 1
  • 6
8
votes
4 answers

Tricky brace expansion in shell

When using a POSIX shell, the following touch {quick,man,strong}ly expands to touch quickly manly strongly Which will touch the files quickly, manly, and strongly, but is it possible to dynamically create the expansion? For example, the following…
dreamlax
  • 93,976
  • 29
  • 161
  • 209
6
votes
2 answers

What is the difference between these two things?

I just wanted to know what is the difference between: echo {$number1..$number2} AND eval echo {$number1..$number2} Of course, imagine that there is a value in $number1 and $number2. With the first option is just not working, but with the second…
6
votes
2 answers

Understanding code ({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1})

I saw the following code in Bash shell Decimal to Binary conversion and I was wondering how it works? I tried googling with no avail. D2B=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}) echo ${D2B[7]} What does the code above do?
6
votes
4 answers

When do you use brace expansion?

I understood what brace expansion is. But I don't know where I use that. When do you use it? Please give me some convenient examples. Thanks.
Benjamin
  • 10,085
  • 19
  • 80
  • 130
5
votes
3 answers

Generate all permutations of up to `n` letters

I'd like to generate all permutations of letters up to length n For example, for the argument 2 I'd like obtain a list like a aa .. az ... z za .. zz I tried using a for loop to generate n increasingly larger brace expansions by repeating {a..z}…
mdcq
  • 1,593
  • 13
  • 30
5
votes
2 answers

How to store curly brackets in a Bash variable

I am trying to write a bash script. I am not sure why in my script: ls {*.xml,*.txt} works okay, but name="{*.xml,*.txt}" ls $name doesn't work. I get ls: cannot access {*.xml,*.txt}: No such file or directory
twq
  • 53
  • 4
1
2 3
8 9