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 #.
#!/bin/bash #!/usr/bin/env bash #!/usr/bin/env python3
Try it
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.
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
#!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. SearchesPATHforbash, so a newer Bash from Homebrew or Nix is picked up. The cost is that the result depends on the user'sPATH, which is a weakness for setuid, cron or service scripts./usr/bin/envitself is almost universally present.- Passing options. Put them inside the script instead:
set -euo pipefail. On GNUenvyou can use#!/usr/bin/env -S bash -e -uto split arguments, but that is not portable to older systems. #!/bin/shmeans POSIX only. Scripts using arrays,[[ ]]orlocalwith#!/bin/shbreak whereshis dash (Debian, Ubuntu). Use#!/bin/bashwhen you want Bash features.- Make it executable.
chmod +x script.sh. Without the x bit, run it asbash 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 withsed -i 's/\r$//' script.sh. - Sourced files (
source file) ignore the shebang: it is just a comment there. Use a# shellcheck shell=bashdirective if you want linters to know the dialect. - Does not apply to
bash -cstrings, and does not need a file extension:.shis only a convention.