SoloCDM wrote:
> 
> Every time I have a bash while, for or any other type of loop,
> with numbers processed and saved to a variable, the numbers are
> not in the variable outside the loop.  Although, the numbers are
> capable of use and manipulation in the variable, for reassigning
> the variable or recall, anytime before the loop ends.  How can the
> variable's content be available outside the loop?

bash allows access to in-loop defined/updated variables outside loops EXCEPT
those defined/updated inside functions; is this your problem?

Example:

#!/bin/bash
 
i=1
j=10
li=5
 
myloop() {
  li=$li*$li  # Note:  li takes from global but does not affect global
  lj=8        # Note:  lj is not defined globally
}
 
echo -n "i:k:li = "
while [ $i -le $j ]; do
  let k=${i}*$li
  echo -n "$i:$k:$li "
  let i=$i+1
done
echo
echo "Note:  k not defined outside the loop but is available here..."
echo "i,j,[k],(li) at loop exit:  $i $j [$k] ($li)"
 
for n in 1 2 3; do
  let n=$n*$li
  echo "li defined outside function gives: $n"
done
 
# this loop gives errors 'cuz lj defined inside function
for n in 1 2 3; do
  let n=$n*$lj
  echo "lj defined INside function gives: $n"
done

Which gives:

# ./numtst
i:k:li = 1:5:5 2:10:5 3:15:5 4:20:5 5:25:5 6:30:5 7:35:5 8:40:5 9:45:5 10:50:5
Note:  k not defined outside the loop but is available here...
i,j,[k],(li) at loop exit:  11 10 [50] (5)
li defined outside function gives: 5
li defined outside function gives: 10
li defined outside function gives: 15
./numtst: let: n=1*: syntax error: operand expected (error token is "*")
lj defined INside function gives: 1
./numtst: let: n=2*: syntax error: operand expected (error token is "*")
lj defined INside function gives: 2
./numtst: let: n=3*: syntax error: operand expected (error token is "*")
lj defined INside function gives: 3

Error is due to n=$n*<blank_cuz_undefined_here>

HTH,
Pierre

Reply via email to