Master Bash File Redirection: From /dev/null to TCP Streams
This guide demonstrates how to use Bash to redirect standard input, output, and error streams to files, /dev/null, and even a TCP server, illustrating each technique with clear examples and step‑by‑step commands.
0x01 Test Program
The article starts with a small Rust program that reads data from standard input and writes the same data to both standard output and standard error, serving as the test subject for the redirection examples.
0x02 Redirect Standard Input to /dev/null
/dev/nullis a special file in Linux that immediately returns EOF on read. Redirecting the program’s input from /dev/null results in empty output for both stdout and stderr.
0x03 Redirect Standard Input to a Regular File
First, use echo hello > stdin.log to write the string hello into a file. Then run the test program with its standard input redirected from stdin.log. Both stdout and stderr now display hello.
0x04 Redirect Standard Output to a Regular File
Redirect the program’s standard output to stdout.log. The terminal will only show the stderr line, while the stdout line is saved in stdout.log.
0x05 Redirect Standard Error to a Regular File
Redirect the program’s standard error (file descriptor 2) to stderr.log using 2> stderr.log. No output appears on the terminal; both stdout and stderr are stored in their respective files.
0x06 Redirect Both Standard Output and Error to the Same File
First redirect stdout to hello.log, then use 2>&1 to point stderr (fd 2) to the same file descriptor as stdout (fd 1). The order matters: the redirection must be performed after stdout has been redirected, otherwise both descriptors still point to the terminal.
Incorrect order (stderr first, then stdout) leaves stderr pointing to the terminal, so the command fails to capture error output.
Another valid method combines the two redirections in a single command:
0x07 Append Both Output and Error to the Same File
To preserve existing file contents, use the append operator >> instead of overwrite >. The command >>> hello.log 2>&1 appends both stdout and stderr to hello.log.
0x08 Use a Here‑Document to Supply Multi‑Line Input
A single‑line string can be fed to stdin with echo "text" | ./program. For multi‑line input, a here‑document is used:
The delimiter (e.g., xxx) can be any chosen string.
0x09 Redirect Output and Error to a TCP Server
Start a TCP listener with ncat -l -p 9999. Then redirect both stdout and stderr to the server using:
Here localhost is the target address and 9999 the port. After running the command, the ncat window shows the captured output.
0x0a Additional Resources
For more details, consult the Bash manual pages ( man bash).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
