Compilation error when using ${file.join(' ')} in for loop

Hi,
I am trying to loop through a list of files, but am getting a compilation error. Does anyone see where I am going wrong?

process cgpVAF_merge {
  tag "${meta.donor_id}"
  label "normal"
  publishDir "${params.outdir}/${meta.donor_id}/", mode: 'copy'

  input:
  tuple val(meta),
        path(vafs, stageAs: "vaf??.tsv")

  output:
  tuple val(meta),
        path("${meta.donor_id}_${meta.vcf_type}_vaf.tsv")

  script:
  """
  # get variant info cols: Chrom Pos Ref Alt normal_MTR normal_DEP
  cut -f 3,4,5,6,24,26 ${vafs[0]} \\
  > tmp.1.cut


  # for all files, get *_MTR and *_DEP columns
  for file in ${vafs.join(' ')} ; do
    cols=\$(head -1 \$file | tr '\\t' '\\n' \\
      | grep -n '_\(MTR\|DEP\)$' \\
      | grep -v '^.*normal_\\(DEP\\|MTR\\)$' \\
      | cut -d: -f1 | tr '\\n' ',' | sed 's/,\$//')
    cut -f\$cols \$file > tmp.\$file.cut
  done

  # combine all extracted columns
  paste tmp.*.cut > ${meta.donor_id}_${meta.vcf_type}_vaf.tsv
  """
}

Here is the error when I try to run it:

$ nextflow run ../low_input_trees/   --reflag false   --sample_sheet out/metadata/sample_sheet_BRCA.csv   --sequencing_type WGS   -profile sanger_hg38   --outdir out/BRCA/   -resume   -with-tower   -N at31@sanger.ac.uk

 N E X T F L O W   ~  version 24.10.0

Launching `../low_input_trees/main.nf` [spontaneous_cori] DSL2 - revision: 9c4d6c611a

ERROR ~ Module compilation error
- file : /lustre/scratch126/casm/team154pc/at31/low_input_trees/./modules/cgpVAF.nf
- cause: token recognition error at: ' ; do\n    cols=\$(head -1 \$file | tr '\\t' '\\n' \\\n      | grep -n '_\(' @ line 107, column 32.
     for file in ${vafs.join(' ')} ; do
                                  ^

1 error


 -- Check script '/lustre/scratch126/casm/team154pc/at31/low_input_trees/main.nf' at line: 14 or see '.nextflow.log' file for more details

I can see some lone $ in the script that do not refer to any groovy variable. They probably should be literal $ in the output, so they should be escaped. I’m referring to the $ used in the greps to mark the end of the line in the pattern.