0

New to shellcheck here. See pseudocode and error below.

#!/bin/bash
function throwAwayFunc() {
        local _array=();
}

function arraysFromArray() {
  local i _id; _id=$1;
  declare -n _array="${_id}";
#               ***^-- SC2178 (warning): Variable was used as an array but is now assigned a string.***
  for i in "${_array[@]}"; do
    makeArr "$i";
  done;
}

Edited to clarify context: I am, indeed, expecting an array here. Am I doing this wrong? My point here is that the declare -n here is, at worst, ambiguous, and yet, this is the error. So, can I ignore this?

BenG
  • 31
  • 5
  • Do you need the `_id` variable? `local -n _array=$1` – glenn jackman Jun 26 '22 at 15:03
  • 2
    This seems to be fixed in the most recent ShellCheck. What version do you use? – Benjamin W. Jun 26 '22 at 15:07
  • @glennjackman Yes, this is just a sample function. In a lot of cases, I do need the name of the array I'm working with. And, of course, it gripes if I use a direct assignment as well, so, not sure how to get around that. – BenG Jun 26 '22 at 18:09
  • @BenjaminW.Using the website directly for this. Someone else pointed me to it in an earlier post. – BenG Jun 26 '22 at 18:10
  • 1
    I put a shebang line before the code in the question and copied it into [Shellcheck](https://www.shellcheck.net/). The only warning was about the unused variable `i`. See [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) (MCVE). – pjh Jun 26 '22 at 18:50
  • 1
    Are you calling this function from another function where you also use the `_id` variable? – glenn jackman Jun 26 '22 at 19:44
  • @glennjackman I've provided some clarification above here. This is part of a larger array wrapper package I put together to make other aspects of my code less complex (namely, pretending that there's such a thing as multi-dimensional arrays in bash). In any case, the point here is that I am, in fact, declaring an array here by way of name-ref which is the point, I think, of declare -n. What I'm wondering is whether the parser just doesn't relate to it that way or if there is something going on under the hood that I should know about. – BenG Jun 26 '22 at 23:37
  • Check the wiki page for this one: https://www.shellcheck.net/wiki/SC2178 -- there is a known bug – glenn jackman Jun 26 '22 at 23:48

0 Answers0