# 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?

You could do:

``````\$ eval echo '{a..c}'{1..3}
a1 b1 c1 a2 b2 c2 a3 b3 c3
``````

Which then tells the shell to evaluate:

``````echo {a..c}1 {a..c}2 {a..c}3
``````

For this particular case, I think that the option given by Stéphane Chazelas is the best one.

On the other hand, when you expand more complex things, this option doesn't scale well. So, you can achieve the same with this:

``````\$ printf '%s\0' {a..c}{1..3} | sort -zk 1.2,1.2 | tr '\0' ' '
``````

which returns:

``````a1 b1 c1 a2 b2 c2 a3 b3 c3
``````

Seems a little messy, but now, I have a huge control in the order, just changing two chars in the command above; for example:

``````\$ echo {a..b}{1..2}{a..b}{1..2}
``````

this will expand to:

``````a1a1 a1a2 a1b1 a1b2 a2a1 a2a2 a2b1 a2b2 b1a1 b1a2 b1b1 b1b2 b2a1 b2a2 b2b1 b2b2
``````

Suppose I want all the `1` in the second expansion, then the `2`:

``````\$ printf '%s\0' {a..b}{1..2}{a..b}{1..2} | sort -zk 1.2,1.2 | tr '\0' ' '
a1a1 a1a2 a1b1 a1b2 b1a1 b1a2 b1b1 b1b2 a2a1 a2a2 a2b1 a2b2 b2a1 b2a2 b2b1 b2b2
``````

Suppose I want all the `a` in the third expansion, then the `b`:

``````\$ printf '%s\0' {a..b}{1..2}{a..b}{1..2} | sort -zk 1.3,1.3 | tr '\0' ' '
a1a1 a1a2 a2a1 a2a2 b1a1 b1a2 b2a1 b2a2 a1b1 a1b2 a2b1 a2b2 b1b1 b1b2 b2b1 b2b2
``````

Suppose I want all the `1` in the fourth expansion, then the `2`:

``````\$ printf '%s\0' {a..b}{1..2}{a..b}{1..2} | sort -zk 1.4,1.4 | tr '\0' ' '
a1a1 a1b1 a2a1 a2b1 b1a1 b1b1 b2a1 b2b1 a1a2 a1b2 a2a2 a2b2 b1a2 b1b2 b2a2 b2b2
``````

Suppose I want all the `1a` in the middle, then `1b`, then `2a`, then `2b`:

``````\$ printf '%s\0' {a..b}{1..2}{a..b}{1..2} | sort -zk 1.2,1.3 | tr '\0' ' '
a1a1 a1a2 b1a1 b1a2 a1b1 a1b2 b1b1 b1b2 a2a1 a2a2 b2a1 b2a2 a2b1 a2b2 b2b1 b2b2
``````

You can even, just as easily, reverse any order in the expansions above, just adding an `r` to the previous command; for example, the last one:

``````\$ printf '%s\0' {a..b}{1..2}{a..b}{1..2} | sort -rzk 1.2,1.3 | tr '\0' ' '
b2b2 b2b1 a2b2 a2b1 b2a2 b2a1 a2a2 a2a1 b1b2 b1b1 a1b2 a1b1 b1a2 b1a1 a1a2 a1a1
``````

Note_1: usually, if this final expansion is going to be used as a list of arguments, the trailing space is not a problem; but if you want to get rid of it, you can add, to any of the commands above, for example `| sed 's/ \$//'`; or even `| sed 's/ \$/\n/'`, to change that trailing space for a `newline`

Note_2: In the examples above, I've use subsets of two elements (i.e.: {a,b} and {1,2}) just for simplicity in the proof of concept: you can use subsets of any finite length, and the corresponding command, would be comparable.

# bash, ksh, zsh

A one liner that works in (bash, ksh, zsh) (not all shells can do "Brace expansion" in reverse order):

``````\$ echo {3..1}{c..a} | rev
a1 b1 c1 a2 b2 c2 a3 b3 c3
``````

An alternative that use `eval` (which is still for bash, ksh, zsh and may be more cryptic) is:

``````\$ eval echo '{a..c}'{1..3}
a1 b1 c1 a2 b2 c2 a3 b3 c3
``````

To understand what happens, replace `eval` with `echo`:

``````\$ echo echo '{a..c}'{1..3}
echo {a..c}1 {a..c}2 {a..c}3
``````

The command executed (after eval expansion) is actually `echo {a..c}1 {a..c}2 {a..c}3`. Which expands as you want/need.

