Let's say I have a shell script, test.sh
, that is supposed to echo some number of values. I'd like to be able to pass those values in via an environment variable with glob expansion:
$ ls
1.js 2.js test.sh*
$ FOO="*.js" bash ./test.sh
1.js 2.js
Easy, you say! Just write
#!/bin/bash
echo $FOO
and indeed, this works. But it does not pass ShellCheck, which calls us out for using implicit globbing/expansion (SC2086). (And this is fair; in the original code, the echo line was cp $FOO $BAR
, where indeed we didn't want to expand $BAR
; this error catches bugs.)
So, in a quest to make this more explicit, I was hoping that maybe the following would work:
#!/bin/bash
array=( $FOO )
echo "${array[@]}"
but no: we end up with SC2206.
Is there a canonical way of doing this kind of thing, which falls within what the ShellCheck authors consider best practices? (Feel free to use the online checker at www.shellcheck.net to test it out.) Is this the wrong approach entirely? It seems unlikely the ShellCheck authors never thought of this use case...