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

2

14

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...

Villainy answered 7/3, 2010 at 1:5 Comment(0)
A
9

As this is still a top google result, an updated answer:

The current release supports bash style {c1..c2} where c1 and c2 are characters:

An expression of the form ‘{c1..c2}’, where c1 and c2 are single characters (which may be multibyte characters), is expanded to every character in the range from c1 to c2 in whatever character sequence is used internally. For characters with code points below 128 this is US ASCII (this is the only case most users will need). If any intervening character is not printable, appropriate quotation is used to render it printable. If the character sequence is reversed, the output is in reverse order, e.g. ‘{d..a}’ is substituted as ‘d c b a’.

This is definitely present in 5.0.7 onwards. I can't seem to find when this was introduced in the zsh release history, but the first archived version documenting it indicates it was introduced between July 2012 and November 2014.

Avaricious answered 26/8, 2015 at 13:59 Comment(1)
I can confirm that this feature is not in 5.0.5 (the version that ships with Mac OS X).Emmerie
B
27
zsh$ setopt BRACE_CCL
zsh$ echo {a-k}
a b c d e f g h i j k
zsh$ echo {1-9}
1 2 3 4 5 6 7 8 9

From ZSH Documentation:

BRACE_CCL

Expand expressions in braces which would not otherwise undergo brace expansion to a lexically ordered list of all the characters. See Brace Expansion.

Broderick answered 7/3, 2010 at 1:33 Comment(3)
JFTR zsh git indicates that brace_ccl was present in the initial commit which was done in 1999 for version 3.1.5Come
Is there a way to have a range like {1-12} to expand to 1 2 3 ... 9 10 11 12?Flotsam
@KedarMhaswade: Why doesn't the {1..12} syntax suit your needs?Untrue
A
9

As this is still a top google result, an updated answer:

The current release supports bash style {c1..c2} where c1 and c2 are characters:

An expression of the form ‘{c1..c2}’, where c1 and c2 are single characters (which may be multibyte characters), is expanded to every character in the range from c1 to c2 in whatever character sequence is used internally. For characters with code points below 128 this is US ASCII (this is the only case most users will need). If any intervening character is not printable, appropriate quotation is used to render it printable. If the character sequence is reversed, the output is in reverse order, e.g. ‘{d..a}’ is substituted as ‘d c b a’.

This is definitely present in 5.0.7 onwards. I can't seem to find when this was introduced in the zsh release history, but the first archived version documenting it indicates it was introduced between July 2012 and November 2014.

Avaricious answered 26/8, 2015 at 13:59 Comment(1)
I can confirm that this feature is not in 5.0.5 (the version that ships with Mac OS X).Emmerie

© 2022 - 2024 — McMap. All rights reserved.