Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
celermajer opened this issue Feb 20, 2025 · 0 comments

Comments

@celermajer
Copy link

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant