Mutable list or array structure in Bash? How can I easily append to it?
I'm trying to collect string values in a bash script. What's the simplest way that I can append string values to a list or array structure such that I can echo them out at the end?
Though the question is answered and is pretty old, I'd like to share a namespace-solution as it works significantly faster than any other ways except for ennukiller's answer (on my 100k lines tests it won ~12 secs against my ~14 secs, whereas list-append solution would take a few minutes).
You can use the following trick:
# WORKS FASTER THAN THESE LAME LISTS! ! !
size=0;while IFS= read -r line; do
echo $line
((++size))
eval "SWAMP_$size='$line'"
done