How to generate a loop sequence using variable names in bash?

Can someone explain how I can use variable names in bash for a loop to generate a sequence of numbers?

for year in {1990..1997} do echo ${year} done 

Results:

1990 1991 1992 1993 1994 1995 1996 1997 1997

but

 year_start=1990 year_end=1997 for year in {${year_start}..${year_end}} do echo ${year} done 

Results:

{1990..1997}

How can I do a second job? otherwise I have to write tiring while loops. Thank you very much.

+6
source share
2 answers

You can use something like this

 for (( year = ${year_start}; year <=${year_end}; year++ )) do echo ${year} done 
Command

seq deprecated and best avoided.

http://www.cyberciti.biz/faq/bash-for-loop/

And if you really want to use the following syntax,

 for year in {${year_start}..${year_end}} 

change it like this:

 for year in $(eval echo "{$year_start..$year_end}") 

http://www.cyberciti.biz/faq/unix-linux-iterate-over-a-variable-range-of-numbers-in-bash/

Personally, I prefer to use for (( year = ${year_start}; year <=${year_end}; year++ )) .

+12
source

Try the following:

 start=1990; end=1997; for year in $(seq $start $end); do echo $year; done 
+3
source

Source: https://habr.com/ru/post/944765/


All Articles