Bash Redirections: Complete Linux Cheat Sheet

x32x01
  • 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 ▶️

  • cmd > file
    Redirect standard output (stdout) of cmd to file (overwrite).
  • cmd 1> file
    Same as cmd > file - 1 is the file descriptor (fd) for stdout.
  • cmd >> file
    Append stdout of cmd to file.

Example:
Bash:
echo "Hello" > out.txt
echo "More" >> out.txt



Error Redirections (stderr) ⚠️

  • cmd 2> file
    Redirect standard error (stderr) to file.
  • cmd 2>> file
    Append stderr to file.

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
Warning: 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 for cmd.

Here-documents (multi-line input):
Bash:
cat <<EOF
line1
line2
EOF
If EOF is quoted (<<'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 of cmd1 to stdin of cmd2.
  • 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
Use {} 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>&-
This is a lightweight way to perform basic socket operations in bash scripts.



Practical Examples & Tips ✅

  1. Log both output and errors and still see them on screen:
Bash:
command 2>&1 | tee -a combined.log
  1. Swap stdout and stderr for a command:
Bash:
command 3>&1 1>&2 2>&3
  1. Run a long job, background it, and redirect output:
Bash:
longjob > job.out 2> job.err &
  1. 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:
Related Threads
x32x01
Replies
0
Views
739
x32x01
x32x01
x32x01
Replies
0
Views
832
x32x01
x32x01
x32x01
Replies
0
Views
730
x32x01
x32x01
x32x01
Replies
0
Views
804
x32x01
x32x01
x32x01
Replies
0
Views
918
x32x01
x32x01
x32x01
  • x32x01
Replies
0
Views
746
x32x01
x32x01
x32x01
Replies
0
Views
786
x32x01
x32x01
x32x01
  • x32x01
Replies
0
Views
67
x32x01
x32x01
x32x01
  • x32x01
Replies
0
Views
768
x32x01
x32x01
x32x01
Replies
0
Views
802
x32x01
x32x01
Register & Login Faster
Forgot your password?
Forum Statistics
Threads
628
Messages
632
Members
64
Latest Member
alialguelmi
Back
Top