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

shfmt bug: &> is broken onto a new line, while >& isn't #991

Closed
tandy-1000 opened this issue Apr 17, 2023 · 3 comments
Closed

shfmt bug: &> is broken onto a new line, while >& isn't #991

tandy-1000 opened this issue Apr 17, 2023 · 3 comments

Comments

@tandy-1000
Copy link

tandy-1000 commented Apr 17, 2023

When shfmt sees a &>, it is broken onto a new line:

{ echo "foo" } &>/dev/null || {
    echo "bar"
}

Becomes:

{ echo "foo" } &
	>/dev/null || {
    echo "bar"
}

The transformation also breaks the code.

If you switch &> with >& this does not happen.

@tandy-1000 tandy-1000 changed the title shellfmt bug: &> is broken onto a new line, while >& isn't shfmt bug: &> is broken onto a new line, while >& isn't Apr 17, 2023
@mvdan
Copy link
Owner

mvdan commented Apr 22, 2023

What did you run exactly?. It's likely that you're trying to use POSIX mode when &> is bash syntax. Unfortunately, &> is also valid POSIX syntax; it just parses as & >, i.e. "background command then start a redirection".

@netrixken
Copy link

Just noticed today

if ping -c 1 $default_gateway_ip &>/dev/null; then

becomes

	if
		ping -c 1 $default_gateway_ip &
		>/dev/null
	then

Using https://marketplace.visualstudio.com/items?itemName=mkhl.shfmt

@mvdan mvdan closed this as completed in 6f8f5d9 Jun 10, 2023
@mvdan
Copy link
Owner

mvdan commented Jun 10, 2023

The "fix" above is to give a helpful error, since I suspect that both of you were using POSIX mode to try to parse Bash syntax. You should be using the Bash mode, which should kick in automatically if your shell script has a .bash extension or a shebang like #!/usr/bin/bash. More details in the commit above.

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

3 participants