Shell Scripting
7 pages · ~5 min✓ Reviewed
Builds on Text Processing. Next up: Processes and Signals.
Shebang and Variables
1. Shebang and Running
- Shebang is line 1 only; the kernel reads it on exec
#!/usr/bin/env bashpicks bash from PATH#!/bin/bashis a fixed path
| Run | Shebang | Runs in | Vars persist |
|---|---|---|---|
chmod +x s.sh && ./s.sh | used | child | no |
bash s.sh | ignored | child | no |
source s.sh | ignored | current shell | yes |
2. Variables
bash
name=world # no spaces around = greet=${name:-hi} # fallback if unset or empty now=$(date +%F) # command substitution n=$((n+1)) # arithmetic readonly MAX=3 export MODE=dev
readonlyblocks reassignment;exportshows it to child processes- Untyped strings; global unless
localin a function