Skip to content

shfmt: places ";then" on wrong line when using heredoc as stdin for a command #1047

Closed

Description

@dionysius

shfmt v3.6.0

Before:

#!/bin/bash

if ! lxc launch --vm "$image" "$name" <<EOF
${instance_config}
EOF
then
        lxc delete -f "$name"
fi

After:

#!/bin/bash

if ! lxc launch --vm "$image" "$name" <<EOF; then
${instance_config}
EOF
        lxc delete -f "$name"
fi

It breaks the script and modifies the heredoc content. It should not move the then and keep it on his own line without prepended ;.

Note: Putting the command into the if is recommended by SC2181. Obviously <<<"${instance_config}" can be used but the issue is still present when using heredoc.

Activity

  1. Ledorub commented on Apr 24, 2025

    @Ledorub

    I'm also facing this issue. The only workaround I found is to disable the SC2181 and check the exit code in a separate if-statement.

    cat << EOF > file
    ...
    EOF
    # shellcheck disable=SC2181
    if [[ "${?}" -ne 0 ]]; then
      ...
    fi
  2. jakub-bochenski commented on May 29, 2025

    @jakub-bochenski

    is it possible to prevent shfmt from reformatting the if then statements?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      ⌂ 换网址