Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

I read this in the bash man page:

If the word is double-quoted, ${name[*]} expands to a single word with the value of each array member separated by the first character of the IFS special variable.

so you could replace your loop by :

arrSNPsString="$(IFS="|"; echo "${arrSNPsToBeExtracted[*]}" )"
echo "$arrSNPsString"

I tried this myself directly at the bash prompt :

a[1]=aaaa
a[2]="bbbb cccc"
a[3]=dddd
b="$(IFS="|"; echo "${a[*]}")"
echo "$b"

which prints:

aaaa|bbbb cccc|dddd

Don't mess with IFS, always restore its old value after you have changed it. The $(..) syntax in this code does that for you.

share|improve this answer
    
Even better, just use IFS for only that command like this: IFS='|' arrSNPsString="${arrSNPsToBeExtracted[*]}" on one line. No need to restore it –  minerz029 Dec 1 '13 at 5:14
    
@minerz029 I tried that syntax but it didn't worked. I tried to escape the " and $, I tried to use eval, I tried a mix ... no success. I think that's because second argument arrSNPsString=... is also an assignement,, not a command that will use the IFS value. –  Emmanuel Dec 2 '13 at 11:02
    
What about arrSNPsString="$(IFS='|' echo "${arrSNPsToBeExtracted[*]}")"? –  minerz029 Dec 2 '13 at 20:31
    
@minerz029 good idea to use $(...) solution is : arrSNPsString="$(IFS='|' eval echo \"\${arrSNPsToBeExtracted[*]}\")" or arrSNPsString="$(IFS='|' eval echo '"${arrSNPsToBeExtracted[*]}"')" –  Emmanuel Dec 3 '13 at 10:48
1  
@minerz029 arrSNPsString="$(IFS='|' ; echo "${arrSNPsToBeExtracted[*]}")" thank you for pointing to separate the two commands. –  Emmanuel Dec 18 '13 at 13:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.