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.35k stars 346 forks source link

Arithmetic expansion: "= must follow a name" when variable expansion is used #1086

Closed laurenthuberdeau closed 4 months ago

laurenthuberdeau commented 4 months ago

The following valid POSIX shell script fails to parse with shfmt:

var1="foo"

echo $(($var1 + 12)) # Parsed by shfmt
echo $(($var1 = 12)) # Fails with "= must follow a name"

It seems to not like the variable expansion nested in an arithmetic expansion.

mvdan commented 4 months ago

See my response to https://github.com/mvdan/sh/issues/858. Bash supports more trickery than we do, and at least for now, that's an intentional limitation.

laurenthuberdeau commented 4 months ago

Hi!

Thanks for looking at the issue. The example in my original message is compatible with all POSIX-compliant shells, and doesn't use any Bash specific construct so I'm not sure how the issue you link to is related. Perhaps this example better demonstrates the error:

# Assign the value in the second argument ($2) in the variable named by the first argument ($1)
assign_to() {
  : $(($1 = $2)) # Fails with "= must follow a name"
}

assign_to var1 12
echo "var1: $var1"