I am using a bash script to run a program on a list of genes, all of which are named in a file called IDlist.txt. After reading IDlist.txt into an array, I am trying to loop through the array and execute the command once for every gene. This requires concatenating the path with the gene name (IDlist[i]) and the file extension (.fas).

Here's what my code looks like:


## This declares IDlist and reads each line in the file into the array
declare -a IDlist

while read line
done < "IDlist.txt"

##Then I loop through IDlist

for i in ${IDlist[@]}
    echo $i
    echo $path1

##This is the command
(echo 1; echo ${path1}; echo ${path1}; echo ${path2}; echo 2; echo 2) | /path/to/program/hyphy/HYPHYMP /path/to/program/hyphy/build/lib/hyphy/TemplateBatchFiles/SelectionAnalyses/RELAX.bf

The output from "echo $i" is correct, but the output from "echo $path1" is not. For $i = CRIPT $path1 is .fas1/to/geneTrees/63mammnt/trimallcds_wSpalax/newFasta/CRIPT

.fas is appending to the beginning of the string (even though I'm adding it to the end), and it's writing over the first folder in the path. This does not happen when I run these commands without a script.

  • 1
    Your IDlist.txt is a DOS/Windows file terminated by CR/LF. Convert it to unix with dos2unix or trim the CR with IDlist[n]=${line%$'\r'}.
    – user313992
    Commented Jul 3, 2019 at 19:33
  • 1
    Your script has many other problems, the most obvious being that you keep appending numbers to the n variable with n=${n}+1, instead of adding them to it; while they will be evaluated as arithmetic expressions in IDlist[n]=..., it's still better to just write IDList[n++]=... and get rid of the n=${n}+1.
    – user313992
    Commented Jul 3, 2019 at 19:36

1 Answer 1


Some hints:

  • Use dos2unix to convert files to Unix newlines. What looks like overwriting is actually the carriage return character, which when displaying the value has the special meaning of moving the cursor to the start of the line.
  • By far the simplest way to read a file into an array is readarray.
  • Use More Quotes™
  • Try running the result through shellcheck for more hints, and once the script passes you could post it for review.
  • You probably want to group the echo commands using { and } to avoid creating a subshell.
  • For any WSL2 wonderers with a windows file open in notepad++, don't forget the classic "Edit -> EOL" menu ;)
    – noelicus
    Commented Jun 4, 2021 at 13:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .