序列扩展和变量在bash [英] Sequences expansion and variable in bash
问题描述
我有内建序列的问题(即:不使用SEQ)中的Bash当SEQ数量是一个变量。例如,这个工程并打印了我1 2 3:
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
但这:
的bash-3.2 $一= 3;因为我在{1 .. $ A};做回声$我;做了
bash-3.2$ a=3;for i in {1..$a};do echo $i;done
失败,并打印了我{} 1..3仅
fail and print me {1..3} only
这适用于ZSH,我知道我有一个替代方案,使计数器的事情,但不知道是否这是一个错误或括号扩展功能!
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!
推荐答案
在bash中,括号扩展是变量扩展之前执行。请参见 shell扩展的订货。
In Bash, brace expansion is performed before variable expansion. See Shell Expansions for the order.
$ a=7; echo {1..3} {4..$a}
1 2 3 {4..7}
如果你想使用一个变量,使用C语言风格的为
循环在肖恩的答案。
If you want to use a variable, use C-style for
loops as in Shawn's answer.
这篇关于序列扩展和变量在bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!