shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

SC1007 uncovered arrays

Open IvanGrigorik opened this issue 4 months ago • 2 comments

For bugs with existing features

  • SC2155:
  • online:
  • [X] The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
  • [X] I tried on https://www.shellcheck.net/ and verified that this is still a problem on the latest commit

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

#!/bin/bash
export a="$(ls -la ./)"
export b=("$(ls -la ./)")
echo "$a"
echo "${b[@]}"

Here's what shellcheck currently says:

In btest.sh line 2:
export a="$(ls -la ./)"
       ^-- SC2155 (warning): Declare and assign separately to avoid masking return values.

Here's what I wanted or expected to see:

In btest.sh line 2:
export a="$(ls -la ./)"
       ^-- SC2155 (warning): Declare and assign separately to avoid masking return values.
In btest.sh line 3:
export b=("$(ls -la ./)")
       ^-- SC2155 (warning): Declare and assign separately to avoid masking return values.

Rationale

ShellCheck should throw a declare and assign separately warning, no matter if we are assigning an array or a variable.

IvanGrigorik avatar Sep 17 '25 20:09 IvanGrigorik

SC2155: declare and assign separately seems to be not a relevant warning at all for the case you provide

SC2155 is totally reasonable for things kinda export a="$(ls -la ./)" or local a="$(ls -la ./)", but there's no way to declare and assign separately, when it goes about readonly variable

juliyvchirkov avatar Sep 18 '25 12:09 juliyvchirkov

This comment by @juliyvchirkov seems unrelated to this issue, since the main idea of the issue and the comment differ. The attributes may differ, while expected and actual behaviour would be the same, so the specific attribute does not matter. For the sake of simplicity, I changed the attributes to export in the issue example


Regarding the comment, ShellCheck specifies how to deal with readonly variables in the following wiki page

It should be done like this:

#!/bin/bash
a="$(ls -la ./)"
b=("$(ls -la ./)")
readonly a
readonly b
echo "$a"
echo "${b[@]}"

So the issue should be fixed for all attributes, including readonly

IvanGrigorik avatar Sep 18 '25 16:09 IvanGrigorik