# DRY Principle in Nextflow: Reusing Output Path Definitions in \`output:\` and \`script:\` sections

**URL:** <https://community.seqera.io/t/dry-principle-in-nextflow-reusing-output-path-definitions-in-output-and-script-sections/2203>\
**Category:** Ask for help\
**Tags:** nextflow\
**Created:** [June 17, 2025, 9:25pm UTC](https://community.seqera.io/t/dry-principle-in-nextflow-reusing-output-path-definitions-in-output-and-script-sections/2203 "2025-06-17T21:25:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ppreshant](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/ppreshant/32/1862_2.png) [@ppreshant](https://community.seqera.io/u/ppreshant)\
**Post date:** [June 17, 2025, 9:25pm UTC](https://community.seqera.io/t/dry-principle-in-nextflow-reusing-output-path-definitions-in-output-and-script-sections/2203/1 "2025-06-17T21:25:26Z")

</div>

# Reusing Output Paths in Nextflow

I’m new to Nextflow and doing the Hello-Nextflow training just now. I had this question nagging me for the last couple days and thought I’ll use this to say hi to this helpful community 😄

I’m coming from Snakemake, where we can define output paths as variables and reuse them as `$output.name`. I’m wondering if there’s a similar pattern in Nextflow to avoid repeating the output path definition.

For example, in this process:

```groovy
process toUpperCase {
    publishDir 'results', mode: 'copy'

    input:
        path in_file
    output:
        path "${in_file}-upper.txt"
    script:
    """
    echo ${in_file} | tr '[:lower:]' '[:upper:]' > '${in_file}-upper.txt'
    """
}

```

I notice that I’m repeating the pattern `${in_file}-upper.txt` in both the `output` block and the `script` block. In Snakemake, I could define this as a variable and reuse it. Is there a similar pattern in Nextflow to:

1. Define the output path pattern once
2. Reuse it in both the output and script blocks
3. Potentially make it easier to maintain if the pattern needs to change

I’m looking for best practices to handle this pattern in Nextflow. Any suggestions or examples would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![robsyme](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/robsyme/32/342_2.png) [@robsyme](https://community.seqera.io/u/robsyme)\
**Post date:** [June 17, 2025, 9:34pm UTC](https://community.seqera.io/t/dry-principle-in-nextflow-reusing-output-path-definitions-in-output-and-script-sections/2203/2 "2025-06-17T21:34:59Z")

</div>

Hi Prashat

You can absolutely define new variables, for example:

```groovy
process toUpperCase {
    publishDir 'results', mode: 'copy'
    input: path in_file
    output: path outputWithSuffix
    script:
    outputWithSuffix = "${in_file}-upper.txt"
    """
    echo ${in_file} | tr '[:lower:]' '[:upper:]' > '${outputWithSuffix}'
    """
}

```

Given the modest size of most processes, it’s debatable whether this change makes an impact on maintainability. If you prefer this approach, it’s certainly available for you.

You can also use glob patterns in the output to give some flexibility without defining new variables:

```groovy
process toUpperCase {
    publishDir 'results', mode: 'copy'
    input: path in_file
    output: path "*.txt"
    script:
    """
    echo ${in_file} | tr '[:lower:]' '[:upper:]' > '${in_file}-upper.txt'
    """
}

```

---

<div class="post-metadata">

**Author:** ![ppreshant](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/ppreshant/32/1862_2.png) [@ppreshant](https://community.seqera.io/u/ppreshant)\
**Post date:** [June 18, 2025, 1:41am UTC](https://community.seqera.io/t/dry-principle-in-nextflow-reusing-output-path-definitions-in-output-and-script-sections/2203/3 "2025-06-18T01:41:08Z")

</div>

Wonderful. That makes a lot of sense now. Thank you for these two approaches!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/system/32/2402_2.png) [@system](https://community.seqera.io/u/system)\
**Post date:** [June 25, 2025, 1:41am UTC](https://community.seqera.io/t/dry-principle-in-nextflow-reusing-output-path-definitions-in-output-and-script-sections/2203/4 "2025-06-25T01:41:36Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
