I want to start off by distinguishing that Repeating command with different filenames is not what I am asking.

My question is, how do you build a single command with a directory of filenames in it, each prepended with the --thingy part of the command? In my specific application, I'm using GATK (though this question is about building the command line call), and even more specifically GenotypeGVCFs. My problem is that the command should look like:

java -jar GenomeAnalysisTK.jar \
-T GenotypeGVCFs \
-R reference.fasta \
--variant sample1.g.vcf \
--variant sample2.g.vcf \
-o output.vcf

but I have 93 variant files, and so the --variant line needs to be repeated 93 times, each with a different filepath. They're all in the same directory together, and they all have the same extension (*.g.vcf).

I've looked at doing something with find -exec + but couldn't think of a way to get the --variant attached to each result. xargs also seems promising, but I don't understand what it does super well, and so was having trouble setting it up. At this point, I might just have a Python script generate the command and then just paste it into the terminal, but I wanted to know if there's a "correct" way to do this for future.

  • What happens if you put a wildcard in front of g.vcf?
    – ryekayo
    Feb 18, 2016 at 18:34
  • That'll give a list with exactly 1 --variant where I need the --variant bit to repeat for each thing in the glob. Feb 18, 2016 at 18:53

2 Answers 2


In theory, this might help, but I have never tried such a thing. If I were you I would just test it with some sort of echo command first, before launching the java program.

java -jar GenomeAnalysisTK.jar \
-T GenotypeGVCFs \
-R reference.fasta \
$(for v in *.g.vcf;do echo --variant ${v}" \\";done) -o output.vcf
  • 2
    $(printf ' --variant "%s" \\\n' *.g.vcf)
    – Costas
    Feb 18, 2016 at 18:42
  • Testing it with echo, it at least generates the correct string. Be back soon to let you know if it runs correctly too! Feb 18, 2016 at 18:51
  • This works with the caveat that " \\" should just be " ". The slashes are to make the multiline command work, but it appears that it interprets the loop as 1 line and does so literally (so the '\' ends up in the call). Adding in a newline as in Costas would maybe solve this? Otherwise excellent! Also, @Costas, for some reason, printf kept stripping out the escaped characters, so I just got a big jumble. Honestly, I didn't put much effort into figuring out why though, since Mel's solution worked. Feb 18, 2016 at 19:11

The trick is to get the list you want, and manipulate each element. Try changing to the directory with your variant files, and something like

FILES=$(ls | sed 's/^/--variant /')
echo ${FILES}

Then just substitute ${FILES} into your command.


You must log in to answer this question.

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