Bash/Linux terminal patterns. Critical commands, piping, error handling, scripting. Use when working on macOS or Linux systems.
Add this skill
npx mdskills install sickn33/bash-linuxComprehensive reference guide with tables and examples, but lacks agent-specific instruction patterns
1---2name: bash-linux3description: Bash/Linux terminal patterns. Critical commands, piping, error handling, scripting. Use when working on macOS or Linux systems.4allowed-tools: Read, Write, Edit, Glob, Grep, Bash5---67# Bash Linux Patterns89> Essential patterns for Bash on Linux/macOS.1011---1213## 1. Operator Syntax1415### Chaining Commands1617| Operator | Meaning | Example |18|----------|---------|---------|19| `;` | Run sequentially | `cmd1; cmd2` |20| `&&` | Run if previous succeeded | `npm install && npm run dev` |21| `\|\|` | Run if previous failed | `npm test \|\| echo "Tests failed"` |22| `\|` | Pipe output | `ls \| grep ".js"` |2324---2526## 2. File Operations2728### Essential Commands2930| Task | Command |31|------|---------|32| List all | `ls -la` |33| Find files | `find . -name "*.js" -type f` |34| File content | `cat file.txt` |35| First N lines | `head -n 20 file.txt` |36| Last N lines | `tail -n 20 file.txt` |37| Follow log | `tail -f log.txt` |38| Search in files | `grep -r "pattern" --include="*.js"` |39| File size | `du -sh *` |40| Disk usage | `df -h` |4142---4344## 3. Process Management4546| Task | Command |47|------|---------|48| List processes | `ps aux` |49| Find by name | `ps aux \| grep node` |50| Kill by PID | `kill -9 <PID>` |51| Find port user | `lsof -i :3000` |52| Kill port | `kill -9 $(lsof -t -i :3000)` |53| Background | `npm run dev &` |54| Jobs | `jobs -l` |55| Bring to front | `fg %1` |5657---5859## 4. Text Processing6061### Core Tools6263| Tool | Purpose | Example |64|------|---------|---------|65| `grep` | Search | `grep -rn "TODO" src/` |66| `sed` | Replace | `sed -i 's/old/new/g' file.txt` |67| `awk` | Extract columns | `awk '{print $1}' file.txt` |68| `cut` | Cut fields | `cut -d',' -f1 data.csv` |69| `sort` | Sort lines | `sort -u file.txt` |70| `uniq` | Unique lines | `sort file.txt \| uniq -c` |71| `wc` | Count | `wc -l file.txt` |7273---7475## 5. Environment Variables7677| Task | Command |78|------|---------|79| View all | `env` or `printenv` |80| View one | `echo $PATH` |81| Set temporary | `export VAR="value"` |82| Set in script | `VAR="value" command` |83| Add to PATH | `export PATH="$PATH:/new/path"` |8485---8687## 6. Network8889| Task | Command |90|------|---------|91| Download | `curl -O https://example.com/file` |92| API request | `curl -X GET https://api.example.com` |93| POST JSON | `curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' URL` |94| Check port | `nc -zv localhost 3000` |95| Network info | `ifconfig` or `ip addr` |9697---9899## 7. Script Template100101```bash102#!/bin/bash103set -euo pipefail # Exit on error, undefined var, pipe fail104105# Colors (optional)106RED='\033[0;31m'107GREEN='\033[0;32m'108NC='\033[0m'109110# Script directory111SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"112113# Functions114log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }115log_error() { echo -e "${RED}[ERROR]${NC} $1" >&2; }116117# Main118main() {119 log_info "Starting..."120 # Your logic here121 log_info "Done!"122}123124main "$@"125```126127---128129## 8. Common Patterns130131### Check if command exists132133```bash134if command -v node &> /dev/null; then135 echo "Node is installed"136fi137```138139### Default variable value140141```bash142NAME=${1:-"default_value"}143```144145### Read file line by line146147```bash148while IFS= read -r line; do149 echo "$line"150done < file.txt151```152153### Loop over files154155```bash156for file in *.js; do157 echo "Processing $file"158done159```160161---162163## 9. Differences from PowerShell164165| Task | PowerShell | Bash |166|------|------------|------|167| List files | `Get-ChildItem` | `ls -la` |168| Find files | `Get-ChildItem -Recurse` | `find . -type f` |169| Environment | `$env:VAR` | `$VAR` |170| String concat | `"$a$b"` | `"$a$b"` (same) |171| Null check | `if ($x)` | `if [ -n "$x" ]` |172| Pipeline | Object-based | Text-based |173174---175176## 10. Error Handling177178### Set options179180```bash181set -e # Exit on error182set -u # Exit on undefined variable183set -o pipefail # Exit on pipe failure184set -x # Debug: print commands185```186187### Trap for cleanup188189```bash190cleanup() {191 echo "Cleaning up..."192 rm -f /tmp/tempfile193}194trap cleanup EXIT195```196197---198199> **Remember:** Bash is text-based. Use `&&` for success chains, `set -e` for safety, and quote your variables!200
Full transparency — inspect the skill content before installing.