Bash to Script File Converter Skill
Purpose: Convert bash commands with parameter expansion or complex syntax to script file format to avoid parse errors in the Bash tool.
When to Use:
- •When bash command fails with parse error:
(eval):1: parse error near '(' - •Before executing commands with
${VAR##pattern}or${VAR%%pattern}syntax - •When using complex jq pipelines with command substitution
- •When bash command has multiple nested parameter expansions
- •Proactively for any bash command pattern known to cause issues
Problem
The Bash tool has limitations with certain bash syntax patterns, particularly:
- •Parameter expansion with pattern removal:
${VAR##prefix},${VAR%%suffix} - •Complex command substitution:
$(jq ... | tail -1) - •Nested parameter expansions in single command
- •Heredoc with complex variable interpolation
These patterns can fail with cryptic parse errors when executed directly via the Bash tool.
Solution
Convert the problematic command to a script file approach:
- •Write command to temporary script file using heredoc
- •Execute the script file with
bash /tmp/script.sh - •Script file handles all parameter expansion correctly
Usage
Input Required:
- •The bash command that needs conversion (as a string)
- •Optional: Script filename (defaults to
/tmp/converted-script.sh)
Output:
- •Converted command using script file approach
- •Ready to execute via Bash tool
Conversion Instructions
When you encounter a bash command that needs conversion, follow this pattern:
# Original problematic command (example):
SESSION_ID="abc-123"
AGENT_ID=$(jq -r 'select(.toolUseResult.agentId)' /path/${SESSION_ID}.jsonl | tail -1)
echo "$AGENT_ID" > output.txt
# Convert to script file approach:
cat > /tmp/converted-script.sh << 'EOF'
#!/bin/bash
SESSION_ID="abc-123"
AGENT_ID=$(jq -r 'select(.toolUseResult.agentId)' /path/${SESSION_ID}.jsonl | tail -1)
echo "$AGENT_ID" > output.txt
EOF
bash /tmp/converted-script.sh
Automatic Conversion
To invoke this skill for automatic conversion:
Skill: bash-to-script Command: <paste the problematic bash command here>
The skill will output the converted command ready to execute.
Pattern Recognition
Commands that need conversion (automatic detection patterns):
- •
Parameter Expansion with Pattern Removal:
bash${VAR##prefix*} # Remove longest matching prefix ${VAR%%*suffix} # Remove longest matching suffix ${VAR#prefix} # Remove shortest matching prefix ${VAR%suffix} # Remove shortest matching suffix - •
Complex Command Substitution:
bashVAR=$(complex command | pipeline | tail -1) # Especially with jq, grep, awk, sed pipelines
- •
Multiple Parameter Expansions:
bashecho "${VAR1}" > /path/to/${VAR2}/file-${VAR3}.txt # Multiple expansions in single command - •
Nested Quotes with Variables:
bashjq -r 'select(.field == "'"$VAR"'")' file.json # Nested single/double quotes with variable interpolation
Conversion Template
Standard template for conversion:
cat > /tmp/<descriptive-name>.sh << 'EOF' #!/bin/bash set -euo pipefail # Your original command(s) here # All parameter expansion works correctly in script file EOF bash /tmp/<descriptive-name>.sh
With error handling:
cat > /tmp/<descriptive-name>.sh << 'EOF' #!/bin/bash set -euo pipefail trap 'echo "ERROR in script at line $LINENO: Command failed: $BASH_COMMAND" >&2; exit 1' ERR # Your original command(s) here EOF bash /tmp/<descriptive-name>.sh
Examples
Example 1: Parameter Expansion with Pattern Removal
❌ Fails with parse error:
BACKUP_FILE=$(ls /workspace/CLAUDE.md.backup-* 2>/dev/null | tail -1)
VALIDATION_ID="${BACKUP_FILE##*.backup-}"
echo "$VALIDATION_ID"
✅ Correct (converted):
cat > /tmp/extract-validation-id.sh << 'EOF'
#!/bin/bash
BACKUP_FILE=$(ls /workspace/CLAUDE.md.backup-* 2>/dev/null | tail -1)
VALIDATION_ID="${BACKUP_FILE##*.backup-}"
echo "$VALIDATION_ID"
EOF
bash /tmp/extract-validation-id.sh
Example 2: Complex jq Pipeline
❌ Fails with parse error:
SESSION_ID="7f08a8f6-b90c-47ac-bc1b-31943c7b995a"
AGENT_ID=$(jq -r 'select(.toolUseResult.agentId) | .toolUseResult.agentId' /home/node/.config/projects/-workspace/${SESSION_ID}.jsonl 2>/dev/null | tail -1)
echo "$AGENT_ID" > /tmp/agent-id.txt
✅ Correct (converted):
cat > /tmp/extract-agent-id.sh << 'EOF'
#!/bin/bash
SESSION_ID="7f08a8f6-b90c-47ac-bc1b-31943c7b995a"
AGENT_ID=$(jq -r 'select(.toolUseResult.agentId) | .toolUseResult.agentId' /home/node/.config/projects/-workspace/${SESSION_ID}.jsonl 2>/dev/null | tail -1)
echo "$AGENT_ID" > /tmp/agent-id.txt
EOF
bash /tmp/extract-agent-id.sh
Example 3: Multiple Parameter Expansions
❌ Fails with parse error:
PROJECT="my-project"
SUBDIR="src/main"
TARGET="/workspace/${PROJECT}/${SUBDIR}/code"
mkdir -p "$TARGET"
cd "$TARGET" && git status
✅ Correct (converted):
cat > /tmp/setup-dir.sh << 'EOF'
#!/bin/bash
PROJECT="my-project"
SUBDIR="src/main"
TARGET="/workspace/${PROJECT}/${SUBDIR}/code"
mkdir -p "$TARGET"
cd "$TARGET" && git status
EOF
bash /tmp/setup-dir.sh
When NOT to Use Script File
Some bash commands work fine directly in Bash tool:
✅ Simple commands (no conversion needed):
# Simple variable substitution NAME="test" echo "Hello $NAME" # Basic command substitution FILES=$(ls *.txt) echo "$FILES" # Simple paths cd /workspace/main && pwd # Git commands git status git log --oneline -5
Error Messages to Watch For
If you see these errors, use this skill:
- •
(eval):1: parse error near '(' - •
(eval):1: command not found: (eval) - •
syntax error near unexpected token - •
bad substitution
Integration with Existing Workflows
optimize-doc command: Already documented with script file requirement for validation ID extraction
Other skills/commands: Use this skill proactively when commands match the problematic patterns above
Best Practices
- •Name script files descriptively: Use
/tmp/<descriptive-name>.shinstead of generic names - •Add error handling: Include
set -euo pipefailand trap for better debugging - •Clean up: Remove script files after use if they're not needed for debugging
- •Document: Add comment in script explaining what it does
- •Test: Verify script works before using in production workflows
Quick Reference Card
Need script file if command has:
- •✓
${VAR##pattern}or${VAR%%pattern} - •✓ Complex
$(...)with pipes and filters - •✓ Multiple
${VAR}expansions in one line - •✓ Nested quotes with variables
- •✓ Parse error mentioning
(eval):1
Don't need script file if:
- •✗ Simple
$VARor${VAR} - •✗ Basic
$(command) - •✗ No error reported
- •✗ Simple single-line commands
Output Format
When skill is invoked, it outputs:
# Converted command (ready to execute): cat > /tmp/<generated-name>.sh << 'EOF' #!/bin/bash set -euo pipefail <your original command converted> EOF bash /tmp/<generated-name>.sh
Simply copy and paste this into the Bash tool to execute.
Related Documentation
- •CLAUDE.md § Tool Usage Best Practices § Bash Tool - Path Handling § Parameter Expansion Limitation
- •optimize-doc command (documents script file requirement)
Maintenance
This skill should be updated when:
- •New problematic bash patterns are discovered
- •Bash tool behavior changes
- •Better conversion approaches are found
- •Additional error patterns are identified