Skip to content

syntax: printer breaks certain arithmetic expressions #987

Closed

Description

@musjj

By default, shfmt removes brackets within parentheses, but this breaks certain kinds of expressions.

For example:

(( ! 0 )) && echo true || echo false # true

shfmt will format this into:

((!0)) && echo true || echo false # bash: !0: event not found

Activity

  1. changed the title [-]Formatting breaks certain arithmetic expressions[/-] [+]syntax: printer breaks certain arithmetic expressions[/+] on Mar 25, 2023
  2. mvdan commented on Mar 25, 2023

    @mvdan
    Owner

    Thanks for reporting - this does indeed look like a bug. Happy to review a PR, as this one shouldn't be hard to fix.

  3. kiyoon commented on Apr 15, 2023

    @kiyoon

    I realised in zsh, the expression like this would be treated as + operation and will make spaces in between, breaking the script.

    if (($+commands[exa])); then
  4. mikeBashStuff commented on May 13, 2024

    @mikeBashStuff

    ((!0)) && echo true || echo false # bash: !0: event not found

    I believe this is only true for interactive shells since this syntax triggers history expansion. It's perfectly fine to use it from within a script by the very default, unless you explicitly enable histexpand|history there as well (which I guess is a valid use-case, though I personally prefer to trace the entire thing rather then save a history of it).

  5. mahadzaryab1 commented on Aug 16, 2024

    @mahadzaryab1

    hi @mvdan ! i'm new to this project and to contributing to open-source in general. i would like to pick up this issue but am unsure of what needs to be done. would you be able to help me out with how to get started?

  6. mdekstrand commented on Jul 31, 2025

    @mdekstrand

    @mikeBashStuff It may only be a problem when history expansion is enabled, however, one of the major use cases for zsh support discussed in #120 — and likely also bash support — is to support formatting shell plugins, which will often run in an interactive shell.

  7. added a commit that references this issue on Jul 28, 2026
    58a8c02
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

      ⌂ 换网址