Skip to content

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

Closed

Description

@tandy-1000

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.

Activity

  1. 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[/+] on Apr 17, 2023
  2. mvdan commented on Apr 22, 2023

    @mvdan
    Owner

    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".

  3. netrixken commented on Jun 6, 2023

    @netrixken

    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

  4. mvdan commented on Jun 10, 2023

    @mvdan
    Owner

    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.

  5. added a commit that references this issue on May 28, 2024
    6f8f5d9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址