I have a simple code to assign variables in loop based on simple ariphmetic equaltion
# assign initial value
restr_start='25'
# assign a new variable, which is a number that will decrease initial value by 5
# keeping always the value of previous variable as restr_prev
for step in {1..4}; do
let "restr=(${restr_start} - (5 * ${step}))"
let "restr_prev=(${restr} + (5 * ${step}))"
echo this is $restr current restart
echo this is $restr_prev previous restart
done
From this script I am expecting to have:
this is 20 current restart
this is 25 previous restart
this is 15 current restart
this is 20 previous restart
this is 10 current restart
this is 15 previous restart
this is 5 current restart
this is 10 previous restart
however what I actually do have
this is 20 current restart
this is 25 previous restart
this is 15 current restart
this is 25 previous restart
this is 10 current restart
this is 25 previous restart
this is 5 current restart
this is 25 previous restart
why $restr_prev is usually unchanged? how I could modify the code, e.g. using something instead of let?
restr_previsn't correct. Please have a look at my answer for a more precise explanation.let; it's obsolete in the presence of POSIX arithmetic expressions.restr=$((restr_start - 5*step)).(( restr = restr_start - 5 * step ))- note in both these examples that the dollar signs and curly braces are omitted from the variable names.