Shebang and Script Execution
The shebang line is the first and most important line in any script. It tells the operating system how to execute your script.
What is a Shebang?
The shebang (also called hashbang) is a special character sequence at the very beginning of a script:
#!/bin/bash
It consists of:
#!- The magic number that identifies this as a script/bin/bash- The path to the interpreter
Why is the Shebang Important?
Without a shebang, the system doesn't know which interpreter to use:
# With shebang - system knows to use bash
#!/bin/bash
echo "This runs with bash"
# Without shebang - behavior is undefined
echo "Which shell runs this?"
Common Shebang Lines
| Shebang | Interpreter | Use Case |
|---|---|---|
#!/bin/bash | Bash | Most common, bash-specific features |
#!/bin/sh | POSIX shell | Maximum portability |
#!/usr/bin/env bash | Bash via env | Flexible path lookup |
#!/usr/bin/env python3 | Python 3 | Python scripts |
#!/usr/bin/env node | Node.js | JavaScript scripts |
The env Trick
Using #!/usr/bin/env bash instead of #!/bin/bash has advantages:
#!/usr/bin/env bash
# Finds bash wherever it's installed
# More portable across different systems
This is useful because:
- Bash might be at
/bin/bash,/usr/bin/bash, or elsewhere envsearches PATH to find the interpreter- Works across different Unix/Linux distributions
Shebang Rules
Critical rules:
- Must be the very first line (no blank lines before it)
- Must start at column 1 (no spaces before
#!) - Path must be absolute or use
env
# WRONG - space before shebang
#!/bin/bash
# WRONG - blank line before shebang
#!/bin/bash
# CORRECT
#!/bin/bash
# Rest of script follows
Exercise: Identify Shebang
Check what shebang the backup script uses:
How Script Execution Works
When you run a script, here's what happens:
# Step 1: You execute the script
./myscript.sh
# Step 2: System reads the shebang
#!/bin/bash
# Step 3: System actually runs:
/bin/bash ./myscript.sh
The shebang transforms your command into an interpreter call.
Ways to Execute Scripts
There are multiple ways to run a script:
# Method 1: Direct execution (requires execute permission)
./script.sh
# Method 2: Explicit interpreter (ignores shebang)
bash script.sh
# Method 3: Source (runs in current shell)
source script.sh
# or
. script.sh
Differences Between Methods
| Method | New Shell | Inherits Variables | Needs +x |
|---|---|---|---|
./script.sh | Yes | No | Yes |
bash script.sh | Yes | No | No |
source script.sh | No | Yes | No |
Sourcing vs Executing
Executing creates a new shell process:
# script.sh
#!/bin/bash
MY_VAR="set in script"
# Running it
./script.sh
echo $MY_VAR # Empty! Variable was set in child shell
Sourcing runs in the current shell:
source script.sh
echo $MY_VAR # "set in script" - variable persists!
When to Use Each Method
| Scenario | Recommended Method |
|---|---|
| Normal script execution | ./script.sh |
| Testing without permissions | bash script.sh |
| Setting environment variables | source script.sh |
| Loading functions/aliases | source ~/.bashrc |
Exercise: Execute a Script
Create and execute a simple script:
Key Takeaways
- The shebang (
#!/bin/bash) must be the first line of every script - It tells the system which interpreter to use
- Use
#!/usr/bin/env bashfor maximum portability ./script.shexecutes in a new shell (requires +x permission)bash script.shexecutes in a new shell (no permission needed)source script.shruns in the current shell (variables persist)
التالي · الدرس 3 من 34
Script Permissions and Running
قراءة 3 دقيقة
أنهيت 2 من 34 درسًا

اسأل Byte عن هذا الدرس
محادثة خاصة حول هذا الدرس. قرأ Byte الدرس كاملًا، ويجيب خلال ثوانٍ، ويُظهر القسم الذي تأتي منه كل إجابة. يمكنك السؤال بأي لغة.
الأسئلة والأجوبة
أسئلة عامة حول هذا الدرس. يجيب AI Tutor فورًا بلغتك، ويراجع فريق FreeAcademy الإجابات، ويمكن لكل المتعلمين قراءتها.