# all shells

There are several shells without "brace expansions", so, not possible to use that for "all shells". We need a loop (with a trailing white space):

``````\$ for i in 1 2 3; do for j in a b c; do printf "%s%s " "\$j" "\$i"; done; done; echo
a1 b1 c1 a2 b2 c2 a3 b3 c3
``````

If you must have no trailing space added:

``````s=""
for i in 1 2 3; do
for j in a b c; do
printf "%s%s%s" "\$s" "\$j" "\$i"
s=" "
done
done
echo
``````

Prints

``````a1 b1 c1 a2 b2 c2 a3 b3 c3
``````

IF you need to do this for many values we need to use something similar to the brace expansion to generate a list of numbers `\$(seq 10)`. And, as seq can not generate a list of letters, we need to convert to ascii the numbers generated:

``````s=""
for i in \$(seq 4); do
for j in \$(seq 5); do
printf "%s\\\$(printf %03o \$((96+j)))%s" "\$s" "\$i"
s=" "
done
done
echo
``````

prints:

``````a1 b1 c1 d1 e1 a2 b2 c2 d2 e2 a3 b3 c3 d3 e3 a4 b4 c4 d4 e4
``````
• You can also add `yash -o braceexpand` to the list. Commented Jun 20, 2019 at 6:45
• @StéphaneChazelas I am not sure I should. The command `yash -o braceexpand -c 'echo {3..1}{c..a}'` prints `3{c..a} 2{c..a} 1{c..a}` in linux. Not a full "brace expansion".
– user232326
Commented Jun 20, 2019 at 15:10
``````{a..c}1 {a..c}2 {a..c}3
``````

The brace expansions in `{a..c}{1..3}` are expanded left to right, so you first get `a{1..3} b{1..3} c{1..3}` and then the letters are combined with the numbers into `a1 a2 a3 b1 b2 b3 c1 c2 c3`. To get the order you want, you will have to use the slightly longer expression above.

• If you wanted to do it for a large range of "numbers" it wouldn't be practical anymore. Commented Jun 19, 2019 at 21:21
• @RUBENGONÇALOMOROUÇO No it wouldn't, and if you are doing it for a large range of numbers, I would suggest using an alternative approach, like a double loop. That would work for many thousands of combinations, whereas a brace expansions my trigger "argument list too long" in certain contexts. Commented Jun 19, 2019 at 21:22

Using a loop:

``````for n in {1..3}; do printf '%s\n' {a..c}"\$n"; done
``````

This will loop through your first expansion and then expand each character with the second.

If you need the output all on one line you can remove the `\n`:

``````for n in {1..3}; do printf '%s ' {a..c}"\$n"; done
``````

This won't give you a trailing newline but if you are passing it to a command or variable that shouldn't be an issue.

• Why so many down votes for a loop solution?
– user232326
Commented Jun 20, 2019 at 16:32
• Duh I must have read the question wrong. Updated Commented Jun 21, 2019 at 1:51

This works for your simple case and can be extended, but it would quickly get out of hand. More complex cases that this wouldn't work for are easy to construct.

Reverse the order of the brace expansions, then swap the characters:

``````echo {1..3}{a..c} | sed -E 's/(.)(.)( ?)/\2\1\3/g'
``````
• @muru: Oops. Fixed. Thanks. Commented Jun 22, 2019 at 16:35
• @Isaac: I fixed the trailing space. Commented Jun 22, 2019 at 22:29

One simple method would be to use sort (the 1.2,1.2 means that you take one character at the second position and end at the same place).

``````\$ for i in {a..c}{1..3}; do echo \$i; done|sort -n -k1.2,1.2
a1
b1
c1
a2
b2
c2
a3
b3
c3

``````

If you want them in one line, you can use tr like so:

``````\$ for i in {a..c}{1..3}; do echo \$i; done|sort -n -k1.2,1.2|tr '\n' ' '
a1 b1 c1 a2 b2 c2 a3 b3 c3
``````

Done by below method

``````for i in {1..10}; do for j in {a..c}; do echo \$j\$i; done; done| perl -pne "s/\n/ /g"
``````

output

``````a1 b1 c1 a2 b2 c2 a3 b3 c3 a4 b4 c4 a5 b5 c5 a6 b6 c6 a7 b7 c7 a8 b8 c8 a9 b9 c9 a10 b10 c10
``````
• consider also `for i in {1..10}; do for j in {a..c}; do printf '%s ' "\$j\$i"; done; done;echo` Commented Jun 20, 2019 at 16:51