streams

A command wrote an error to the terminal and the script kept going Nothing was lost I just was not reading the right stream

Three streams

NumberNameDefault destination
0stdinkeyboard
1stdoutterminal
2stderrterminal

The numbers matter when you redirect

Pipe

printf 'payload\n' | tr '[:lower:]' '[:upper:]'

Output:

PAYLOAD

| connects stdout of the left command to stdin of the right stderr is not piped unless you say so

Keep a copy

printf 'kept\n' | tee out.txt

tee writes to the file and to stdout Useful when you want the log and the screen at the same time

Catch the error stream

{ printf 'bad\n' >&2; } 2>&1

Output:

bad

2>&1 merges stderr into stdout >&2 sends stdout to stderr Order matters , 2>&1 after a redirect behaves differently

Here doc

cat <<'DOC'
here
DOC

Quoted delimiter stops expansion inside the block Unquoted delimiter lets variables expand Pick on purpose

Next

Variables and quoting come in 06 variables

Posted