mvdan/sh

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

laurenthuberdeau opened this issue · 2 comments

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.

See my response to #858. Bash supports more trickery than we do, and at least for now, that's an intentional limitation.

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"