- by x32x01 ||
In Bash, redirection controls where a command reads input from and where it sends output to. Mastering redirections is essential for writing robust scripts, saving logs, handling errors, and chaining commands. This cheat-sheet explains the common redirection operators, file-descriptor tricks, here-documents, process substitution, and practical examples you can use immediately. 
Basic Output Redirections
Example:
Error Redirections (stderr)
Example:
Redirecting Both stdout and stderr
Correct usage:
Warning:
Discard Output (silence)
Example:
Input Redirections
Here-documents (multi-line input):
If EOF is quoted (
Here-strings (single line):
Process Substitution (anonymous FIFOs)
Process substitution lets you treat command output like a file.
Example (compare two directories):
Example (send different streams to different handlers):
Pipes and Combined Streams |
Get exit codes of all piped commands:
Advanced File Descriptor Tricks
Bash supports custom file descriptors and
Copying and closing fds:
Example:
Redirecting Multiple Commands (groups)
Use
Network I/O via /dev/tcp and /dev/udp (bash feature)
Bash can open TCP/UDP connections using
This is a lightweight way to perform basic socket operations in bash scripts.
Practical Examples & Tips
Common Pitfalls to Avoid
Conclusion - Control Your I/O Like a Pro
Redirections are powerful and let you shape how programs interact with files, devices, and each other. Once you practice the basics - stdout, stderr, pipes, here-docs, and file descriptors - you’ll be able to build concise, reliable scripts and handle complex workflows without external tools. Keep this cheat-sheet handy and try the examples in a test shell.
Basic Output Redirections
cmd > file
Redirect standard output (stdout) ofcmdtofile(overwrite).cmd 1> file
Same as cmd > file - 1 is the file descriptor (fd) for stdout.cmd >> file
Append stdout ofcmdtofile.
Example:
Bash:
echo "Hello" > out.txt
echo "More" >> out.txt Error Redirections (stderr)
cmd 2> file
Redirect standard error (stderr) tofile.cmd 2>> file
Append stderr tofile.
Example:
Bash:
ls /no/such/path 2> errors.log Redirecting Both stdout and stderr
cmd &> file
Redirect both stdout and stderr to file (bash shorthand).cmd > file 2>&1
Redirect stdout to file then redirect stderr to where stdout is pointing. Order matters.
Correct usage:
Bash:
command > all.log 2>&1
# or (bash-only)
command &> all.log cmd 2>&1 > file is not equivalent; that order redirects stderr to current stdout (typically the terminal) before stdout is redirected to file.Discard Output (silence)
- cmd > /dev/null - discard stdout
- cmd 2> /dev/null - discard stderr
- cmd &> /dev/null - discard both
Example:
Bash:
grep pattern bigfile > /dev/null 2>&1 Input Redirections
cmd < file- Use file as stdin forcmd.
Here-documents (multi-line input):
Bash:
cat <<EOF
line1
line2
EOF <<'EOF'), the content is literal (no variable expansion). Use <<-EOF to strip leading tabs.Here-strings (single line):
Bash:
grep foo <<< "single-line input" Process Substitution (anonymous FIFOs)
Process substitution lets you treat command output like a file.- cmd <(cmd1) - pass stdout of cmd1 as a filename argument to cmd.
- cmd >(cmd2) - create a pipe where cmd writes and cmd2 reads.
Example (compare two directories):
Bash:
diff <(ls /path1 | sort) <(ls /path2 | sort) Example (send different streams to different handlers):
Bash:
command > >(grep "info" > info.log) 2> >(grep "error" > error.log) Pipes and Combined Streams |
cmd1 | cmd2- pipe stdout ofcmd1to stdin ofcmd2.cmd1 |& cmd2- pipe stdout and stderr (bash 4+). For older bash:cmd1 2>&1 | cmd2.
Get exit codes of all piped commands:
Bash:
cmd1 | cmd2 | cmd3
echo ${PIPESTATUS[@]} Advanced File Descriptor Tricks
Bash supports custom file descriptors and exec to manipulate them:exec 3> file- open file for writing on fd 3.exec 3< file- open file for reading on fd 3.exec 3>&-- close fd 3.echo "text" >&3- write to fd 3.cat <&3- read from fd 3.
Copying and closing fds:
exec 4>&3- duplicate fd 3 to fd 4.exec 4>&3-- copy fd 3 to 4 and close 3.
Example:
Bash:
exec 3> logfile.txt
echo "Start" >&3
exec 3>&- Redirecting Multiple Commands (groups)
- Subshell grouping
( ... )runs in a subshell:
Bash:
( cmd1; cmd2 ) > out.txt - Compound grouping
{ ...; }runs in the current shell (no subshell):
Bash:
{ cmd1; cmd2; } > out.txt {} when you want the redirection to affect the current shell and to avoid subshell overhead.Network I/O via /dev/tcp and /dev/udp (bash feature)
Bash can open TCP/UDP connections using exec on special device files (not available on all shells): Bash:
exec 3<> /dev/tcp/example.com/80
printf 'GET / HTTP/1.0\r\nHost: example.com\r\n\r\n' >&3
cat <&3
exec 3>&- Practical Examples & Tips
- Log both output and errors and still see them on screen:
Bash:
command 2>&1 | tee -a combined.log - Swap stdout and stderr for a command:
Bash:
command 3>&1 1>&2 2>&3 - Run a long job, background it, and redirect output:
Bash:
longjob > job.out 2> job.err & - Debugging scripts:
Bash:
set -x # enable debug trace
# run commands...
set +x # disable debug trace Common Pitfalls to Avoid
- Pay attention to the order when mixing
[ICODE]2>&1[/ICODE] and > - wrong order leads to unexpected results. - Remember that
&>is bash-specific (not POSIX). For maximum portability use> file 2>&1. - Don’t forget to close custom file descriptors (
exec 3>&-) to avoid resource leaks in long-running scripts. - Quoting matters in here-documents: unquoted delimiter prevents variable expansion.
Conclusion - Control Your I/O Like a Pro
Redirections are powerful and let you shape how programs interact with files, devices, and each other. Once you practice the basics - stdout, stderr, pipes, here-docs, and file descriptors - you’ll be able to build concise, reliable scripts and handle complex workflows without external tools. Keep this cheat-sheet handy and try the examples in a test shell. Last edited: