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

Bash syntax breaks when posix sh shebang is present #1092

Open
Amund211 opened this issue Sep 18, 2024 · 2 comments
Open

Bash syntax breaks when posix sh shebang is present #1092

Amund211 opened this issue Sep 18, 2024 · 2 comments

Comments

@Amund211
Copy link

If you have an incorrect sh shebang while using bash's test syntax shfmt breaks the script. Formatting works correctly with a bash shebang, and with no shebang at all.

$ cat fmt.sh 
#!/bin/sh
if [[ "1" > "2" ]]; then :; fi
$ shfmt fmt.sh 
#!/bin/sh
if [[ "1" ]] >"2"; then :; fi
@mvdan
Copy link
Owner

mvdan commented Sep 18, 2024

What exactly do you want to see happen here? The original happens to be valid posix shell, which is unfortunate, but the parser and formatter are technically correct in continuing if they can.

If what you mean is "the parser should reject [[ in POSIX mode, telling the user it's bash syntax", we could possibly do that and it would probably lead to better UX for the vast majority of users, but I wonder if it would technically break with the POSIX spec. It just says that some implementations may treat [[ as a reserved keyword with unspecified results.

@Amund211
Copy link
Author

Ah, I was not aware that the original code was valid posix shell :^(.
Rejecting (or maybe warning about) [[ in posix mode is likely to save a few souls, but not sure what that does for posix compliance.

I imagine this case could come up quite a lot, as #!/bin/sh is a common incantation, and most people don't know the difference between posix sh and bash...

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

2 participants