Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

The shebang line (#!)

Reference for the shebang (#!) line: how the kernel picks an interpreter, bin/bash versus env bash, passing one option, CRLF problems and how to make scripts executable.

The shebang (also called hashbang) is the first line of a script when it begins with the two characters #! followed by the path of an interpreter. When you run the file as a program, the kernel reads that line and starts the named interpreter, passing the script's path as an argument. Without it, the file is not a valid executable image, and a shell that is asked to run it usually falls back to interpreting it as a script for itself, which may be the wrong language.

The line is a convention handled by the operating system, not by Bash. To Bash itself it is simply a comment, because it starts with #.

Terminal
#!/bin/bash
#!/usr/bin/env bash
#!/usr/bin/env python3

Try it

Bash The kernel runs the interpreter named in line one

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Different interpreters, env and one-argument limits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
#!interpreter [optional-argument]
Name Description
#! Must be the first two bytes of the file. Nothing, not even a blank line or a byte order mark, may come before it.
interpreter Absolute path of a program that can read a script. Relative names are not searched in PATH.
optional-argument At most one argument, passed as a single string.

Returns: the status of the interpreter. If the interpreter does not exist, the shell reports "bad interpreter: No such file or directory" and returns 126 or 127 depending on the shell.

Errors and edge cases: a line over a few hundred bytes may be truncated (the Linux limit is 255 characters since kernel 5.1, 127 before). Only one argument is passed, so #!/bin/bash -e -u gives Bash the single string -e -u on Linux.

Usage notes

  • #!/bin/bash. Predictable and fast, but only works if Bash lives there. On macOS it is the old Bash 3.2, on NixOS and some containers it is somewhere else.
  • #!/usr/bin/env bash. Searches PATH for bash, so a newer Bash from Homebrew or Nix is picked up. The cost is that the result depends on the user's PATH, which is a weakness for setuid, cron or service scripts. /usr/bin/env itself is almost universally present.
  • Passing options. Put them inside the script instead: set -euo pipefail. On GNU env you can use #!/usr/bin/env -S bash -e -u to split arguments, but that is not portable to older systems.
  • #!/bin/sh means POSIX only. Scripts using arrays, [[ ]] or local with #!/bin/sh break where sh is dash (Debian, Ubuntu). Use #!/bin/bash when you want Bash features.
  • Make it executable. chmod +x script.sh. Without the x bit, run it as bash script.sh, which ignores the shebang.
  • Check line endings. A script saved with Windows CRLF line endings fails with a "bad interpreter" error because the interpreter name becomes bash\r. Fix it with sed -i 's/\r$//' script.sh.
  • Sourced files (source file) ignore the shebang: it is just a comment there. Use a # shellcheck shell=bash directive if you want linters to know the dialect.
  • Does not apply to bash -c strings, and does not need a file extension: .sh is only a convention.