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

syntax: printer breaks certain arithmetic expressions #987

Open
musjj opened this issue Mar 25, 2023 · 3 comments
Open

syntax: printer breaks certain arithmetic expressions #987

musjj opened this issue Mar 25, 2023 · 3 comments

Comments

@musjj
Copy link

musjj commented Mar 25, 2023

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 mvdan changed the title Formatting breaks certain arithmetic expressions syntax: printer breaks certain arithmetic expressions Mar 25, 2023
@mvdan
Copy link
Owner

mvdan commented Mar 25, 2023

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
Copy link

kiyoon commented Apr 15, 2023

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
Copy link

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

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

No branches or pull requests

4 participants