In POSIX (Portable Operating System Interface) systems, such as Linux and macOS, programs interact with the world through three standard data streams:
Redirection allows you to change where these streams go. Instead of reading from the keyboard or writing to the terminal, you can redirect input from a file or send output to a file. Here are some common redirection operators:
< (Input Redirection): Redirects standard input from a file. For example, command < input.txt runs command with the contents of input.txt as its input.> (Output Redirection): Redirects standard output to a file, overwriting the file if it exists. For example, command > output.txt runs command and saves its output to output.txt.>> (Appending Output Redirection): Redirects standard output to a file, appending to the file if it exists. For example, command >> output.txt adds the output of command to the end of output.txt.2> (Error Redirection): Redirects standard error to a file. For example, command 2> errors.txt saves any error messages from command to errors.txt.2>&1 (Redirecting stderr to stdout): Redirects standard error to the same location as standard output. For example, command > output.txt 2>&1 saves both the standard output and any error messages to output.txt.
Piping allows you to connect the output of one command to the input of another command. This creates a chain of commands where the data flows from one to the next. The pipe operator is |.
For example, command1 | command2 takes the standard output of command1 and makes it the standard input of command2. This is very useful for processing data in stages. You can even chain multiple commands together: command1 | command2 | command3.
A common example is using grep to search for a string in the output of another command: ls -l | grep "txt" lists all files in the current directory and then filters the output to only show lines containing "txt".
Redirection and piping are powerful tools for manipulating data flow and are fundamental to shell scripting and command-line work in POSIX systems.