I am trying to enable a ShellCheck directive, which i disabled before in the same script file.
My code looks like this:
#!/bin/sh
code here......
# shellcheck disable=SC2016,SC2028
some code here......
# shellcheck enable=SC2016,SC2028
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ causes SC1125
What is the true syntax for achieve something like this??
I cannot verify this answer -- my shellcheck is version: 0.4.6, and there were changes to directives in 0.7.0 and 0.9.0.
However, Optional · koalaman/shellcheck Wiki · GitHub seems to state that enable does not reverse previous disable directives. It only activates a completely different set of checks which are classified as optional (which can be listed with shellcheck --list-optional).
The enable directive also seems to accept only names (not SC numbers), and not to permit a comma-separated list, and to also be allowed as command-line arguments with the -o option.
The Directives page Directive · koalaman/shellcheck Wiki · GitHub may also be helpful.
Note that every SCnnnn report has an explanatory page of its own, e.g. SC1125 · koalaman/shellcheck Wiki · GitHub
https://github.com/koalaman/shellcheck/wiki/SC1125
EDIT: Note also the scoping rules in the Directives page (again I have not tested these).
Directives that replace or are immediately after the shebang apply to the entire script. Otherwise, they are scoped to the command that follows it (including compound commands like function definitions, loops and case statements).
As I understand that, if you need to disable any checks selectively, you can wrap that section of code in either a compound statement or a function, and immediately precede that with a disable directive.
If you want to disable the first declared function or command, you need to precede the first disable directive with a dummy command (like true) so it does not apply globally.
I thought I should answer the actual question (and test my answer), although this test is on ShellCheck version: 0.4.6
My script: three identical groups of statements. (a) before disabling; (b) disabled; (c) check the disable went away.
#!/bin/sh
true
printf '.... Set (a)\n'
name=World
echo 'Hello $name' # Outputs Hello $name
echo "Name:\t$value"
# shellcheck disable=SC2016,SC2028
{
printf '.... Set (b)\n'
name=World
echo 'Hello $name' # Outputs Hello $name
echo "Name:\t$value"
}
printf '.... Set (c)\n'
name=World
echo 'Hello $name' # Outputs Hello $name
echo "Name:\t$value"
The output:
In ./myScript line 6:
echo 'Hello $name' # Outputs Hello $name
^-- SC2016: Expressions don't expand in single quotes, use double quotes for that.
In ./myScript line 7:
echo "Name:\t$value"
^-- SC2028: echo won't expand escape sequences. Consider printf.
^-- SC2154: value is referenced but not assigned.
In ./myScript line 18:
name=World
^-- SC2034: name appears unused. Verify it or export it.
In ./myScript line 19:
echo 'Hello $name' # Outputs Hello $name
^-- SC2016: Expressions don't expand in single quotes, use double quotes for that.
In ./myScript line 20:
echo "Name:\t$value"
^-- SC2028: echo won't expand escape sequences. Consider printf.
Interestingly, shellcheck reports only the first instance of SC2154, and only the last instance of SC2034. Logical, but surprising.