Variables in bash seq replacement ({1..10})

BashSeq

Bash Problem Overview


Is it possible to do something like this:

start=1
end=10
echo {$start..$end}
# Ouput: {1..10}
# Expected: 1 2 3 ... 10 (echo {1..10})

Bash Solutions


Solution 1 - Bash

In bash, brace expansion happens before variable expansion, so this is not directly possible.

Instead, use an arithmetic for loop:

start=1
end=10
for ((i=start; i<=end; i++))
do
   echo "i: $i"
done

###OUTPUT

i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
i: 10

Solution 2 - Bash

You should consider using seq(1). You can also use eval:

eval echo {$start..$end}

And here is seq

seq -s' ' $start $end

Solution 3 - Bash

You have to use eval:

eval echo {$start..$end}

Solution 4 - Bash

If you don't have seq, you might want to stick with a plain for loop

for (( i=start; i<=end; i++ )); do printf "%d " $i; done; echo ""

Solution 5 - Bash

I normally just do this:

echo `seq $start $end`

Solution 6 - Bash

Are you positive it has be BASH? ZSH handles this the way you want. This won't work in BASH because brace expansion happens before any other expansion type, such as variable expansion. So you will need to use an alternative method.

Any particular reason you need to combine brace and variable expansion? Perhaps a different approach to your problem will obviate the need for this.

Solution 7 - Bash

use -s ex: seq -s ' ' 1 10 output: 1 2 3 4 5 6 7 8 9 10

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTyiloView Question on Stackoverflow
Solution 1 - BashanubhavaView Answer on Stackoverflow
Solution 2 - BashcnicutarView Answer on Stackoverflow
Solution 3 - BashbmkView Answer on Stackoverflow
Solution 4 - Bashglenn jackmanView Answer on Stackoverflow
Solution 5 - BashopsguyView Answer on Stackoverflow
Solution 6 - BashSpencer RathbunView Answer on Stackoverflow
Solution 7 - Bashlantian0811View Answer on Stackoverflow