|
| 1 | +# Piping with start-command (`$`) |
| 2 | + |
| 3 | +This document explains how to use pipes with the `$` command effectively. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Quick Summary](#quick-summary) |
| 8 | +- [The Preferred Way: Pipe TO `$`](#the-preferred-way-pipe-to-) |
| 9 | +- [Alternative: Quoting](#alternative-quoting) |
| 10 | +- [Why This Matters](#why-this-matters) |
| 11 | +- [Examples](#examples) |
| 12 | +- [Troubleshooting](#troubleshooting) |
| 13 | + |
| 14 | +## Quick Summary |
| 15 | + |
| 16 | +When piping data to a command wrapped with `$`, **put `$` on the receiving command**: |
| 17 | + |
| 18 | +```bash |
| 19 | +# Preferred - pipe TO the $-wrapped command |
| 20 | +echo "hi" | $ agent |
| 21 | + |
| 22 | +# Alternative - quote the entire pipeline (more verbose) |
| 23 | +$ 'echo "hi" | agent' |
| 24 | +``` |
| 25 | + |
| 26 | +Both approaches work, but piping TO `$` is simpler and requires fewer quotes. |
| 27 | + |
| 28 | +## The Preferred Way: Pipe TO `$` |
| 29 | + |
| 30 | +The cleanest way to use pipes with `$` is to place `$` on the command that **receives** the piped input: |
| 31 | + |
| 32 | +```bash |
| 33 | +# Data flows: echo "hi" -> agent (wrapped with $) |
| 34 | +echo "hi" | $ agent |
| 35 | +``` |
| 36 | + |
| 37 | +This works because: |
| 38 | + |
| 39 | +1. The shell creates a pipeline: `echo "hi"` piped to `$ agent` |
| 40 | +2. `$ agent` receives "hi" on stdin |
| 41 | +3. The `$` command wraps `agent`, which processes the input |
| 42 | + |
| 43 | +### Real-World Examples |
| 44 | + |
| 45 | +```bash |
| 46 | +# Pipe text to an AI agent |
| 47 | +echo "Explain this code" | $ agent |
| 48 | + |
| 49 | +# Pipe file contents to a processor |
| 50 | +cat file.txt | $ processor |
| 51 | + |
| 52 | +# Pipe command output to an analyzer |
| 53 | +ls -la | $ analyzer |
| 54 | + |
| 55 | +# Chain multiple commands, wrap the final one |
| 56 | +cat data.json | jq '.items[]' | $ handler |
| 57 | +``` |
| 58 | + |
| 59 | +### When to Use This Approach |
| 60 | + |
| 61 | +Use `command | $ target` when: |
| 62 | + |
| 63 | +- You want to pipe data INTO a command that `$` wraps |
| 64 | +- You want the `$` logging and monitoring for the receiving command |
| 65 | +- You prefer minimal quoting |
| 66 | + |
| 67 | +## Alternative: Quoting |
| 68 | + |
| 69 | +You can also wrap the entire pipeline in quotes: |
| 70 | + |
| 71 | +```bash |
| 72 | +# Single quotes preserve the pipe literally |
| 73 | +$ 'echo "hi" | agent' |
| 74 | + |
| 75 | +# The $ command receives the whole pipeline as one argument |
| 76 | +$ 'cat file.txt | grep pattern | wc -l' |
| 77 | +``` |
| 78 | + |
| 79 | +### When to Use Quoting |
| 80 | + |
| 81 | +Use quotes when: |
| 82 | + |
| 83 | +- You want `$` to wrap the ENTIRE pipeline (logging all commands) |
| 84 | +- You need the pipeline to run as a single tracked unit |
| 85 | +- You want a single log file for the whole operation |
| 86 | + |
| 87 | +### Quote Types |
| 88 | + |
| 89 | +| Quote Type | Behavior | Example | |
| 90 | +| ---------- | ------------------ | ---------------------- | |
| 91 | +| `'single'` | Everything literal | `$ 'echo $HOME \| wc'` | |
| 92 | +| `"double"` | Variables expand | `$ "echo $HOME \| wc"` | |
| 93 | + |
| 94 | +## Why This Matters |
| 95 | + |
| 96 | +### Shell Parsing Order |
| 97 | + |
| 98 | +When you type a command, the shell parses it **before** any program runs: |
| 99 | + |
| 100 | +``` |
| 101 | +Without quotes or positioning: |
| 102 | + $ echo "hi" | agent |
| 103 | + └───┬────┘ └──┬──┘ |
| 104 | + │ │ |
| 105 | + Command 1 Command 2 |
| 106 | + ($ echo "hi") (agent) |
| 107 | +
|
| 108 | + Result: agent receives $ output, not "hi" |
| 109 | +``` |
| 110 | + |
| 111 | +The pipe `|` is a shell operator, so the shell splits the command at that point. |
| 112 | + |
| 113 | +### Solution Comparison |
| 114 | + |
| 115 | +``` |
| 116 | +Preferred - Pipe TO $: |
| 117 | + echo "hi" | $ agent |
| 118 | + └───┬────┘ └──┬──┘ |
| 119 | + │ │ |
| 120 | + Command 1 Command 2 |
| 121 | + (echo "hi") ($ agent) |
| 122 | +
|
| 123 | + Result: $ agent receives "hi" - correct! |
| 124 | +
|
| 125 | +Alternative - Quoting: |
| 126 | + $ 'echo "hi" | agent' |
| 127 | + └─────────┬──────────┘ |
| 128 | + │ |
| 129 | + Single command |
| 130 | + (pipeline runs inside $) |
| 131 | +
|
| 132 | + Result: agent receives "hi" - correct! |
| 133 | +``` |
| 134 | + |
| 135 | +## Examples |
| 136 | + |
| 137 | +### Basic Piping |
| 138 | + |
| 139 | +```bash |
| 140 | +# Pipe text to a command |
| 141 | +echo "hello world" | $ processor |
| 142 | + |
| 143 | +# Pipe file contents |
| 144 | +cat config.json | $ validator |
| 145 | + |
| 146 | +# Pipe command output |
| 147 | +git diff | $ reviewer |
| 148 | +``` |
| 149 | + |
| 150 | +### Multiple Pipes |
| 151 | + |
| 152 | +```bash |
| 153 | +# $ wraps only the final command |
| 154 | +cat file.txt | grep "error" | $ reporter |
| 155 | + |
| 156 | +# $ wraps the entire pipeline (quoted) |
| 157 | +$ 'cat file.txt | grep "error" | wc -l' |
| 158 | +``` |
| 159 | + |
| 160 | +### With Variables |
| 161 | + |
| 162 | +```bash |
| 163 | +# Variable expands before piping (shell handles it) |
| 164 | +echo "$HOME" | $ agent |
| 165 | + |
| 166 | +# Variable preserved literally (single quotes) |
| 167 | +$ 'echo $HOME | wc -c' |
| 168 | +``` |
| 169 | + |
| 170 | +### Complex Commands |
| 171 | + |
| 172 | +```bash |
| 173 | +# Process JSON and pipe to handler |
| 174 | +curl -s https://api.example.com/data | jq '.items' | $ handler |
| 175 | + |
| 176 | +# Pipe to a command with arguments |
| 177 | +echo "analyze this" | $ agent --verbose --format json |
| 178 | +``` |
| 179 | + |
| 180 | +## Troubleshooting |
| 181 | + |
| 182 | +### Problem: Output goes to wrong place |
| 183 | + |
| 184 | +**Symptom:** Running `$ cmd1 | cmd2` and cmd2 receives unexpected output. |
| 185 | + |
| 186 | +**Cause:** Shell parses `|` before `$` runs, so cmd2 gets `$` output. |
| 187 | + |
| 188 | +**Solutions:** |
| 189 | + |
| 190 | +1. Pipe TO $: `cmd1 | $ cmd2` |
| 191 | +2. Quote: `$ 'cmd1 | cmd2'` |
| 192 | + |
| 193 | +### Problem: Command not receiving stdin |
| 194 | + |
| 195 | +**Symptom:** `echo "data" | $ cmd` but cmd doesn't see the data. |
| 196 | + |
| 197 | +**Check:** Does `cmd` read from stdin? Not all commands do. |
| 198 | + |
| 199 | +**Test:** Try `echo "data" | cmd` without `$` first. |
| 200 | + |
| 201 | +### Problem: Quotes inside quotes |
| 202 | + |
| 203 | +**Symptom:** `$ 'echo "hello 'world'"'` causes errors. |
| 204 | + |
| 205 | +**Solutions:** |
| 206 | + |
| 207 | +```bash |
| 208 | +# Use double quotes with escaping |
| 209 | +$ "echo \"hello 'world'\"" |
| 210 | + |
| 211 | +# Or mix quote styles |
| 212 | +echo "hello 'world'" | $ processor |
| 213 | +``` |
| 214 | + |
| 215 | +### Problem: Variables not expanding |
| 216 | + |
| 217 | +**Symptom:** `$ 'echo $HOME'` prints literal "$HOME". |
| 218 | + |
| 219 | +**Cause:** Single quotes prevent expansion. |
| 220 | + |
| 221 | +**Solutions:** |
| 222 | + |
| 223 | +```bash |
| 224 | +# Use double quotes (escape the pipe) |
| 225 | +$ "echo $HOME | wc -c" |
| 226 | + |
| 227 | +# Or pipe TO $ (variable expands in source command) |
| 228 | +echo "$HOME" | $ wc -c |
| 229 | +``` |
| 230 | + |
| 231 | +## Summary |
| 232 | + |
| 233 | +| Approach | Syntax | Best For | |
| 234 | +| --------- | ------------------ | --------------------------- | |
| 235 | +| Pipe TO $ | `cmd1 \| $ cmd2` | Simple piping, less quoting | |
| 236 | +| Quoted | `$ 'cmd1 \| cmd2'` | Logging entire pipeline | |
| 237 | + |
| 238 | +The preferred approach is **piping TO `$`** - it's simpler and avoids quote complexity. |
| 239 | + |
| 240 | +## See Also |
| 241 | + |
| 242 | +- [USAGE.md](USAGE.md) - General usage guide |
| 243 | +- [Case Study: Issue #28](case-studies/issue-28/README.md) - Detailed analysis |
0 commit comments