Globbing a filename and then saving to a variable

I’ve got some files in a directory with a standard format, I’m looking to use a txt file with part of the filenames to extend them through * then finally add on a .gz tag as an output

For example, a file called 1.SNV.111-T.vcf in my directory, I have 111-T in my txt file.

while getopts f: flag
        case "${flag}" in
                f) file=${OPTARG};;

while IFS="" read -r p || [ -n "$p" ]

        bgzip -c ${vcf} > ${vcf}.gz

done < $file

This will successfully run bgzip but actually save the output to be:


So adding .gz at the end has "deactivated" the * character, as pointed out by Barmar this is because there isn’t a file in my directory called 1.SNV.111-T.vcf.gz so the wildcard is inactivated, please can anyone help?

I’m new to bash scripting but I assume there must be some way to save the "absolute" value of my vcf variable so that once it has found a match the first time, it’s now a string that can be used downstream? I really cant find anything online.

>Solution :

The problem is that wildcards are only expanded when they match an existing file. You can’t use a wildcard in the filename you’re trying to create.

You need to get the expanded filename into the vcf variable. You can do it this way:

vcf=$(echo *"$p.vcf")
bgzip -c "$vcf" > "$vcf.gz"

Leave a Reply