/dev/null.
1. Basic redirection with >
A single greater‐than symbol sends a file descriptor to a file (or another stream):
- Left of
>is the source FD (default is 1, i.e. stdout). - Right of
>is the destination (often a filename).
When you omit the FD before
>, Bash assumes 1 (standard output).2. Merging stdout and stderr with &>
Bash provides a shorthand to capture both streams at once:
&> is equivalent to >file 2>&1 in Bash.
3. Duplicating file descriptors using >&n
When & appears on the right side of >, you’re duplicating an FD instead of writing to a filename:
&> file.txt, which writes both stdout and stderr into a file.
4. Swapping streams with n>&m
You can redirect one FD into another:
-
Send stdout into stderr:
-
Send stderr into stdout:
Order matters when combining redirections. Always redirect stdout first, then redirect stderr into it.
5. Redirecting both streams to a file
To capture both stdout and stderr in a single file, use:> file.txtsends stdout (fd 1) intofile.txt.2>&1redirects stderr (fd 2) into wherever stdout is now pointing.
6. Discarding all output with /dev/null
If you want a command to produce no output, redirect both streams to /dev/null, the special “black hole” in Unix-like systems:


Key takeaways
| Operator | Purpose | Example |
|---|---|---|
>file | Redirect stdout (fd 1) to a file | ls > out.txt |
2>file | Redirect stderr (fd 2) to a file | ls -z 2>err.txt |
&>file | Redirect both stdout and stderr to a file | cmd &>all.txt |
n>&m | Duplicate FD n into FD m | echo "err" >&2 |
2>&1 | Merge stderr into stdout | cmd >out.txt 2>&1 |
> /dev/null 2>&1 | Discard both stdout and stderr | some_command > /dev/null 2>&1 |
