Skip to content

Shebang with more than one parameter SH-2096 , Variables must be defined before use SH-2154 #1032

@philipjonsen

Description

@philipjonsen

DESCRIPTION

Problematic code:
#!/usr/bin/env bash -x

Correct code:
#!/usr/bin/env bash
set -x

Rationale:
Most operating systems, including POSIX, Linux and FreeBSD, allow only a single parameter in the shebang. The example is equivalent to calling env 'bash -x' instead of env 'bash' '-x', and it will therefore fail.

The shebang should be rewritten to use at most one parameter. Shell options can instead be set in the body of the script.

Exceptions
Mac OS currently allows multiple words in the shebang. In this case, you can ignore this issue.

DESCRIPTION
A variable has been referenced that is not known to have been assigned earlier in the script. Double-check that the variable has indeed been assigned before being referenced and that it does not contain any typos.

Note: This issue only triggers for variables with lowercase characters in their name (foo and kFOO but not FOO) due to the standard convention of using lowercase variable names for unexported, local variables.

Exceptions
The checker intentionally does not attempt to figure out runtime or dynamic assignments like with source "$(date +%F).sh" or eval var=value. If you know for a fact that the variable is set, you can use ${var:?} to fail if the variable is unset (or empty), initialize it to a default value if uninitialized with : "${var:=}", or explicitly initialize/declare it with var="" or declare var. You can also disable this issue by using a // skipcq: SH-2154 pragma.

References
POSIX - Parameter expansion: * https://stackoverflow.com/a/16753536/2309247 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3chap02.html#tag180602

bug

Screenshots
If applicable, add screenshots to help explain your problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions