i/o redirection
There are always three default files open, STDIN, STDOUT, and STDERR. These, and any other open files, can be redirected. Redirection simply means capturing output from a file, command, program, script, or even code block within a script and sending it as input to another file, command, program, or script.
Name | Default Destination | Redirection Use | File Descriptor Number |
---|---|---|---|
STDIN | Keyboard | < | 0 |
STDOUT | Monitor | > | 1 |
STDERR | Monitor | 2> | 2 |
Using I/O Redirection
Redirector | Explanation | Example |
---|---|---|
1> | Redirect STDOUT | 1> stdout.txt |
1>> | Redirect and append STDOUT | 1>> stdout.txt |
2> | Redirect STDERR | 2> stderr.txt |
2>> | Redirect and append STDERR | 2>> stderr.txt |
&> | Redirect both STDOUT and STDERR | &> stdout_err.txt |
&>> | Redirect and append both STDOUT and STDERR | &>> stdout_err.txt |
Using Pipes
A pipe |
can be used to catch the output of one command and use that as input for a second command. A pipe |
can be useful for chaining commands, scripts, files, and programs together.
cat *.txt | sort | uniq > result-file
# Sorts the output of all the .txt files and deletes duplicate lines,
# finally saves results to "result-file".