bash-大括号扩展不扩展?

用户1291

在Kubuntu 15.10上

echo $BASH_VERSION
4.3.42(1)-release

我尝试

reps=1
threads={1}{10..60..10}

for((r=0;r<$reps;r++))
do
    tCount=0
    for t in $threads
    do
        echo "t=$t, tCount=${tCount}"
        #do something funny with it
        ((tCount++))
    done
done

它产生一条线

t={1}{10..60..10}, tCount=0

我该如何工作?

编辑

我预计

t=1, tCount=0
t=10, tCount=1
t=20, tCount=2
t=30, tCount=3
t=40, tCount=4
t=50, tCount=5
t=60, tCount=6

更新

注意 threads=({1}{10..60..10})

然后 for t in ${threads[@]}

10..60..10使用字符串作为范围的前缀{1}

(即{1}10,{1}20,..,{1}60

罗斯兰·奥斯曼诺夫(Ruslan Osmanov)

{1}表达式只是一个字符串,因为它不符合大括号扩展语法:

序列表达式采用的形式{X..Y[..INCR]},其中X和Y是整数或单个字符,而INCR(可选的增量)是整数。

大括号扩展是变量扩展之前执行的,因此不能仅通过引用变量来扩展大括号:

扩展的顺序是:大括号扩展;波浪线扩展,参数和变量扩展,算术扩展和命令替换(以从左到右的方式完成);分词 和文件名扩展。

是直接编写括号扩展,还是使用eval(通常不建议)。

例子:

tCount=0
for t in {1,{10..60..10}}; do
  echo "t=$t tCount=$tCount"
  (( tCount++ ))
done

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章