Search code examples
bashbedtools

bedtools intersect, error reading files read from a directory


I am not sure what I am doing wrong here. It seems to work properly when I use command line.

My script is pretty simple

!/bin/bash
for file in '*.bed';do

        echo  $file
        bedtools intersect -wa -wb -a /data2/E34_uniq.bed -b $file > $file.tsv;
done

The error I get is for all the files read from the directory -

*****ERROR: Unrecognized parameter: GSM2310918_methylcall.Sample_7273.mincov10.bed *****


*****ERROR: Unrecognized parameter: GSM2310919_methylcall.Sample_7301.mincov10.bed *****


*****ERROR: Unrecognized parameter: GSM2310920_methylcall.Sample_7308.mincov10.bed *****

Solution

  • It should have been as

    #!/bin/bash
    
    for file in *.bed; do
        echo "$file"
        bedtools intersect -wa -wb -a /data2/E34_uniq.bed -b "$file" > "${file}.tsv";
    done
    

    Notice a few things here,

    1. Removal of the single quotes in the glob expansion
    2. Adding double-quotes to variable to avoid word-splitting.
    3. Setting proper interpreter path for the script to run #!/bin/bash