How do I iterate over a range of numbers defined by variables in Bash?

后端 未结 20 1963
予麋鹿
予麋鹿 2020-11-21 05:19

How do I iterate over a range of numbers in Bash when the range is given by a variable?

I know I can do this (called \"sequence expression\" in the Bash documentatio

20条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 05:42

    if you don't wanna use 'seq' or 'eval' or jot or arithmetic expansion format eg. for ((i=1;i<=END;i++)), or other loops eg. while, and you don't wanna 'printf' and happy to 'echo' only, then this simple workaround might fit your budget:

    a=1; b=5; d='for i in {'$a'..'$b'}; do echo -n "$i"; done;' echo "$d" | bash

    PS: My bash doesn't have 'seq' command anyway.

    Tested on Mac OSX 10.6.8, Bash 3.2.48

提交回复
热议问题