$npx -y skills add One-Man-Company/Skills-ContextManager --skill bash-linuxBash/Linux terminal patterns. Critical commands, piping, error handling, scripting. Use when working on macOS or Linux systems.
| 1 | # Bash Linux Patterns |
| 2 | |
| 3 | > Essential patterns for Bash on Linux/macOS. |
| 4 | |
| 5 | --- |
| 6 | |
| 7 | ## 1. Operator Syntax |
| 8 | |
| 9 | ### Chaining Commands |
| 10 | |
| 11 | | Operator | Meaning | Example | |
| 12 | |----------|---------|---------| |
| 13 | | `;` | Run sequentially | `cmd1; cmd2` | |
| 14 | | `&&` | Run if previous succeeded | `npm install && npm run dev` | |
| 15 | | `\|\|` | Run if previous failed | `npm test \|\| echo "Tests failed"` | |
| 16 | | `\|` | Pipe output | `ls \| grep ".js"` | |
| 17 | |
| 18 | --- |
| 19 | |
| 20 | ## 2. File Operations |
| 21 | |
| 22 | ### Essential Commands |
| 23 | |
| 24 | | Task | Command | |
| 25 | |------|---------| |
| 26 | | List all | `ls -la` | |
| 27 | | Find files | `find . -name "*.js" -type f` | |
| 28 | | File content | `cat file.txt` | |
| 29 | | First N lines | `head -n 20 file.txt` | |
| 30 | | Last N lines | `tail -n 20 file.txt` | |
| 31 | | Follow log | `tail -f log.txt` | |
| 32 | | Search in files | `grep -r "pattern" --include="*.js"` | |
| 33 | | File size | `du -sh *` | |
| 34 | | Disk usage | `df -h` | |
| 35 | |
| 36 | --- |
| 37 | |
| 38 | ## 3. Process Management |
| 39 | |
| 40 | | Task | Command | |
| 41 | |------|---------| |
| 42 | | List processes | `ps aux` | |
| 43 | | Find by name | `ps aux \| grep node` | |
| 44 | | Kill by PID | `kill -9 <PID>` | |
| 45 | | Find port user | `lsof -i :3000` | |
| 46 | | Kill port | `kill -9 $(lsof -t -i :3000)` | |
| 47 | | Background | `npm run dev &` | |
| 48 | | Jobs | `jobs -l` | |
| 49 | | Bring to front | `fg %1` | |
| 50 | |
| 51 | --- |
| 52 | |
| 53 | ## 4. Text Processing |
| 54 | |
| 55 | ### Core Tools |
| 56 | |
| 57 | | Tool | Purpose | Example | |
| 58 | |------|---------|---------| |
| 59 | | `grep` | Search | `grep -rn "TODO" src/` | |
| 60 | | `sed` | Replace | `sed -i 's/old/new/g' file.txt` | |
| 61 | | `awk` | Extract columns | `awk '{print $1}' file.txt` | |
| 62 | | `cut` | Cut fields | `cut -d',' -f1 data.csv` | |
| 63 | | `sort` | Sort lines | `sort -u file.txt` | |
| 64 | | `uniq` | Unique lines | `sort file.txt \| uniq -c` | |
| 65 | | `wc` | Count | `wc -l file.txt` | |
| 66 | |
| 67 | --- |
| 68 | |
| 69 | ## 5. Environment Variables |
| 70 | |
| 71 | | Task | Command | |
| 72 | |------|---------| |
| 73 | | View all | `env` or `printenv` | |
| 74 | | View one | `echo $PATH` | |
| 75 | | Set temporary | `export VAR="value"` | |
| 76 | | Set in script | `VAR="value" command` | |
| 77 | | Add to PATH | `export PATH="$PATH:/new/path"` | |
| 78 | |
| 79 | --- |
| 80 | |
| 81 | ## 6. Network |
| 82 | |
| 83 | | Task | Command | |
| 84 | |------|---------| |
| 85 | | Download | `curl -O https://example.com/file` | |
| 86 | | API request | `curl -X GET https://api.example.com` | |
| 87 | | POST JSON | `curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' URL` | |
| 88 | | Check port | `nc -zv localhost 3000` | |
| 89 | | Network info | `ifconfig` or `ip addr` | |
| 90 | |
| 91 | --- |
| 92 | |
| 93 | ## 7. Script Template |
| 94 | |
| 95 | ```bash |
| 96 | #!/bin/bash |
| 97 | set -euo pipefail # Exit on error, undefined var, pipe fail |
| 98 | |
| 99 | # Colors (optional) |
| 100 | RED='\033[0;31m' |
| 101 | GREEN='\033[0;32m' |
| 102 | NC='\033[0m' |
| 103 | |
| 104 | # Script directory |
| 105 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 106 | |
| 107 | # Functions |
| 108 | log_info() { echo -e "${GREEN}[INFO]${NC} $1"; } |
| 109 | log_error() { echo -e "${RED}[ERROR]${NC} $1" >&2; } |
| 110 | |
| 111 | # Main |
| 112 | main() { |
| 113 | log_info "Starting..." |
| 114 | # Your logic here |
| 115 | log_info "Done!" |
| 116 | } |
| 117 | |
| 118 | main "$@" |
| 119 | ``` |
| 120 | |
| 121 | --- |
| 122 | |
| 123 | ## 8. Common Patterns |
| 124 | |
| 125 | ### Check if command exists |
| 126 | |
| 127 | ```bash |
| 128 | if command -v node &> /dev/null; then |
| 129 | echo "Node is installed" |
| 130 | fi |
| 131 | ``` |
| 132 | |
| 133 | ### Default variable value |
| 134 | |
| 135 | ```bash |
| 136 | NAME=${1:-"default_value"} |
| 137 | ``` |
| 138 | |
| 139 | ### Read file line by line |
| 140 | |
| 141 | ```bash |
| 142 | while IFS= read -r line; do |
| 143 | echo "$line" |
| 144 | done < file.txt |
| 145 | ``` |
| 146 | |
| 147 | ### Loop over files |
| 148 | |
| 149 | ```bash |
| 150 | for file in *.js; do |
| 151 | echo "Processing $file" |
| 152 | done |
| 153 | ``` |
| 154 | |
| 155 | --- |
| 156 | |
| 157 | ## 9. Differences from PowerShell |
| 158 | |
| 159 | | Task | PowerShell | Bash | |
| 160 | |------|------------|------| |
| 161 | | List files | `Get-ChildItem` | `ls -la` | |
| 162 | | Find files | `Get-ChildItem -Recurse` | `find . -type f` | |
| 163 | | Environment | `$env:VAR` | `$VAR` | |
| 164 | | String concat | `"$a$b"` | `"$a$b"` (same) | |
| 165 | | Null check | `if ($x)` | `if [ -n "$x" ]` | |
| 166 | | Pipeline | Object-based | Text-based | |
| 167 | |
| 168 | --- |
| 169 | |
| 170 | ## 10. Error Handling |
| 171 | |
| 172 | ### Set options |
| 173 | |
| 174 | ```bash |
| 175 | set -e # Exit on error |
| 176 | set -u # Exit on undefined variable |
| 177 | set -o pipefail # Exit on pipe failure |
| 178 | set -x # Debug: print commands |
| 179 | ``` |
| 180 | |
| 181 | ### Trap for cleanup |
| 182 | |
| 183 | ```bash |
| 184 | cleanup() { |
| 185 | echo "Cleaning up..." |
| 186 | rm -f /tmp/tempfile |
| 187 | } |
| 188 | trap cleanup EXIT |
| 189 | ``` |
| 190 | |
| 191 | --- |
| 192 | |
| 193 | > **Remember:** Bash is text-based. Use `&&` for success chains, `set -e` for safety, and quote your variables! |