SC2148: Missing Shebang Line in Shell Scripts
Why every shell script needs a shebang (#!/bin/bash) line. Learn the difference between #!/bin/bash, #!/bin/sh, and #!/usr/bin/env bash.
Detailed Explanation
The Shebang Line
The shebang (hash-bang) is the first line of a script that tells the operating system which interpreter to use. Without it, the behavior depends on the calling shell and is unpredictable.
Common Shebangs
#!/bin/bash # Bash (absolute path)
#!/usr/bin/env bash # Bash (portable, uses PATH lookup)
#!/bin/sh # POSIX shell (may be dash, ash, etc.)
#!/usr/bin/env sh # POSIX shell (portable)
#!/bin/zsh # Zsh
Why It Matters
Without a shebang, executing ./script.sh may use:
- The current shell as interpreter
/bin/shon some systems- Fail entirely on others
# Without shebang, this Bash-specific syntax might fail:
if [[ $var =~ regex ]]; then
echo "match"
fi
# If executed by /bin/sh (dash), [[ is a syntax error
#!/bin/bash vs #!/usr/bin/env bash
| Approach | Pros | Cons |
|---|---|---|
#!/bin/bash |
Direct, fast | Fails if bash is not at /bin/bash (NixOS, FreeBSD) |
#!/usr/bin/env bash |
Portable, finds bash in PATH | Slightly slower, cannot pass flags on some systems |
#!/bin/sh vs #!/bin/bash
Using #!/bin/sh means your script must be POSIX-compliant. Bash-specific
features are not available:
| Feature | POSIX sh | Bash |
|---|---|---|
[[ ]] |
No | Yes |
| Arrays | No | Yes |
${var//pattern/replace} |
No | Yes |
function keyword |
No | Yes |
Process substitution <() |
No | Yes |
Best Practice
Use #!/usr/bin/env bash for scripts that use Bash features, and
#!/bin/sh only when you have verified POSIX compliance.
Use Case
Every shell script you write or distribute. Particularly critical for scripts shared between different systems (Linux distributions, macOS, CI/CD runners) where the default shell may vary.
Try It — Shell Script Linter
Related Topics
Bash Strict Mode: set -euo pipefail Explained
Script Setup
[ ] vs [[ ]] in Bash: Single vs Double Brackets
Deprecated Syntax
Writing POSIX-Compatible Shell Scripts
POSIX Compatibility
SC2086: Unquoted Variable Expansion in Shell Scripts
Quoting
SC2006: Replace Backtick Command Substitution with $()
Deprecated Syntax