_ _
___ | |__ __ _ _______ | |_
/ _ \| '_ \ / _` |_ / _ \| __|
| (_) | | | | (_| |/ / (_) | |_
\___/|_| |_|\__,_/___\___/ \__|
bash —
usage, redirection and tips
Commands can output to files or programs.
| Overwrite file |
: ls > log |
| Append to file |
: ls >> log |
Command results can:
| - be displayed on the terminal |
: stdout, file descriptor 1. |
| - be an error |
: stderr, file descriptor 2. |
When redirecting, only the output is sent, to add error, stderr must be added to
stdout:
ls > log 2>&1
This redirects stderr(2) to stdout(&1) and the output is saved to log.
&1 means 1 is a file descriptor, not a file.
Commands can be combined:
| Sequential execution |
: command1 ; command2 |
| Execute command2 if command1 was successful |
: command1 && command2 |
| Execute command2 if command1 failed |
: command1 || command2 |
(back to top)
Commands can be grouped and redirected together.
( command1 && command2 )
>> log 2>&1