Brace expansion with range in fish shell
Asked Answered
D

1

39

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?

Danged answered 25/12, 2013 at 8:43 Comment(1)
As an aside for those less familiar with fish: fish does support brace expansion, but only with lists (e.g., echo b{ar,az}), not ranges.Fossil
S
63

The short answer is echo bunny(seq 6)

Longer answer: In keeping with fish's philosophy of replacing magical syntax with concrete commands, we should hunt for a Unix command that substitutes for the syntactic construct {1..6}. seq fits the bill; it outputs numbers in some range, and in this case, integers from 1 to 6. fish (to its shame) omits a help page for seq, but it is a standard Unix/Linux command.

Once we have found such a command, we can leverage command substitutions. The command (foo)bar performs command substitution, expanding foo into an array, and may result in multiple arguments. Each argument has 'bar' appended.

Sidney answered 25/12, 2013 at 9:45 Comment(7)
@chepner: It doesn't matter. fish shell automatically defines seq if it doesn't exist, as certain parts of fish source depend on it to exist.Avertin
Since this is the first result for "fish brace expansion"... It is worth noting that fish does do simple brace expansion with commas. e.g. echo {foo,bar} or mkdir --parents /tmp/{folder1,folder2,folder3} results in /tmp/folder1, /tmp/folder2, /tmp/folder3. More info @ fishshell.com/docs/2.0/index.html#expandBrowning
@wviana Try jot -c 26 a z 1Verlie
@wviana: a self contained answer: printf '%b' (printf '\\\\x%02X ' (seq 97 122))Pairs
What about touch file{01..10}, how do you do that in fish shell?Snashall
You can pass a format string to seq. touch file(seq -f %02g 10)Sidney
@Snashall this should also work touch file(seq -w 10)Angeliqueangelis

© 2022 - 2024 — McMap. All rights reserved.