Przejdลบ do treล›ci

๐Ÿ“œ 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
nano myscript.sh

Write your commands:

1
2
3
#!/bin/sh
echo "Hello, World!"
date

Make it executable:

1
chmod +x myscript.sh

Run it:

1
./myscript.sh


๐Ÿ”ง Shebang Explained

First line specifies interpreter:

1
#!/bin/sh

Other common shebangs:

1
2
3
4
#!/bin/bash
#!/bin/zsh
#!/usr/bin/env python3
#!/usr/bin/perl

Using env locates interpreter dynamically:

1
#!/usr/bin/env node

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