How to retain the value of a variable inside for loop in shell script
<p>Below is my shell script code:</p>

<pre><code>#!/bin/bash
first=0

my_for_loop()
{

 first="$1";
 if [ $first -eq 0 ]
 then
   return 1;
 elif [ $first -eq 1 ]
then
   return 0;
 fi
}

while read line; do
for word in $line; do
    echo "word = '$word'"; ( echo "$word" | grep -Eq  "\([0-9]+\)" ) &amp;&amp; ( word="${word%\)}"; word="`echo ${word} | cut -d'(' -f 2`"; echo "no = $word";
my_for_loop "$first"; first="$?"; echo "foo = $first")
done
echo "first value is $first" )
done
</code></pre>

<p>Basically I'm trying to read the words from the input and checking whether it is of the form "([0-9]+)" {e.g. (120) } if so, extracting the numerals i.e 120  </p>

<p>The problem arises when I'm trying to set the variable "first" to 1 on the odd no of occurence of the pattern, and to 0 on eventh occurence</p>

<p>I tried Process Substitution, but not working porperly.</p>

<p>Can you help me with setting the boolean value inside the for loop such that it retains the value during next iteration also?</p>