Add a shebang to your script

Without a shebang the script runs under whatever shell happens to invoke it. Always start with `#!/usr/bin/env bash` (or another explicit interpreter).

Problem

A script without a shebang line is interpreted by whatever shell happens to be running — `sh` on most systems, which is often `dash` on Debian/Ubuntu and `bash` in POSIX-mode on others. Bash-only features (`[[ ]]`, arrays, `<()`, etc.) will fail silently or with cryptic errors on systems where `/bin/sh` is not Bash.

Bad

# No shebang
[[ -f config ]] && source config
arr=(one two)

Good

#!/usr/bin/env bash
[[ -f config ]] && source config
arr=(one two)

Explanation

`#!/usr/bin/env bash` looks up Bash via `PATH`, which works on systems where Bash lives in non-standard locations (notably macOS with Homebrew Bash 5). Use `#!/bin/sh` only when the script is genuinely POSIX — and then avoid Bash-only syntax.

Related

SC2096

SC2239

When It Matters

Without a shebang the kernel refuses to execute the file directly, and whatever runs it falls back to a default — /bin/sh on many systems, the invoking shell in others, cmd.exe conventions in some CI runners. A script full of Bash arrays and [[ ]] then fails with a syntax error on a machine where sh is dash. ShellCheck also needs the shebang to know which dialect to check, so without one you get sh-level warnings on a Bash script.

Second Example

Note

A sourced library fragment that is never executed directly can carry the shell directive comment instead of a shebang.

Exceptions

Files that are only ever sourced — .bashrc fragments, config snippets, function libraries — do not need a shebang. Give them "# shellcheck shell=bash" so the analysis still uses the right dialect.

Faq

Q

Which shebang should a Bash script use?

A

#!/usr/bin/env bash resolves bash via PATH and works on systems where it is not in /bin. Hardcode /bin/bash only when the path is guaranteed.

Q

Is /bin/sh the same as Bash?

A

Not on Debian, Ubuntu, Alpine, or most containers, where it is dash or busybox ash. Bash-only syntax fails there.

Q

Does the shebang need to be the very first line?

A

Yes — the first two bytes of the file must be #!. A blank line or a comment before it disables the mechanism entirely.