I have a comma delimited string variable that I need to remove values that are coming from standard input, which is coming in from a file. When I finish the script the values are not removed from the variable but you can tell that they were removed in the do loop. How do I update a var that is global inside the do loop of the operation?
#!/bin/bash
varFoo="text1,text2,obit,poodle,text2,text1,doberman,bloodhound,treewalker,breaker,circuit,bum,rush"
echo text2 > /tmp/fileFeed.txt
echo circuit >> /tmp/fileFeed.txt
echo rush >> /tmp/fileFeed.txt
cat /tmp/fileFeed.txt | while read word; do
removeWord=${varFoo//$word,/}
removeWord=${removeWord//,$word/}
echo ========== transaction seperator ==========
echo word=$word
echo removeWord=$removeWord
varFoo=$removeWord
done
echo ^^^^^^^^^^^exiting work loop and heading to the end ^^^^^^^^^^^
echo FINAL varFoo = $varFoo
exit $?
The output is
========== transaction seperator ==========
word=text2
removeWord=text1,obit,poodle,text1,doberman,bloodhound,treewalker,breaker,circuit,bum,rush
========== transaction seperator ==========
word=circuit
removeWord=text1,obit,poodle,text1,doberman,bloodhound,treewalker,breaker,bum,rush
========== transaction seperator ==========
word=rush
removeWord=text1,obit,poodle,text1,doberman,bloodhound,treewalker,breaker,bum
^^^^^^^^^^^exiting work loop and heading to the end ^^^^^^^^^^^
FINAL varFoo = text1,text2,obit,poodle,text2,text1,doberman,bloodhound,treewalker,breaker,circuit,bum,rush
So you'll notice that the loop removes the value from the string but when it exits the loop the variable is still at the original value before entering the loop.