Bash Scripting
Shebang
Programming
Linux
Coding Practices

What is the preferred Bash shebang ("#!")?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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:

bash
#!/usr/bin/env bash

and:

bash
#!/bin/bash

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:

bash
1#!/usr/bin/env bash
2set -euo pipefail
3
4echo "Running with Bash"

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:

bash
1#!/usr/bin/env bash
2
3items=("a" "b" "c")
4printf '%s\n' "${items[@]}"

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:

bash
#!/usr/bin/env bash
set -euo pipefail

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 bash for portability across user environments
  • choose #!/bin/bash when 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 env without understanding that it depends on PATH.
  • Hardcoding /bin/bash in 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 bash is often the best default for portable developer environments.'
  • '#!/bin/bash is 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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.