| name | cli-command-patterns |
| description | Cross-platform CLI command patterns, shell tips, and defensive construction techniques. Load when building shell commands, piping output, or working across Windows/macOS/Linux. |
| version | 1.1.0 |
CLI Command Patterns
All platforms use bash. On Windows, commands run in Git Bash by default. Use forward slashes in paths (C:/Users/...). MSYS_NO_PATHCONV=1 is set automatically.
Windows Notes
- GUI apps block - always append
& to background: notepad.exe &
.exe suffix - optional in Git Bash (notepad and notepad.exe both work)
- PowerShell - use
powershell -c "command" for Windows-only operations (registry, services, COM)
- Windows-only tools -
tasklist, taskkill, netstat, systeminfo work from bash
Command Chaining
command_a && command_b
command_a || command_b
command_a ; command_b
(cmd && echo ok) || fallback
Output Filtering
command | grep "pattern"
grep -c "ERROR" logfile
command | awk '{print $2}'
command | sort | uniq -c | sort -rn
command | head -20
grep -n -C 3 "error" log.txt
JSON (jq)
echo '{"name":"Alice"}' | jq '.name'
cat data.json | jq '.[] | select(.status=="err")'
powershell -c "(Get-Content data.json | ConvertFrom-Json).field"
Text manipulation
echo "hello world" | sed 's/world/there/'
grep -v '^$' file.txt
sed -n '10,20p' file.txt
wc -l file.txt
Defensive Construction
Quoting - #1 source of agent bugs
cat "/Users/john/My Documents/file.txt"
grep -F "$user_input" file.txt
Paths
cat /home/user/file.txt
cat file.txt
realpath "$path"
test -f "$file" && cat "$file"
Timeout
timeout 10 find / -name "*.log"
File Operations
cat > file.txt << 'EOF'
Line one
Line two with $dollar signs preserved
EOF
echo "content" > file.txt
echo "more" >> file.txt
for f in *.txt; do mv "$f" "${f%.txt}.md"; done
find . -name "*.tmp" -delete
find . -size +100M -type f
cp -R src/ dst/
cat -n file.txt
sed -n '10,20p' file.txt
grep -o "pattern" file | wc -l
Process & System
ps aux | grep -i "chrome"
tasklist | grep -i "chrome"
lsof -i :8080
ss -tlnp | grep 8080
netstat -ano | grep :8080
pkill -f "name"
taskkill /f /im name.exe
curl -L -o output.zip "https://example.com/file.zip"
Git Patterns
git branch --show-current
git diff main...HEAD --stat
git log --all --oneline --grep="fix login"
git show HEAD~3:path/to/file.py
git blame -L 10,20 file.py
git log -S "function_name" --oneline
git reset --soft HEAD~1
git ls-files --others --exclude-standard
git stash push -m "wip: feature x"
Power Patterns
find . -name "*.pyc" | xargs rm
cat urls.txt | xargs -I {} curl -s {}
diff <(ls dir1) <(ls dir2)
(cd /tmp && do_work)
touch file_{a,b,c}.txt
cp config.yaml{,.bak}
mkdir day_{01..31}
Heredocs
cat > script.py << 'PYEOF'
def hello():
print("Hello from generated script")
PYEOF
cat > config.txt << EOF
home=$HOME
date=$(date)
EOF
git commit -m "$(cat <<'EOF'
feat: add new feature
Multi-line description here.
EOF
)"
PowerShell (Windows-only operations)
Use powershell -c "..." from bash when you need registry, services, COM, or other Windows-only APIs:
powershell -c "(Get-FileHash 'file.txt' -Algorithm SHA256).Hash"
powershell -c "Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion'"
powershell -c "Get-Service | Where-Object {$_.Status -eq 'Running'}"
powershell -c "Compress-Archive -Path 'folder' -DestinationPath 'archive.zip'"
powershell -c "Expand-Archive -Path 'archive.zip' -DestinationPath 'output'"
Agent Principles
mkdir -p /path
cp -n src dst
grep -q "line" file || echo "line" >> file
echo "content" > /tmp/tempfile && mv /tmp/tempfile /final/path
curl -o file.zip URL && test -s file.zip && echo "ok"
command > /dev/null 2>&1
command | head -50
git log --oneline -10
command 2>&1 || echo "FAILED: command"
command; code=$?; [ $code -ne 0 ] && echo "failed: $code"
filename="${1:-default.txt}"
Quick Reference
| Pattern | What it does |
|---|
cmd1 && cmd2 | Run cmd2 only if cmd1 succeeds |
cmd1 || cmd2 | Run cmd2 only if cmd1 fails |
$(command) | Substitute command output inline |
command > file 2>&1 | Redirect stdout + stderr |
command | tee file | Write to file AND stdout |
command & | Run in background |
$? | Exit code of last command |
${var:-default} | Use default if var is unset |
${var%suffix} | Remove suffix from var |
> / >> | Overwrite / append redirect |
2>/dev/null | Suppress stderr |
<(command) | Process substitution |
{a,b,c} | Brace expansion |