mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
7.2k stars 340 forks source link

syntax: printer breaks certain arithmetic expressions #987

Open musjj opened 1 year ago

musjj commented 1 year ago

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
mvdan commented 1 year ago

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

kiyoon commented 1 year ago

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
mikeBashStuff commented 4 months ago

((!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).

mahadzaryab1 commented 1 month ago

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?