๐ Scripts and Shebang
Scripts allow you to save sequences of commands for repeated execution. The shebang line tells the system how to interpret the script.
๐งญ Creating a Script
Create a new file with a .sh extension:
1 | |
Write your commands:
1 2 3 | |
Make it executable:
1 | |
Run it:
1 | |
๐ง Shebang Explained
First line specifies interpreter:
1 | |
Other common shebangs:
1 2 3 4 | |
Using env locates interpreter dynamically:
1 | |
Important considerations: - Must be first line (no blank lines before!) - Points to actual executable path - Not necessary for sourced files
๐งช Running Scripts
Several methods exist:
| Method | Notes |
|---|---|
./script.sh |
Requires execute permission |
sh script.sh |
Runs regardless of permissions |
source script.sh |
Runs in current shell context |
. script.sh |
Same as source |
Choosing depends on desired scope and reusability.
๐งพ Summary
- Scripts bundle commands for reuse.
- Shebang determines interpreter.
- Make scripts executable with
chmod +x. - Consider portability when choosing interpreter.
๐ Continue to: Portability