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
| Number | Name | Default destination |
|---|---|---|
| 0 | stdin | keyboard |
| 1 | stdout | terminal |
| 2 | stderr | terminal |
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