Parallel variable extension output to argument inside quotes

67 Views Asked by At

I`m trying to expand a variable inside parallel for a program argument

For example TAG=IMPACT

ls *vcf | parallel -k -v 'bcftools query -f "%$TAG/n"'

Where it should be expanded to

ls *vcf | parallel -k -v 'bcftools query -f "%IMPACT/n"'

I tried many variantions of "" and '' as I could think of, but I always get

bcftools query -f %IMPACT/n

bcftools query -f '"%$TAG/n"'

Or anything else like

bcftools query -f '"%"$TAG"\n"'

Tried some variantions of parallel like --shellquote but no combination worked out and expanded correctly (if it is possible at all)

I tried by delimiting everything inside $TAG like:

TAG="%IMPACT/n"

then

ls *vcf | parallel -k -v 'bcftools query -f '"%$TAG/n"''

Which could work, but is there a way to expand it inside the code and not outside of it?

Just as a test that I was doing, Using

 ls *vcf | parallel -k -v 'bcftools +split-vep -f '"$TAG"' {} |  tr '"$COMMA"' '"$SPACE"''

When defining

TAG='"%IMPACT/n"'
SPACE='"/n"'
COMMA='","'

Expands to:

bcftools +split-vep -f "%IMPACT/n" input.vcf |  tr "," "/n"
1

There are 1 best solutions below

0
markp-fuso On

Since parallel is being started in a subprocess make sure to export TAG beforehand to insure parallel can properly expand $TAG at run time:

$ export TAG

$ ls file?.vcf | parallel -k -v 'echo "%$TAG%"'
echo "%$TAG%" file1.vcf 
%IMPACT% file1.vcf                               # $TAG expanded at parallel call
echo "%$TAG%" file2.vcf
%IMPACT% file2.vcf
echo "%$TAG%" file3.vcf
%IMPACT% file3.vcf

Alternatively, allow $TAG to be expanded by the OS by wrapping the parallel script in double quotes:

$ ls file?.vcf | parallel -k -v "echo '%$TAG%'"
echo '%IMPACT%' file1.vcf                        # $TAG expanded at command line
%IMPACT% file1.vcf
echo '%IMPACT%' file2.vcf
%IMPACT% file2.vcf
echo '%IMPACT%' file3.vcf
%IMPACT% file3.vcf

If you need to maintain the inner double quotes then escape them:

$ ls file?.vcf | parallel -k -v "echo \"%$TAG%\""
echo "%IMPACT%" file1.vcf                        # $TAG expanded at command line
%IMPACT% file1.vcf
echo "%IMPACT%" file2.vcf
%IMPACT% file2.vcf
echo "%IMPACT%" file3.vcf
%IMPACT% file3.vcf