Does a variable lose its value outside the loop in shell script?

hi,

when we assign a variable inside a for loop or while loop in a shell script, does it loses its value after comming out of the loop. i am facing this issue.

can anyone help me??

loops create subshells when you pipe the output to while or for loops so you will loose the values..
use process substitution..

Hello,

The variable should have the last value which was assigned to it in loop.
Here is an example for it.

 
$ cat loop_example.ksh
str='monday, tuesday, wednesday, thursday, friday, saturday, sunday'
str1=`echo $str | sed 's/\,//g'`
set -A array_str ${str}

for i in ${array_str[@]}
do
a=$i

done

echo $a
 
 

$ ksh loop_example.ksh
sunday
$

 

We have a script named loop_example.ksh in which variable a have many values but if you will print it's value outside from loop it will show the last one as Sunday in the above example.

Thanks,
R. Singh

ya obviously, it will have the latest value since u r overwriting the previous value of $a inside the for loop.

my question is

cat file_names
sample.txt

shell_script_B.sh

echo "In shell_script_B.sh"
FILENAME="original.txt"
echo "FILENAME = {$FILENAME}"
echo ""
echo "-------Inside the loop-------"
cat file_names | while read line
do
        FILENAME=$line       # reassigning the FILENAME
        echo "FILENAME = {$FILENAME}"
done
echo "-------End loop--------------"
echo ""
echo "FILENAME = {$FILENAME}"

Output

In shell_script_B.sh
FILENAME = {original.txt}

-------Inside the loop-------
FILENAME = {sample.txt}
-------End loop--------------

FILENAME = {original.txt}

check the output. Inside the Loop it prints the new value and outside the value it prints original file. what is the solution for this??

Pleas read my reply to your thread.. since you are piping the out put to while it will open a subshell and variable value would be lost once its back to main shell

use
while read line ; do
blah blah
blah
done < filename

1 Like

Hello,

Could you please the following if this helps.

 
 
$ cat script_user_error.ksh
echo "In shell_script_B.sh"
FILENAME="original.txt"
echo "FILENAME = {$FILENAME}"
echo ""
echo "-------Inside the loop-------"
while read line
do
        FILENAME=$line       # reassigning the FILENAME
        echo "FILENAME = {$FILENAME}"
done < "file_names"

echo "-------End loop--------------"
echo ""
echo "FILENAME = {$FILENAME}"
 
 

$ ksh script_user_error.ksh
In shell_script_B.sh
FILENAME = {original.txt}
-------Inside the loop-------
FILENAME = {sample.txt}
-------End loop--------------
FILENAME = {sample.txt}
$

 
 

I have just made the changes in color. Is it meets your requirement.
Kindly let me know if I can help on same.

Thanks,
R. Singh

1 Like

i read your thread before also and i got the problem what you said. i just wanted a solution for that. so this code will do the same task what i want. thanks.

Its the way shell behaves if you dont wanna change anything and still want it to work then sorry its not the way shell designed :slight_smile:

either change the way you are using while loop or make export $FILENAME

i have changed the way you told and its working fine..

1 Like