Ignore a ShellCheck rule on one line
Silence one ShellCheck finding on one command with a directive comment, # shellcheck disable=SC2086, placed on the line above, and give a reason with it.
ShellCheck is right in general and wrong for this line, where the unquoted expansion is deliberate.
cat > opts.sh <<'EOF'
#!/bin/bash
flags="-l -a"
ls $flags /tmp > /dev/null
# shellcheck disable=SC2086 # the flags are meant to split into separate words
ls $flags /tmp > /dev/null
EOF
shellcheck opts.sh
echo "exit: $?"
In opts.sh line 3:
ls $flags /tmp > /dev/null
^----^ SC2086 (info): Double quote to prevent globbing and word splitting.
Did you mean:
ls "$flags" /tmp > /dev/null
For more information:
https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...
exit: 1A comment of the form # shellcheck disable=CODE applies to the next command, as the directive documentation explains. Only the second ls is silenced, so the first still produces the finding, and the text after a second hash on the same comment is ignored by ShellCheck, which leaves room for the reason. A directive placed on its own line before a function covers the whole function.
A directive at the top of the file, before the first command, applies to the entire script, which hides real problems for good. Keep it narrow and write the reason, since the next reader cannot tell why the rule was turned off. An array, flags=(-l -a) with "${flags[@]}", removes the need for the directive in Bash.