This is a test version of Biostars. For the public version, visit https://www.biostars.org.
bash script not a valid identifier

I am trying to run bash script, but it gives this error ( `$fastq': not a valid identifier).

kraken2 bash

Do you get valid names if you just run this: ls *_R1.fastq.gz | sed 's/_R1.fastq.gz//'

yes, when I am running ls *_R1.fastq.gz | sed 's/_R1.fastq.gz//' in my fastq folder it gives me names

Hi ! try running your script with "bash -x yr_script.sh", this will expand variables and print commands line by line.

Also, add a '\' :

 kraken2 --db $database \
         --threads 8 --memory-mapping \
         --use-names --confidence 0.1 \
         --report taxonomy_reads/${fastq}_kraken2.tax \
         --paired ${fastq}_R1.fastq.gz ${fastq}_R2.fastq.gz \
         --output taxonomy_reads/${fastq}_kraken2.txt

Okay, I tried, but it also gives the same line error `$fastq': not a valid identifier

2 answers

It should be for fastq in, not for $fastq in. Variables don't get the $ when they're being declared/initialized/assigned to.

I knew that the other way around. But in that case it gives this '*_R1.fastq.gz': 'No such file or directory' , but I tried this on my fastq files and it worked.

Do not delete posts when they've been addressed. If one or more solutions worked, accept them using the green check mark.

upvote_bookmark_accept

#!/bin/bash 
database="kraken2_database"
fastq="fastq_dir" # consider change this variable name, you repeat it in the loop variable (sorry if this is what you wanted to do.)
for fastq in $(ls *_R1.fastq.gz | sed 's/_R1.fastq.gz//') # <---- the problem was the "$fastq"
do
    # also, the '\' are needed
     kraken2 --db $database \
             --threads 8 --memory-mapping \
             --use-names --confidence 0.1 \
             --report taxonomy_reads/${fastq}_kraken2.tax \
             --paired ${fastq}_R1.fastq.gz ${fastq}_R2.fastq.gz \
             --output taxonomy_reads/${fastq}_kraken2.txt
done

Log in to answer this question.