What is the preferred Bash shebang ("#!")?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If your script is written for Bash, the shebang should make that requirement explicit. The two common choices are #!/usr/bin/env bash and #!/bin/bash, and the better one depends on your portability goals. In general, #!/usr/bin/env bash is preferred when you want to find Bash through the current environment, while #!/bin/bash is preferred when you want a fixed known path.
What the Shebang Does
The shebang is the first line of an executable script. It tells the operating system which interpreter to use when the script is launched directly.
For Bash scripts, the two most common options are:
and:
These lines do not change Bash syntax. They only change how the interpreter is found.
Why #!/usr/bin/env bash Is Often Preferred
env searches the current PATH for bash. That makes the script more portable across systems where Bash is installed in different locations.
Example script:
This approach is especially useful when:
- developers use different platforms
- Bash may live outside
/bin - tools are installed through package managers such as Homebrew
That is why it is a common default in cross-environment projects.
Why Some Teams Still Use #!/bin/bash
A fixed path can be desirable when:
- the environment is tightly controlled
- you want to avoid
PATH-based interpreter lookup - production machines guarantee Bash at a known location
In those environments, a hardcoded path is simpler and slightly more explicit operationally. It removes one layer of indirection.
So the question is not which line is always correct. It is which interpreter-discovery model matches the deployment environment.
Do Not Use #!/bin/sh for Bash Scripts
If your script depends on Bash features such as arrays, [[ ... ]], or set -o pipefail, do not label it as sh.
For example:
This is valid Bash. It is not guaranteed to work in plain POSIX sh.
Using the wrong shebang is one of the most common reasons a script works on one machine and breaks on another.
Pair the Shebang With Explicit Shell Options
A strong Bash script often begins with both the correct shebang and clear shell options:
Those options do not replace careful scripting, but they catch many silent failures early.
They also make the script's execution assumptions more obvious to the next maintainer.
Practical Decision Rule
Use this rule:
- choose
#!/usr/bin/env bashfor portability across user environments - choose
#!/bin/bashwhen the execution environment is fixed and controlled - never pretend a Bash-specific script is portable
sh
That keeps the interpreter contract honest.
Common Pitfalls
- Writing a Bash script but labeling it
#!/bin/sh. - Using
envwithout understanding that it depends onPATH. - Hardcoding
/bin/bashin environments where Bash may live elsewhere. - Treating the shebang as enough while ignoring shell options and script portability rules.
- Assuming a script is portable just because it runs on one machine.
Summary
- The preferred Bash shebang depends on whether you want portability or a fixed interpreter path.
- '
#!/usr/bin/env bashis often the best default for portable developer environments.' - '
#!/bin/bashis reasonable in tightly controlled systems.' - Use a Bash shebang only if the script actually requires Bash.
- Keep the interpreter declaration aligned with the script's real feature set.

