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

The Shebang Line & Running Scripts

The shebang is a special first line in a script that tells the computer exactly which program should read and run the rest of the file.

Anatomy of the Shebang

The shebang #!/bin/bash tells the operating system's loader which interpreter to hand the rest of the file to when it is executed directly. It must be the first two characters of the file with no leading blank lines or spaces, otherwise it is just treated as an ordinary comment.

Note: $0 holds the script's own filename/path, which is why head -n 1 "$0" prints the shebang line back out.

Example: Anatomy of the Shebang

bash
#!/bin/bash
echo "This script's interpreter is chosen by the first line"
head -n 1 "$0"

Making a Script Executable

Before you can run a script as ./script.sh, the filesystem must mark it executable using chmod +x script.sh. Without that permission bit set, the kernel refuses to execute the file directly and reports 'Permission denied', even though the file's contents are perfectly valid Bash.

Example: Making a Script Executable

bash
#!/bin/bash
cat <<'EOF' > demo.sh
#!/bin/bash
echo "hello from demo.sh"
EOF
chmod +x demo.sh
ls -l demo.sh
./demo.sh

./script.sh vs bash script.sh

Running ./script.sh asks the kernel to execute the file directly, which requires the execute bit and relies on the shebang to choose the interpreter. Running bash script.sh instead starts Bash explicitly and hands it the file as an argument, so no execute permission or shebang is strictly required, but the shebang is still good practice for consistency.

Example: ./script.sh vs bash script.sh

bash
#!/bin/bash
cat <<'EOF' > greet.sh
echo "hi, no shebang needed for this method"
EOF
bash greet.sh
chmod +x greet.sh
echo '#!/bin/bash' | cat - greet.sh > greet2.sh
chmod +x greet2.sh
./greet2.sh

Checking Which Bash Is Used

#!/usr/bin/env bash looks up bash using the PATH environment variable rather than hard-coding /bin/bash, which matters on systems where Bash lives elsewhere (like some macOS or BSD setups). which bash or type bash shows exactly which binary would be used.

Warning: Hard-coding #!/bin/bash is fine on most Linux systems, but #!/usr/bin/env bash is more portable across different Unix-like systems.

Example: Checking Which Bash Is Used

bash
#!/bin/bash
which bash
type bash
echo "Running under: $BASH"
Common Mistakes
  1. Forgetting that the shebang must be the very first line, first two characters (#!), with nothing before it -- even a blank line above it breaks it.
  2. Trying to run ./script.sh without first making it executable with chmod +x, then being confused by a 'Permission denied' error.
  3. Assuming ./script.sh and bash script.sh always behave identically; if the shebang is missing or wrong, ./script.sh may use a different interpreter than you expect, while bash script.sh always forces Bash regardless of the shebang.
Chapter Summary
  • The shebang line has the exact form #!/bin/bash or #!/usr/bin/env bash as the very first line of the file.
  • chmod +x script.sh grants execute permission so the file can be run directly as ./script.sh.
  • ./script.sh uses the shebang to pick the interpreter; bash script.sh always uses Bash and does not require execute permission or a shebang.
  • #!/usr/bin/env bash is more portable across systems where Bash may not be installed at exactly /bin/bash.

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.