Automated summary
A Bash and Linux/macOS pattern reference covering command chaining operators, file operations, process management, text processing, and a robust script template.
Strengths
- Copy-ready command tables for common terminal tasks (find, grep, sed, awk, lsof)
- Script template with set -euo pipefail, colored logging, and script-directory resolution
- Dedicated error-handling section (set -e/-u/-o pipefail, cleanup trap) plus a PowerShell comparison
- Practical coverage of environment variables, curl-based networking, and background process and job control
Limitations
- Cheatsheet format: little explanation of why commands work or how edge cases behave
- Several commands are macOS/Linux-specific (sed -i, lsof, ifconfig) and may differ on other systems
- The cleanup trap example is truncated in the provided document
- No coverage of alternative shells (zsh, fish) or script security practices
Use it when you need to run or write shell commands and scripts quickly on a macOS or Linux machine.
Avoid it for Windows/PowerShell scripting or for advanced shell programming that needs a full guide rather than a quick reference.
Security analysis
SafeThe document is purely a reference cheat sheet for Bash/Linux commands. There is no obfuscation, no curl|sh installation, no exfiltration of secrets, no disabling of safety mechanisms, and no instruction to execute destructive actions automatically. A few destructive-looking examples (kill -9, rm -f) are shown in their normal educational context and are scoped to specific PIDs/temp files.
- •Includes examples of forceful process termination (`kill -9 <PID>`, `kill -9 $(lsof -t -i :3000)`) and file deletion (`rm -f /tmp/tempfile` in a trap) that could be copy-pasted carelessly, but they are standard documented patterns, not instructions to run anything.
Examples
Find every .js file under src/ that contains a TODO comment, then replace all occurrences of oldFn with newFn in place and show me the diff of what changed.Write a bash script that uses set -euo pipefail, colored log_info/log_error helpers, and a trap that deletes a temp file on exit.Port 3000 is already in use — show me which process is holding it with lsof and kill it cleanly.name: bash-linux description: Bash/Linux terminal patterns. Critical commands, piping, error handling, scripting. Use when working on macOS or Linux systems. allowed-tools: Read, Write, Edit, Glob, Grep, Bash
Bash Linux Patterns
Essential patterns for Bash on Linux/macOS.
1. Operator Syntax
Chaining Commands
| Operator | Meaning | Example |
|----------|---------|---------|
| ; | Run sequentially | cmd1; cmd2 |
| && | Run if previous succeeded | npm install && npm run dev |
| \|\| | Run if previous failed | npm test \|\| echo "Tests failed" |
| \| | Pipe output | ls \| grep ".js" |
2. File Operations
Essential Commands
| Task | Command |
|------|---------|
| List all | ls -la |
| Find files | find . -name "*.js" -type f |
| File content | cat file.txt |
| First N lines | head -n 20 file.txt |
| Last N lines | tail -n 20 file.txt |
| Follow log | tail -f log.txt |
| Search in files | grep -r "pattern" --include="*.js" |
| File size | du -sh * |
| Disk usage | df -h |
3. Process Management
| Task | Command |
|------|---------|
| List processes | ps aux |
| Find by name | ps aux \| grep node |
| Kill by PID | kill -9 <PID> |
| Find port user | lsof -i :3000 |
| Kill port | kill -9 $(lsof -t -i :3000) |
| Background | npm run dev & |
| Jobs | jobs -l |
| Bring to front | fg %1 |
4. Text Processing
Core Tools
| Tool | Purpose | Example |
|------|---------|---------|
| grep | Search | grep -rn "TODO" src/ |
| sed | Replace | sed -i 's/old/new/g' file.txt |
| awk | Extract columns | awk '{print $1}' file.txt |
| cut | Cut fields | cut -d',' -f1 data.csv |
| sort | Sort lines | sort -u file.txt |
| uniq | Unique lines | sort file.txt \| uniq -c |
| wc | Count | wc -l file.txt |
5. Environment Variables
| Task | Command |
|------|---------|
| View all | env or printenv |
| View one | echo $PATH |
| Set temporary | export VAR="value" |
| Set in script | VAR="value" command |
| Add to PATH | export PATH="$PATH:/new/path" |
6. Network
| Task | Command |
|------|---------|
| Download | curl -O https://example.com/file |
| API request | curl -X GET https://api.example.com |
| POST JSON | curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' URL |
| Check port | nc -zv localhost 3000 |
| Network info | ifconfig or ip addr |
7. Script Template
#!/bin/bash
set -euo pipefail # Exit on error, undefined var, pipe fail
# Colors (optional)
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Functions
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1" >&2; }
# Main
main() {
log_info "Starting..."
# Your logic here
log_info "Done!"
}
main "$@"
8. Common Patterns
Check if command exists
if command -v node &> /dev/null; then
echo "Node is installed"
fi
Default variable value
NAME=${1:-"default_value"}
Read file line by line
while IFS= read -r line; do
echo "$line"
done < file.txt
Loop over files
for file in *.js; do
echo "Processing $file"
done
9. Differences from PowerShell
| Task | PowerShell | Bash |
|------|------------|------|
| List files | Get-ChildItem | ls -la |
| Find files | Get-ChildItem -Recurse | find . -type f |
| Environment | $env:VAR | $VAR |
| String concat | "$a$b" | "$a$b" (same) |
| Null check | if ($x) | if [ -n "$x" ] |
| Pipeline | Object-based | Text-based |
10. Error Handling
Set options
set -e # Exit on error
set -u # Exit on undefined variable
set -o pipefail # Exit on pipe failure
set -x # Debug: print commands
Trap for cleanup
cleanup() {
echo "Cleaning up..."
rm -f /tmp/tempfile
}
trap cleanup EXIT
Remember: Bash is text-based. Use
&&for success chains,set -efor safety, and quote your variables!
From the file to a first result
The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.
View the source and supporting filesThis download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.
1. Download the file
Download SKILL.mdFrom the root of a test project, save the file as .skill-downloads/bash-linux-terminal-patterns-msh3usp1/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.
Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.
mkdir -p '.skill-downloads/bash-linux-terminal-patterns-msh3usp1' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/bash-linux-terminal-patterns-msh3usp1/download' --output '.skill-downloads/bash-linux-terminal-patterns-msh3usp1/SKILL.md.part' && mv '.skill-downloads/bash-linux-terminal-patterns-msh3usp1/SKILL.md.part' '.skill-downloads/bash-linux-terminal-patterns-msh3usp1/SKILL.md'2. Check prerequisites
Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.
Tools declared by the author (check availability in your agent): Read, Write, Edit, Glob, Grep, Bash
No recognized prerequisites section in this file. This does not mean it is self-contained: libraries, tools and access requirements may be mentioned elsewhere in the content or source repository.
The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.
3. Load the skill
After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.
Claude Code
test -f '.skill-downloads/bash-linux-terminal-patterns-msh3usp1/SKILL.md' && test -s '.skill-downloads/bash-linux-terminal-patterns-msh3usp1/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/bash-linux' && cp -R '.skill-downloads/bash-linux-terminal-patterns-msh3usp1' '.claude/skills/bash-linux'Cursor
test -f '.skill-downloads/bash-linux-terminal-patterns-msh3usp1/SKILL.md' && test -s '.skill-downloads/bash-linux-terminal-patterns-msh3usp1/SKILL.md' && mkdir -p '.cursor/skills' && test ! -e '.cursor/skills/bash-linux' && cp -R '.skill-downloads/bash-linux-terminal-patterns-msh3usp1' '.cursor/skills/bash-linux'OpenSkills
OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.
Official OpenSkills installationopenskills install './.skill-downloads/bash-linux-terminal-patterns-msh3usp1'openskills read 'bash-linux-terminal-patterns-msh3usp1'Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.
For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.
openskills syncManual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.
4. Try a first task
Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:
Read .skill-downloads/bash-linux-terminal-patterns-msh3usp1/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.
Configure a Chat Channel with SecretRefs
DevOps
Set up chat channels using non-interactive commands, with secrets only as SecretRefs. Covers gather, mutate, repair, and prove steps for reliable delivery.
Investigate ClickHouse CI Failures
DevOps
Investigate a ClickHouse CI failure end-to-end from a PR or S3 report URL. Fetches failed tests, classifies flaky vs real regression, and checks for existing issues/fixes.
Ansible PR Review
DevOps
Reviews an Ansible PR following the project's standardized process, including changelog checks, test coverage validation, and feedback tracking.