1

In zsh, one can create an expression of {nx..ny}, for example to select files x to y inside a folder.

For example, {1..50} selects items, files, etc. from 1 to 50.

How can I concatenate two two brace expansions into one? Example: I would like to select {1..50} and {60..100} for one and the same output.

Philipp
  • 335
  • 2
  • 4
  • 12
  • What's wrong with using two brace expansions, as in `echo {1..50} {60..100}`? – Jens Feb 18 '23 at 12:17
  • You are correct insofar as ```echo {1..50} {60..100}``` works just fine. However, I had to insert the two brace expansions into another script or code, and then this script failed. The solution that someone provided below, that is, ```{{1..50},{60..100}}``` works just fine in this case. – Philipp Feb 18 '23 at 13:30

1 Answers1

3

You can nest brace expansions, so this will work:

> print {{1..50},{60..100}}
1 2 3 (lots of numbers) 49 50 60 61 (more numbers) 99 100

Brace expansions support lists as well as sequences, and can be included in strings:

> print -l file{A,B,WORD,{R..T}}.txt
fileA.txt
fileB.txt
fileWORD.txt
fileR.txt
fileS.txt
fileT.txt

Note that brace expansions are not glob patterns. The {n..m} expansion will include every value between the start and end values, regardless of whether a file exists by that name. For finding files in folders, the <-> glob expression will usually work better:

> touch 2 3 55 89
> ls -l <1-50> <60-100>
-rw-r--r--  1 me  grp  0 Feb 18 06:52 2
-rw-r--r--  1 me  grp  0 Feb 18 06:52 3
-rw-r--r--  1 me  grp  0 Feb 18 06:52 89
Gairfowl
  • 2,226
  • 6
  • 9
  • Thank you. ```{{1..50},{60..100}}``` solved my problem. So what I needed were additional braces around the two expansions including a comma between both. – Philipp Feb 18 '23 at 13:31