Skip to content

Variable assignments without brackets inside $(( ... )) operations confuse Shellcheck #3392

@Winterhuman

Description

@Winterhuman

For bugs with existing features

Here's a snippet or screenshot that shows the problem:

Arithmetic operations, $(( ... )), can contain variable assignments (e.g. var = 1); however, this breaks shellcheck's parsing and causes various unrelated errors if the assignments aren't wrapped in brackets (e.g. (var = 1)).

#!/bin/sh

arg="$1"	var2=""
var="$(( arg < 2 ? var2 = 1 : 0 ))"

echo "$var : $var2"

What this script prints when executed by sh, ksh, bash, or dash (no difference if POSIXLY_CORRECT=1 is set):

$ ./script.sh 1
1 : 1
$ ./script.sh 2
0 : 

Shellcheck suggests the following SCs for the script above: 1102, 2034, and 2210.


#!/bin/sh

arg="$1"	var2=""
var="$((
	arg < 2
	? var2 = 1
	: 0
))"

echo "$var : $var2"

What this script prints when executed by sh, ksh, bash, or dash (no difference if POSIXLY_CORRECT=1 is set):

$ ./script.sh 1
1 : 1
$ ./script.sh 2
0 : 

When the operations is written over multiple lines, Shellcheck additionally suggests SC2211 with the previously listed SCs.

If (var2 = 1) is used instead of var2 = 1, no SCs are suggested when using either version of the operation.

Here's what shellcheck currently says:

See above.

Here's what I wanted or expected to see:

Shellcheck should handle arithmetic operations containing bracket-less variable assignments which, as far as I can tell, is valid syntax for POSIX shell, Korn shell, Bash, and Dash.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions