Bash troubleshooting: Not a valid identifier

user964689 picture user964689 · Jun 19, 2012 · Viewed 12.6k times · Source

Beginner here trying to get a pipeline working in bash. If somebody can see why when I run the following I get:

-bash: `$i': not a valid identifier,

that would be really helpful. Also if there are other mistakes please let me know

for $i in /home/regionstextfile; do tabix /sequences/human_variation/snps/genotypes.vcf.gz $i | vcftools --window-pi 10000 >> /home/Testgenomesdata/genomesregions.txt; done

The idea is for each line in regionstextfile (which contains genome coordinates) run a program called tabix in the vcf.bz file, then with the output run vcftools with the specified options, then put all the outputs into the genomesregions.txt file.

Answer

Igor Chubin picture Igor Chubin · Jun 19, 2012

That must be so:

for i in `</home/regionstextfile`
do 
  tabix /sequences/human_variation/snps/genotypes.vcf.gz $i | vcftools --window-pi 10000 >> /home/Testgenomesdata/genomesregions.txt
done

When you use a variable (e.g. assign value to it or export it, or do anything but with the variable itself) you write its name without $; when you use a value of a variable you write $.

EDIT:

When region names contains spaces but each region is in a separate line, you need while:

cat /home/regionstextfile | while read i
do 
  tabix /sequences/human_variation/snps/genotypes.vcf.gz "$i" | vcftools --window-pi 10000 >> /home/Testgenomesdata/genomesregions.txt
done