Explain file descriptors 0, 1 and 2 and the main redirection operators.
BeginnerConceptArguments, I/O, here-docs, process substitution and subshells
Answer
0 is stdin, 1 is stdout, 2 is stderr.
| Syntax | Effect |
|---|---|
> f / >> f |
stdout to file (truncate / append) |
2> f |
stderr to file |
&> f or > f 2>&1 |
both to file |
2>&1 |
stderr to wherever stdout points now |
< f |
stdin from file |
>&2 |
this command's stdout to stderr |
2>/dev/null |
discard errors |
cmd 2>&1 | tee log |
both streams to screen and file |
More on Arguments, I/O, here-docs, process substitution and subshells
How does `getopts` work? Show a complete example.IntermediateWhat is the difference between `getopts` and `getopt`?AdvancedWhy is `cmd 2>&1 > file` different from `cmd > file 2>&1`?AdvancedWhat is a here-document, and what is the difference between `<<EOF`, `<<'EOF'` and `<<-EOF`?BeginnerWhat is a here-string (`<<<`)?IntermediateHow do you send a here-doc to a remote server over SSH, and when should variables expand locally vs remotely?Advanced