So I set up to learn Bash better. Normally I try to learn by doing koans, but I could not find any, so I decided to learn by building koans for Bash.
I am no Bash expert. This is a project strictly for learning and fun. It's also not complete, I'm doing this when I have a moment.
If you are an expert, PLEASE contribute and make it even better!
Just clone this repo and run:
./meditate
License is MIT. See LICENSE for details.
- Understanding
$?: Every command in Bash sets an exit status stored in$?.0indicates success.- Non-zero indicates failure (commonly
1).
- Examples:
cd /nonexistent→ exit status1(failure).ls ~/→ exit status0(success).
- Key takeaway: Always check
$?to determine if a command succeeded or failed, especially in scripts that depend on previous commands.
- Local vs Global Variables:
localvariables are only visible within the function they are defined in.- Variables defined without
localare global and accessible anywhere.
- Variable Expansion:
- Variables expand inside double quotes (
"foo $var") and with${var}syntax (this_is_${var}_yay).
- Variables expand inside double quotes (
- Unsetting Variables:
unset varremoves a variable value so it no longer exists.
- Exporting Variables:
- Only exported variables (
export VAR=value) are visible to processes. - Local or nonexported variables remain invisible to processes.
- Only exported variables (
- Key takeaway: Understanding scoping and exporting is crucial for writing reliable Bash scripts.