Home > Enterprise >  readarray only taking first line from input file
readarray only taking first line from input file

Time:10-13

I have a pipeline to analyze some data and I wanted to add a short script that takes failed samples from a previous step and do something with them but I'm having an issue reading the input file in as an array. My input file and script are set up like this:

Input file (file name is prev_step.failed):

Samp_12405736
Samp_12405737
Samp_12405738

Bash script:

#!/bin/bash
readarray -t samples </local/path/to/data/prev_step.failed
for samp in $samples
do
    echo $samp
    # do something with $samp here
done

The issue I am having is that what I loop through $samples and print each sample only the first one (Samp_12405736) is printed.

CodePudding user response:

Accessing an array as a simple scalar returns the first element.

$: samples=( a b c )

$: echo $samples
a
$: printf "%s\n" $samples
a

$: echo "${samples[@]}"
a b c 
$: printf "%s\n" "${samples[@]}"
a
b
c
  •  Tags:  
  • bash
  • Related