1

I'm working on a pipeline for RNA-Seq and I'm having trouble with my code. One of my input parameters is the experimental design:EXP_DESIGN=3,3,3. I want to split this string into an array, which for I am using ARRAY_EXP=$( echo $EXP_DESIGN | tr ',' '\n' ). This command line works fine for me. However, if I try to count the elements of this array with NUM_COND=${#ARRAY_EXP[@]}, it does not work. In my log.txt file, I obtain this information:

exp_design 3,3,3
array_exp 3 3 3
num_cond 1

I would appreciate any help

EDIT: I have also tried

IFS=',' read -ra ARRAY_EXP <<< "$EXP_DESIGN"

And I get a different error:

exp_design 3,3,3
array_exp 3
num_cond 3
  • Note: It's tempting to suggest `ARRAY_EXP=( $( echo $EXP_DESIGN | tr ',' '\n' ) )` as a fix - the outer `( ... )` are create an array, but it is generally not advisable to create arrays this way, because output from the - of necessity - unquoted command substitution (`$( ... )`) will be subject to any-whitespace word splitting and also globbing, unless you take additional measures. In this case, a `IFS=, read -ra ARRAY_EXP <<<"$EXP_DESIGN"` is both simpler and more efficient (though all-uppercase variable names should be avoided). – mklement0 Oct 31 '16 at 14:46
  • Now that you have an actual _array_ stored in variable `ARRAY_EXP` (unlike before, where it was a simple string), you must reference the array _as a whole_ if you want to output all of its elements to `log.txt`; e.g.: `echo "${ARRAY_EXP[*]}" >> log.txt`. Note that using just `$ARRAY_EXP` only returns the _1st element_. – mklement0 Oct 31 '16 at 14:50

0 Answers0