← Back to Bash Course | Chapter 1: Setup & Basics | Lesson 4 of 12

The Shebang Line

The shebang is like the name label on a lunchbox that says who should open it. It tells the computer which helper program should read and run the file.

In this page:

  1. The Shebang Line
Syntax
bash
#!/path/to/interpreter
# script commands follow

The Shebang Line

The first line of a script, #!/bin/bash, is the shebang — it tells the operating system which interpreter should run the file when executed directly.

Without it, ./script.sh might use the wrong shell and behave unexpectedly. The shebang must be the very first line, with no blank line above it.

Note: #!/usr/bin/env bash is a more portable shebang that finds bash wherever it is on the PATH.

Example: The Shebang Line

bash
#!/bin/bash
# This shebang tells the OS to run this file with bash
echo "Running under: $0"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Putting a space or a blank line before #!, so it is not read as a shebang and the system uses a default shell.
  2. Writing a wrong interpreter path such as #!/bin/bsh, which gives bad interpreter: No such file or directory.
  3. Saving the script with Windows line endings (CRLF), so the interpreter is read as bash\r and fails.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.