emacs flycheck (bash with shellcheck) throws error when using optional characters

108 views Asked by At

I was trying to improve a function definition using flycheck (shellcheck). The commented line on the code below is what I tried to improve. The code works but I get the error message syntax error near unexpected token ('`

my_loop() {
# for ifastq in $(ls $1 | grep -v txt)
for ifastq in $1/*.f?(ast)q?(.gz)
do
echo "$ifastq"
done
}

Is the code above more fragile than the one using the commented line? Should I also include shopt -s extglob in the function definition or at the top of the script?

Thanks

BTW: the code is supposed to capture files with .fastq .fq fq.gz or .fastq.gz extensions

1

There are 1 answers

2
Barmar On

Is the code above more fragile than the one using the commented line?

No, the opposite. It's generally a bad idea to process the output of ls. If any of the filenames contains whitespace, it will be split into multiple words when you use $(ls ...).

You do need to enable extglob, since it's not enabled by default.

Extended globbing is a bash extension, Flycheck might not recognize it. Emacs SE would be a better place to ask questions specific to this Emacs extension.