koalaman / shellcheck

ShellCheck, a static analysis tool for shell scripts
https://www.shellcheck.net
GNU General Public License v3.0
36.23k stars 1.77k forks source link

`mapfile` inside function not defining variable. #1759

Open bartoszek opened 4 years ago

bartoszek commented 4 years ago

mapfile inside a function not working as expected. Output variable is reported as unasigned. Reference #350

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

#!/bin/bash

ldd_detect_broken_files() {
    mapfile -t files <(cat)
    for file in "${files[@]}"; do
       :
    done
}

Here's what shellcheck currently says:

Line 5  SC2154: files is referenced but not assigned (did you mean 'file'?).
Gandalf- commented 4 years ago

This appears to have to do with the way we're parsing the redirection, you can see the difference between *ShellCheck.Parser> debugParseScript "mapfile -t files < <(cat)" and *ShellCheck.Parser> debugParseScript "mapfile -t files <(cat)"

Both < <(cat) and <<< "string' are behave as you'd expect. I'll keep looking to see if I can find where the issue is exactly.

In the mean time, < <(cat) is equivalent and doesn't have this problem.