Shell Expert
Expert knowledge for shell scripting, command-line tools, and automation with focus on robust, portable, and efficient solutions.
Core Expertise
Command-Line Tool Mastery
- Expert knowledge of modern CLI tools (jq, yq, fd, rg, etc.)
- JSON/YAML processing and transformation
- File searching and text manipulation
- System automation and orchestration
Shell Scripting Excellence
- POSIX-compliant shell scripting for maximum portability
- Bash-specific features and best practices
- Error handling and defensive programming
- Cross-platform compatibility (Linux, macOS, BSD)
Automation & Integration
- CI/CD pipeline scripting
- System administration automation
- Tool integration and workflow automation
- Performance optimization for shell operations
Key Capabilities
JSON/YAML Processing
- jq: Complex JSON queries, transformations, and filtering
- yq: YAML manipulation, in-place editing, format conversion
- jd: JSON diffing and patching for configuration management
- Data pipeline construction: Chaining tools for complex transformations
File Operations & Search
- fd: Fast, user-friendly file finding with intuitive syntax
- rg (ripgrep): Lightning-fast recursive grep with gitignore support
- lsd: Modern ls replacement with visual enhancements
- find/grep alternatives: When and how to use modern replacements
Shell Script Development
- Error Handling: Proper trap usage, exit codes, error propagation
- Input Validation: Argument parsing, option handling, user input sanitization
- Debugging: Set options (-x, -e, -u, -o pipefail), debug output strategies
- Performance: Process substitution, parallel execution, efficient loops
Cross-Platform Scripting
- Platform Detection: OS-specific behavior handling
- Path Management: Portable path construction and manipulation
- Tool Availability: Checking for and handling missing dependencies
- Compatibility Layers: Writing scripts that work everywhere
Automation Patterns
- Idempotent Operations: Scripts that can run multiple times safely
- Atomic Operations: Ensuring all-or-nothing execution
- Progress Reporting: User-friendly output and status updates
- Logging & Monitoring: Structured logging for automated systems
Essential Commands
jq - JSON Processing
jq . data.json # Pretty-print
jq -r '.key.subkey' data.json # Extract value
jq '.items[] | select(.status == "active")' # Filter
yq - YAML Processing
yq '.services.web.image' docker-compose.yml # Read value
yq -i '.version = "2.1.0"' config.yml # Update in-place
yq -o json config.yml # Convert to JSON
fd - Fast File Finding
fd 'pattern' # Find by pattern
fd -e md # Find by extension
fd -e sh -x shellcheck {} # Find and execute
rg - Recursive Grep
rg 'DATABASE_URL' # Basic search
rg 'TODO' -t python # Search specific file types
rg -C 3 'error' # Search with context
Best Practices
Script Development Workflow
- Requirements Analysis: Understand automation need and target platforms
- Tool Selection: Choose appropriate tools for the task
- Prototype Development: Create initial script with core functionality
- Error Handling: Add robust error handling and edge case management
- Cross-Platform Testing: Verify script works on all target systems
- Performance Optimization: Profile and optimize for efficiency
- Documentation: Add clear usage instructions and inline comments
Critical Guidelines
- Always use shellcheck for linting
- Set strict mode:
set -euo pipefail
- Quote all variables:
"${var}"
- Use functions for reusable code
- Implement proper cleanup with trap
- Provide helpful error messages
- Include --help and --version options
- Use meaningful variable names
- Comment complex logic
- Test with different shells when targeting POSIX
Common Patterns
Robust Script Template
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
trap 'echo "Error on line $LINENO"' ERR
trap cleanup EXIT
cleanup() {
rm -f "$TEMP_FILE" 2>/dev/null || true
}
main() {
parse_args "$@"
validate_environment
execute_task
}
main "$@"
Cross-Platform Detection
detect_os() {
case "$OSTYPE" in
linux*) OS="linux" ;;
darwin*) OS="macos" ;;
msys*) OS="windows" ;;
*) OS="unknown" ;;
esac
}
For detailed command-line tools reference, advanced automation examples, and troubleshooting guidance, see REFERENCE.md.
1---
2created: 2025-12-16
3modified: 2025-12-16
4reviewed: 2026-02-08
5name: shell-expert
6description: |
7 Shell scripting expertise, command-line tools, automation, and cross-platform
8 scripting best practices. Covers shell script development, CLI tool usage,
9 and system automation with bash, zsh, and POSIX shell.
10 Use when user mentions shell scripts, bash, zsh, CLI commands, pipes, command-line
11 automation, or writing portable shell code.
12user-invocable: false
13allowed-tools: Bash, BashOutput, KillShell, Grep, Glob, Read, Write, Edit, TodoWrite
14---
15
16# Shell Expert
17
18Expert knowledge for shell scripting, command-line tools, and automation with focus on robust, portable, and efficient solutions.
19
20## Core Expertise
21
22**Command-Line Tool Mastery**
23- Expert knowledge of modern CLI tools (jq, yq, fd, rg, etc.)
24- JSON/YAML processing and transformation
25- File searching and text manipulation
26- System automation and orchestration
27
28**Shell Scripting Excellence**
29- POSIX-compliant shell scripting for maximum portability
30- Bash-specific features and best practices
31- Error handling and defensive programming
32- Cross-platform compatibility (Linux, macOS, BSD)
33
34**Automation & Integration**
35- CI/CD pipeline scripting
36- System administration automation
37- Tool integration and workflow automation
38- Performance optimization for shell operations
39
40## Key Capabilities
41
42**JSON/YAML Processing**
43- **jq**: Complex JSON queries, transformations, and filtering
44- **yq**: YAML manipulation, in-place editing, format conversion
45- **jd**: JSON diffing and patching for configuration management
46- **Data pipeline construction**: Chaining tools for complex transformations
47
48**File Operations & Search**
49- **fd**: Fast, user-friendly file finding with intuitive syntax
50- **rg (ripgrep)**: Lightning-fast recursive grep with gitignore support
51- **lsd**: Modern ls replacement with visual enhancements
52- **find/grep alternatives**: When and how to use modern replacements
53
54**Shell Script Development**
55- **Error Handling**: Proper trap usage, exit codes, error propagation
56- **Input Validation**: Argument parsing, option handling, user input sanitization
57- **Debugging**: Set options (-x, -e, -u, -o pipefail), debug output strategies
58- **Performance**: Process substitution, parallel execution, efficient loops
59
60**Cross-Platform Scripting**
61- **Platform Detection**: OS-specific behavior handling
62- **Path Management**: Portable path construction and manipulation
63- **Tool Availability**: Checking for and handling missing dependencies
64- **Compatibility Layers**: Writing scripts that work everywhere
65
66**Automation Patterns**
67- **Idempotent Operations**: Scripts that can run multiple times safely
68- **Atomic Operations**: Ensuring all-or-nothing execution
69- **Progress Reporting**: User-friendly output and status updates
70- **Logging & Monitoring**: Structured logging for automated systems
71
72## Essential Commands
73
74**jq - JSON Processing**
75```bash
76jq . data.json # Pretty-print
77jq -r '.key.subkey' data.json # Extract value
78jq '.items[] | select(.status == "active")' # Filter
79```
80
81**yq - YAML Processing**
82```bash
83yq '.services.web.image' docker-compose.yml # Read value
84yq -i '.version = "2.1.0"' config.yml # Update in-place
85yq -o json config.yml # Convert to JSON
86```
87
88**fd - Fast File Finding**
89```bash
90fd 'pattern' # Find by pattern
91fd -e md # Find by extension
92fd -e sh -x shellcheck {} # Find and execute
93```
94
95**rg - Recursive Grep**
96```bash
97rg 'DATABASE_URL' # Basic search
98rg 'TODO' -t python # Search specific file types
99rg -C 3 'error' # Search with context
100```
101
102## Best Practices
103
104**Script Development Workflow**
1051. **Requirements Analysis**: Understand automation need and target platforms
1062. **Tool Selection**: Choose appropriate tools for the task
1073. **Prototype Development**: Create initial script with core functionality
1084. **Error Handling**: Add robust error handling and edge case management
1095. **Cross-Platform Testing**: Verify script works on all target systems
1106. **Performance Optimization**: Profile and optimize for efficiency
1117. **Documentation**: Add clear usage instructions and inline comments
112
113**Critical Guidelines**
114- Always use shellcheck for linting
115- Set strict mode: `set -euo pipefail`
116- Quote all variables: `"${var}"`
117- Use functions for reusable code
118- Implement proper cleanup with trap
119- Provide helpful error messages
120- Include --help and --version options
121- Use meaningful variable names
122- Comment complex logic
123- Test with different shells when targeting POSIX
124
125## Common Patterns
126
127**Robust Script Template**
128```bash
129#!/usr/bin/env bash
130set -euo pipefail
131IFS=$'\n\t'
132
133trap 'echo "Error on line $LINENO"' ERR
134trap cleanup EXIT
135
136cleanup() {
137 rm -f "$TEMP_FILE" 2>/dev/null || true
138}
139
140main() {
141 parse_args "$@"
142 validate_environment
143 execute_task
144}
145
146main "$@"
147```
148
149**Cross-Platform Detection**
150```bash
151detect_os() {
152 case "$OSTYPE" in
153 linux*) OS="linux" ;;
154 darwin*) OS="macos" ;;
155 msys*) OS="windows" ;;
156 *) OS="unknown" ;;
157 esac
158}
159```
160
161For detailed command-line tools reference, advanced automation examples, and troubleshooting guidance, see REFERENCE.md.