iterate each input of a CSV format input in shell script

Viewed 16

I'm trying to iterate each input of VAR2 and store it in VAR1. Everything's working fine but I can't get the latest value of VAR1 outside the while loop.

VAR1=""
VAR2=input1,input2
NEWVAR2=$( echo "${VAR2}" | sed "s/","/\n/g" )
echo "${NEWVAR2}" | while IFS= read -r SINGLELINE
do
    VAR1=$( echo ${VAR1}$'\n'${SINGLELINE} )
    echo VAR1 = $VAR1
    done
echo VAR1 = $VAR1
done

I did some research and got to know that the issue is happening because I'm calling the while loop as a subshell, so I took the alternate method, but here the 'IFS' is not getting counted

VAR1=""
VAR2=input1,input2
NEWVAR2=$( echo "${VAR2}" | sed "s/","/\n/g" )
while IFS= read -r SINGLELINE
do
    VAR1=$( echo ${VAR1}$'\n'${SINGLELINE} )
    echo VAR1 = $VAR1
    done<<< $( echo "${NEWVAR2}" )
echo VAR1 = $VAR1
done
0 Answers
Related