🦀 ClawHub
Bash
by @ivangdavila
Avoid common Bash mistakes — quoting traps, word splitting, and subshell gotchas.
TERMINAL
clawhub install bash📖 About This Skill
name: Bash slug: bash version: 1.0.2 description: Write reliable Bash scripts with proper quoting, error handling, and parameter expansion. metadata: {"clawdbot":{"emoji":"🖥️","requires":{"bins":["bash"]},"os":["linux","darwin"]}}
Quick Reference
| Topic | File |
|-------|------|
| Arrays and loops | arrays.md |
| Parameter expansion | expansion.md |
| Error handling patterns | errors.md |
| Testing and conditionals | testing.md |
Quoting Traps
"$var" not $var, spaces break unquoted"${arr[@]}" preserves elements—${arr[*]} joins into single string'$var' doesn't expand"$(command)" not $(command)Word Splitting and Globbing
$var splits on whitespace—file="my file.txt"; cat $file fails* expands to files—quote or escape if literal: "*" or \*set -f disables globbing—or quote everything properlyTest Brackets
[[ ]] preferred over [ ]—no word splitting, supports &&, ||, regex[[ $var == pattern* ]]—glob patterns without quotes on right side[[ $var =~ regex ]]—regex match, don't quote the regex-z is empty, -n is non-empty—[[ -z "$var" ]] tests if emptySubshell Traps
cat file | while read; do ((count++)); done—count lostwhile read < file or process substitution—while read; do ...; done < <(command)( ) is subshell, { } is same shell—variables in ( ) don't persistExit Handling
set -e exits on error—but not in if, ||, && conditionsset -u errors on undefined vars—catches typosset -o pipefail—pipeline fails if any command fails, not just lasttrap cleanup EXIT—runs on any exit, even errorsArrays
arr=(one two three)—or arr=() then arr+=(item)${#arr[@]}—not ${#arr}"${arr[@]}"—always quote${!arr[@]}—useful for sparse arraysParameter Expansion
${var:-default}—use default if unset/empty${var:=default}—also assigns to var${var:?error message}—exits with message${var:0:5}—first 5 chars${var#pattern}—## for greedyArithmetic
$(( )) for math—result=$((a + b))(( )) for conditions—if (( count > 5 )); then$ needed inside $(( ))—$((count + 1)) not $(($count + 1))Common Mistakes
[ $var = "value" ] fails if var empty—use [ "$var" = "value" ] or [[ ]]if [ -f $file ] with spaces—always quote: if [[ -f "$file" ]]local in functions—without it, variables are globalread without -r—backslashes interpreted as escapesecho portability—use printf for reliable formatting