This lesson covers Linux streams, their manipulation in shell scripting, and techniques for redirecting and chaining commands.
In this lesson, we’ll dive into Linux streams and learn how to manipulate them in shell scripting. Linux streams are the flow of data between processes—much like water flowing through a pipe. Just as you can redirect a river with gates, you can reroute streams in the shell using redirection operators and pipes.
Understanding streams is essential for robust shell scripts. You’ll see how to capture command output, handle errors, and chain commands with pipes to build powerful one-liners.
You can reroute streams using <, >, and pipes (|). This allows you to save output to files, read input from files, or chain commands together.
Copy
Ask AI
# Redirect stdout to a filegrep "error" logfile.txt > results.txt# Redirect stderr to a filegcc program.c 2> compile_errors.log# Send both stdout and stderr to the same file./run_tests.sh > all_output.log 2>&1# Read stdin from a filesort < unsorted_list.txt# Pipe stdout of one command into stdin of anotherps aux | grep sshd
When combining redirections, order matters. Always place 2>&1 after > file to capture both streams.
Mastering streams and redirection will elevate your shell scripting from basic commands to automation powerhouses. Practice these techniques to handle data flows confidently in your scripts.