This document provides detailed guidance on using the $ command effectively, including important information about shell quoting and special characters.
The $ command wraps any shell command with automatic logging and failure reporting:
$ echo "Hello World"
$ npm test
$ git statusWhen piping data to a command wrapped with $, there are two approaches. The preferred way is to pipe TO the $-wrapped command:
# Preferred - pipe TO the $-wrapped command
echo "hi" | $ agent
# Alternative - quote the entire pipeline
$ 'echo "hi" | agent'The first approach is simpler and requires fewer quotes. For detailed information about piping, see PIPES.md.
# Pipe text to an AI agent
echo "Explain this code" | $ agent
# Pipe file contents to a processor
cat file.txt | $ processor
# Chain commands, wrap the final one
git diff | $ reviewerThe following characters are interpreted by the shell before reaching $:
| Character | Name | Purpose |
|---|---|---|
| |
Pipe | Connects stdout to stdin |
& |
Background | Runs command in background |
; |
Semicolon | Command separator |
&& |
AND | Run next command if previous succeeds |
|| |
OR | Run next command if previous fails |
> |
Redirect | Redirect stdout to file |
< |
Input | Read input from file |
$ |
Variable | Variable expansion |
` |
Backtick | Command substitution |
*?[] |
Globs | Filename expansion |
When you need to pass special characters literally to $, use quotes:
| Quote Type | Behavior | Use When |
|---|---|---|
'single' |
Everything is literal, no expansion | Preserving pipes, special chars exactly |
"double" |
Variables expand, some escaping needed | Need variable expansion inside |
$'...' |
ANSI-C quoting, escape sequences work | Need escape sequences like \n, \t |
`...` |
Command substitution (old style) | Capturing command output (prefer $()) |
# Single quotes - everything literal
$ 'echo "hello" | wc -c'
# Double quotes - variables expand
$ "echo $HOME | wc -c" # $HOME expands first!
# Double quotes with escaping
$ "echo \$HOME | wc -c" # \$ keeps it literal$ ls -la
$ npm test
$ git status
$ echo "hello world"# Preferred: pipe TO the $-wrapped command
echo "hello" | $ grep "h"
cat file.txt | $ processor
git log | $ reviewer
# Alternative: quote the entire pipeline
$ 'cat file.txt | grep pattern'
$ 'ps aux | grep node'$ 'echo "data" > output.txt'
$ 'cat < input.txt'
$ 'npm test 2>&1 | tee test.log'$ 'npm install && npm test'
$ 'command1 || command2'
$ 'test -f file && cat file'$ 'npm start &'
$ 'sleep 10 && echo "done" &'# Variable expands BEFORE $ sees it (usually what you want)
$ echo "$HOME"
# Variable is passed literally to $ (rare use case)
$ 'echo $HOME'Symptom: Running $ cmd1 | cmd2 and cmd2 receives unexpected output.
Solutions:
- Pipe TO $:
cmd1 | $ cmd2(preferred) - Quote:
$ 'cmd1 | cmd2'
Symptom: $ 'echo $HOME' literally prints "$HOME"
Solution: Use double quotes: $ "echo $HOME" or pipe: echo "$HOME" | $ processor
Symptom: Commands with *, ?, [, ] behave unexpectedly
Solution: Quote the command or escape the characters:
$ 'ls *.txt' # Quotes prevent glob expansion by outer shell
$ ls \*.txt # Escape prevents expansionSymptom: $ 'echo "hello 'world'"' causes syntax errors
Solution: Use different quote styles or escape:
$ "echo \"hello 'world'\"" # Use double quotes with escaping
$ $'echo "hello \'world\'"' # Use ANSI-C quoting- PIPES.md - Detailed guide on piping with
$ - Bash Reference Manual - Quoting
- Bash Reference Manual - Pipelines
- POSIX Shell Command Language
- Case Study: Issue #28 - Shell Quoting Analysis