Sequences expansion and variable in bash [duplicat

2020-01-29 01:30发布

I am having a problem with builtin sequences (ie: not using seq) in Bash when the seq number is a variable. For example, this works and print me 1 2 3:

for i in {1..3};do echo $i;done

but this :

bash-3.2$ a=3;for i in {1..$a};do echo $i;done

fail and print me {1..3} only

This works with ZSH and I know I have an alternative to make a counter thing but wondering if this is a bug or a brace expansion feature!

7条回答
别忘想泡老子
2楼-- · 2020-01-29 02:24

I also needed to do somenthing like:

n=some number; {1..$n..increment}

so I used this workaround:

n=100  
i=1
while [ $i -lt $n ]
do
echo $i
i=$(( $i+1 ))
done
查看更多
登录 后发表回答