I am new to shell scripting so apologies if this was asked before.

I have a file coordinates.txt like this:


I would like to pass each value of each line of the file to a variable and use those values one by one as input in a command line and save that number in part of the file name. I wrote the following code but not sure if this is the correct way to go...

cat coordinates.txt | while read LINE; do
    var="$(echo $LINE)"
    /home/users/scripts/TreeView/TreeView.sh -o $NAME_ALL.chr1.new_estimate.trees.$var --bp_of_interest $var

so in the output -o each value one at the time is appended to the file name and each value is also used as --bp_of_interest, one at the time

Any suggestion highly appreciated. Thanks

  • 1
    What is $NAME_ALL? You need to double quote all variable expansions, and var="$(echo $LINE)" is better written var="$LINE". Also, remove that cat and redirect the file into the loop with while read ...; done <coordinates.txt. – Kusalananda Jul 23 '19 at 16:15
  • $NAME_ALL is another variable of the beginning of the file name which is already in my environment, it is different from the variables (var) from the file – Paolo Lorenzini Jul 23 '19 at 16:18
  • sure, thanks for the suggestion – Paolo Lorenzini Jul 23 '19 at 16:19
  • something like this?: while read LINE; do var="$LINE" /home/users/scripts/TreeView/TreeView.sh -o $NAME_ALL.chr1.new_estimate.trees."$var" --bp_of_interest "$var" done <coordinates.txt – Paolo Lorenzini Jul 23 '19 at 16:22
  • it is working very well, wonderfull – Paolo Lorenzini Jul 23 '19 at 16:30

Your loop is better written as


while IFS= read -r line; do
    TreeView.sh -o "$name_all.chr1.new_estimate.trees.$line" --bp_of_interest "$line"
done <coordinates.txt

This changes the following:

  • I added /home/users/scripts/TreeView to the front of $PATH. This allows us to call TreeView.sh without the absolute path prepended to it.
  • I deleted the var variable, as it was just the same as the value read from the input file.
  • I use IFS= read -r line because "Understanding "IFS= read -r line"".
  • I used lower-case variable names because "Are there naming conventions for variables in shell scripts?".
  • I pass the data from the file into the loop with a redirection, because using cat in this instance is just not needed.

Another way to do the same thing:


xargs -I {} TreeView.sh -o "$name_all.chr1.new_estimate.trees.{}" --bp_of_interest "{}" <coordinates.txt

This uses xargs to insert the data read from each line of coordinates.txt into the arguments of the TreeView.sh script in the locations denoted by {}.

You could even add -P n to the xargs command line (e.g. before -I {}, where n is some number) to run that many parallel instances of the script at once, each with the appropriate data inserted into its command line from coordinates.txt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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