Recently Written · git

tmpl

Insanely simple templates

git clone https://github.com/equwal/tmpl

Log | Files | Refs


sh/redirection (1433 bytes)

1 # Cursed shell redirection
2 
3 <++> 2>&1 # stderr goes wherever stdout is going
4 
5 # Order matters
6 <++> >/dev/null 2>&1 # silence
7 nohup <++> >/dev/null 2>&1 # keeps going even if you hang up (close) the terminal. anti-trap.
8 nohup <++> >/dev/null 2>&1 & # and async
9 <++> >output.log 2>error.log
10 <++> >output.log 2>&1 | tee combined.log
11 <++> 2>&1 | tee combined.log >/dev/null # only stderr
12 
13 # File descriptor swapping
14 <++> 3>&1 1>&2 2>&3 3>&- # swap stdout and stderr using fd3
15 { <++>; } 2>&1 # group commands
16 
17 # POSIX-compliant error filtering (replaces process substitution)
18 <++> 2>&1 >/dev/null | grep ERROR >&2  # filter stderr through grep, discard stdout
19 <++> 2>/tmp/stderr.$$ >/dev/null; grep ERROR < /tmp/stderr.$$ >&2; rm -f /tmp/stderr.$$  # alternative using temp file
20 
21 # Tests/examples:
22 #
23 # ls /nonexistent 2>&1
24 # ls /nonexistent >/dev/null 2>&1
25 # nohup echo "test message" >/dev/null 2>&1
26 # nohup sleep 1 >/dev/null 2>&1 & echo "Background job started"
27 # { echo "stdout message"; echo "stderr message" >&2; } >output.log 2>error.log; cat output.log; cat error.log; rm output.log error.log
28 # { echo "stdout"; echo "stderr" >&2; } >output.log 2>&1 | tee combined.log; cat combined.log; rm output.log combined.log
29 # { echo "stdout"; echo "stderr" >&2; } 2>&1 | tee combined.log >/dev/null; cat combined.log; rm combined.log
30 # { echo "stdout"; echo "stderr" >&2; } 3>&1 1>&2 2>&3 3>&-
31 # { echo "stdout"; echo "stderr" >&2; } 2>&1