0

I want to write a for loop that starts from a date (received as string) and cycles through all dates for a set number of days. I solved the problem in the end but I don't understand why another attempt fails the way it does and I'm hoping for an explanation.

This the end solution:

a=3
start_date="2017-09-01"
for ((i=0;i<$a;i++));
do
cur_date=`date -d $start_date" -"$i" days"`
echo $cur_date
done

Resulting in:

Fri Sep 1 00:00:00 CEST 2017 
Sat Sep 2 00:00:00 CEST 2017 
Sun Sep 3 00:00:00 CEST 2017

Which is ok, however this:

for i in {0..$a}
do 
cur_date=`date -d $start_date" "$i" days"`
echo $cur_date
done

Results in:

date: invalid date ‘2017-09-01 {0..3} days’

While this:

for i in {0..3}
do 
cur_date=`date -d $start_date" "$i" days"`
echo $cur_date
done

Returns the correct sequence So, the question is, why using a variable that has the same value instead of the just value breaks the code?

Thanks

UrVal
  • 351
  • 6
  • 17

0 Answers0