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条回答
Emotional °昔
2楼-- · 2020-01-29 01:59

Other option is to use seq command:

a=3; for i in $(seq 1 $a);do echo $i;done
查看更多
Deceive 欺骗
3楼-- · 2020-01-29 02:00

An alternative would be to use the double-parenthesis construct which allows C-style loops:

A=3
for (( i=1; i<=$A; i++ )); do
    echo $i
done
查看更多
狗以群分
4楼-- · 2020-01-29 02:05
$ num=3
$ for i in $( eval echo {1..$num});do echo $i;done
1
2
3
查看更多
手持菜刀,她持情操
5楼-- · 2020-01-29 02:05

try this:

$ start=3
$ end=5
$ echo {$(echo $start)..$(echo $end)}
查看更多
贼婆χ
6楼-- · 2020-01-29 02:07
    #!/bin/bash - see comment for list of obsolete bash constructs
    function f_over_range {
        for i in $(eval echo {$1..$2}); do
            f $i
        done
    }

    function f {
        echo $1
    }

    #POSIX-compliant
    f_over_range() {
        for i in $(eval echo {$1..$2}); do
            f $i
        done
    }

    f() {
        echo $1
    }


    f_over_range 0 5
    f_over_range 00 05

Notes:

  • Using eval exposes command injection security risk
  • Linux will print "00\n01\n02..etc", but OSX will print "0\n1\n2\n...etc"
  • Using seq or C-style for loops won't match brace expansion's handling of leading zeros
查看更多
干净又极端
7楼-- · 2020-01-29 02:18

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}

If you want to use a variable, use C-style for loops as in Shawn's answer.

查看更多
登录 后发表回答