I have a piece of bash code, in which I would like to make a long string from an array of strings.
The bash code is as follows:
# Load all the SNPs
arrSNPsToBeExtracted=( $( cat $indexFileName ) )
arrSNPsString="\""
lenArrSNPsToBeExtracted=${#arrSNPsToBeExtracted[*]}
si=0
while [ $si -lt $lenArrSNPsToBeExtracted ]
do
arrSNPsString=${arrSNPsString}"|"${arrSNPsToBeExtracted[$si]}
#echo $si": "${arrSNPsString}
let si++
done
arrSNPsString=$arrSNPsString"\""
echo $arrSNPsString
=====================
As you can see, I have a list of SNPs (words):
word1
word2
word3
And I want to make it like this:
"word1|word2|word3"
This string is assigned to arrSNPsString
, but I cannot make it happen.
What am I doing wrong?
As you can see in the code, the arrSNPsString
should get longer, but instead, it takes at the end of the while loop only the last item.
I know it has something to do with local and global environments/variables. I want to extend a global string arrSNPsString
in a local loop.
Does someone know How I can solve this problem