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
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, aIFS=, read -ra ARRAY_EXP <<<"$EXP_DESIGN"is both simpler and more efficient (though all-uppercase variable names should be avoided).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 tolog.txt; e.g.:echo "${ARRAY_EXP[*]}" >> log.txt. Note that using just$ARRAY_EXPonly returns the 1st element.