Shebang Line: The First Line
The shebang line tells the system which interpreter to use.
Here's the thing: The shebang is the first line. It's important. Use it.
What It Is
#!/bin/bash
The #!: That's the shebang. Tells the system what to use.
My take: Shebang is simple. But important. Always use it.
Common Shebangs
bash
#!/bin/bash
My take: Use this for bash scripts. Most common.
sh
#!/bin/sh
My take: Use this for sh scripts. When you need portability.
Find in PATH
#!/usr/bin/env bash
My take: This finds bash in PATH. More portable. Use it.
Why It Matters
Without shebang, you need to run:
bash script.sh
With shebang, you can run:
./script.sh
My take: Shebang makes scripts executable. Use it.
Common Patterns
Standard bash
#!/bin/bash
Portable bash
#!/usr/bin/env bash
Common Mistakes (I've Made These)
-
Forgetting shebang: Always include shebang. First line.
-
Wrong path: Use
/bin/bashor/usr/bin/env bash. Check your system. -
Spaces: No spaces after
#!. It breaks. -
Wrong interpreter: Make sure you use the right one. bash for bash scripts.
What's Next?
Now that you understand shebang, let's talk about Making Scripts Executable.
Personal note: I used to forget shebang. Then scripts wouldn't run. Now I always include it. First line. Always.