How to pass glob to shell script via environment variable when passing ShellCheck

Let's say I have a shell script test.sh, which should drive away a certain amount of values. I would like to be able to pass these values ​​through an environment variable with the glob extension:

$ ls
1.js  2.js  test.sh*

$ FOO="*.js" bash ./test.sh
1.js 2.js

Easy talk! Just write

#!/bin/bash
echo $FOO

and indeed it works. But it does not pass ShellCheck , which calls us to use implicit globbing / extension ( SC2086 ). (And this is true: in the source code, the echo line was cp $FOO $BARwhere we really did not want to expand $BAR, this error detects errors.)

So, in an effort to make this more explicit, I was hoping that perhaps the following would work:

#!/bin/bash
array=( $FOO )
echo "${array[@]}"

: SC2206.

, , ShellCheck ? ( - www.shellcheck.net, .) ? , ShellCheck ...

+6
1

ShellCheck. ShellCheck . , . SC2206 :

:

( IFS set -f), , , .

ShellCheck , :

Shellcheck :

hexToAscii() {
  # shellcheck disable=SC2059
  printf "\x$1"
}

  • disable, :

    # shellcheck disable=code[,code...]
    statement_where_warning_should_be_disabled
    

    ...

, shebang script. , (, case case ).

, :

#!/usr/bin/env bash

# The following line is needed so that the shellcheck directive
# below doesn't have global effect
:

# shellcheck disable=SC2206
array=( $FOO )
echo "${array[@]}"
+5

All Articles