Shorts
Pipes SHORT
COMMAND COVERED

>
- redirect standard output to a file

Redirect output to a file using >

This example uses the echo command to print the word hello and redirects the output to a file named file.txt using the standard output redirection symbol >. Running cat file.txt confirms the file was created and contains the expected text. If the file already existed, > would overwrite the contents entirely. This is one of the most fundamental Unix shell output redirection operators.
Pipes SHORT
COMMAND COVERED

>>
- append standard output to a file

Append output to a file using >>

This example initially writes 1 to a file named file.txt by redirecting standard output with the > operator. Next, it then appends 2 to the file using the >> operator. Running the command cat file.txt shows both lines are present in the file. Unlike >, the >> operator appends to the end of an existing file rather than overwriting it. This makes it useful for logging or building up a file incrementally.
Pipes SHORT
COMMAND COVERED

<
- redirect standard input from a file

Redirect a file to standard input using <

This example uses < to feed the contents of the ~/.vuerc file into the head command as standard input. The -n 3 flag limits the output to the first three lines. Rather than passing a filename as an argument, the shell opens the file and connects it to the command's STDIN (standard input). This distinction of using < is mostly useful for commands that only read from standard input and don't accept file arguments.
Pipes SHORT
COMMAND COVERED

|
- pipe standard output to another command

Pipe output between commands using |

This example uses printf, a tool which produces formatted output, to generate three lines of text and pipes the result directly to the grep command. The filter grep ba prints only lines containing ba, returning bar and baz. The pipe symbol | connects the standard output of one command to the standard input of the next. This is the foundation of Unix command composition. The printf command supports many options for formatting that are detailed in the man pages.
Pipes SHORT
COMMAND COVERED

tee
- duplicate standard input

Write output to both stdout and a file using tee

This example pipes the output of echo hello into the tee command, which copies the standard input to standard output. Essentially, it writes simultaneously to the terminal as well as the out.txt file. Running cat out.txt confirms the file was written. Unlike a plain redirect, tee lets you see the output in real time while also saving it. It's especially useful in long-running pipelines where you want to monitor and log output at the same time.
Pipes SHORT
COMMAND COVERED

2>
- redirect standard error

Suppress error output by redirecting stderr to /dev/null

This example first runs the command ls /nonexistent to produce an error message due to the directory not existing on the file system. The second attempt uses 2> /dev/null, redirecting STDERR (standard error) to the null device where output is discarded. The terminal shows nothing the second time, confirming the error was suppressed. Remember, the 2> part indicates that STDERR, also known as file descriptor 2, should be redirected. Redirecting to /dev/null is a common technique for silencing unwanted error messages in scripts.
Pipes SHORT
COMMAND COVERED

2>
- redirect standard error to a file

Save error output to a log file using 2>

This example first demonstrates the standard output of listing the ~/src directory (expanded by the Unix shell to be /Users/chip/src), followed by the error produced by attempting to list the contents of /nonexistent, a directory that does not exist. The second run redirects STDERR (standard error) to a file named error.log using 2>, so only STDOUT (standard output) appears on the screen. Running the command cat error.log confirms the error message, and therefore only STDERR, was captured in the file. This pattern is commonly used in scripts to preserve error output for later review.
Pipes SHORT
COMMAND COVERED

&>
- redirect both stdout and stderr to a file

Redirect both stdout and stderr to a file using &>

This example runs the ls command for both ~/src and /nonexistent, but uses &> to redirect both STDOUT (standard output) and STDERR (standard error) streams to the output.log file. As you can see from this syntax, no output is displayed to STDOUT. Running cat output.log verifies that both streams were captured, first showing the error, followed by the output of /Users/chip/src.