Skip to content

syntax: Incorrectly Formats Bash Script with Heredoc and Subshell #1131

@celermajer

Description

@celermajer

Bug Report: shfmt 3.10.0 (Linux ARM64) Incorrectly Formats Bash Script with Heredoc and Subshell

Summary

When using shfmt version 3.10.0 on Linux ARM64 to format a Bash script containing a heredoc (cat <<END) piped into a tee command with a subshell redirection (>(...)), the tool generates incorrect and broken code.
The resulting output does not preserve the original functionality and introduces syntax errors.

Environment

  • Tool: shfmt
  • Version: 3.10.0
  • Platform: Linux ARM64
  • Date: February 20, 2025
  • Steps to Reproduce
    Create a Bash script with the following content:
#!/usr/bin/bash

function payload() {
    cat <<END | tee >( { echo here the payload ; cat ; } >&2 )
Line : ${LINENO}
END
}
cat -n <(payload)

Run shfmt on the script:
shfmt -i 4 -w script.sh
Observe the output generated by shfmt.
Actual Output
The formatted code produced by shfmt is:

#!/usr/bin/bash

function payload() {
    cat <<END | tee >({
Line : ${LINENO}
END
        echo payload
        cat
    } >&2)
}

cat -n <(payload)

Issues:

The heredoc (cat <<END) and the tee command with subshell redirection (>(...)) are incorrectly split across multiple lines.
The subshell >({ echo payload ; cat ; } >&2) is improperly indented and placed outside the intended pipeline structure.
The resulting code is syntactically incorrect and fails to execute as intended.

Expected Output

The correctly formatted code should be:

#!/usr/bin/bash

function payload() {
    cat <<END |
Line : ${LINENO}
END
        tee >(
            {
                echo payload
                cat
            } >&2
        )
}

cat -n <(payload)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions