Ever wondered how Linux commands talk to each other? Let's dive into the power-packed symbols and streams that drive automation in the terminal.
FromSTDOUT
to2>&1
, this is your one-stop cheat sheet π§Ύ
π¬ What Are STDIN, STDOUT & STDERR?
Every process in Linux interacts with 3 default streams:
Stream | Meaning | File Descriptor | Role |
---|---|---|---|
STDIN |
Standard Input | 0 |
Takes input (usually keyboard) |
STDOUT |
Standard Output | 1 |
Shows successful command output |
STDERR |
Standard Error Output | 2 |
Displays error messages |
β‘οΈ STDOUT
is for successful output, while STDERR
is where errors are sent β they are separate!
π’ Whatβs a File Descriptor?
File Descriptors are just numbers assigned to files or streams opened by a process.
They help the shell identify where to read/write.
Standard mapping:
0 - STDIN (input)
1 - STDOUT (normal output)
2 - STDERR (error output)
π§ Shell Symbols Cheat Sheet
Symbol | Name | What It Does |
---|---|---|
; |
Command Separator | Runs commands one after another, regardless of success. |
| | Pipe | Sends output of one command to input of another. |
> |
Redirect Output | Sends STDOUT to a file (overwrite). |
>> |
Redirect Output | Sends STDOUT to a file (append). |
2> |
Redirect STDERR | Sends errors to a file. |
2>&1 |
Merge Outputs | Merges STDERR into STDOUT. |
π Real-Life Examples
π© ;
β Run Multiple Commands
echo "Start"; ls; echo "End"
π¦ |
β Pipe Output into Another Command
ps aux | grep apache
π₯ >
β Redirect STDOUT (Overwrite)
echo "Hello World" > file.txt
π¨ >>
β Redirect STDOUT (Append)
echo "New Line" >> file.txt
π§ 2>
β Redirect STDERR
ls notafile 2> error.log
πͺ 2>&1
β Combine STDOUT + STDERR
make > output.log 2>&1
π§ Bonus: Redirect STDIN (Input)
sort < names.txt
π‘ Quick Reference Table
Operator | Meaning | Example |
---|---|---|
> |
STDOUT to file (overwrite) | echo hi > file.txt |
>> |
STDOUT to file (append) | echo again >> file.txt |
2> |
STDERR to file | cmd 2> errors.txt |
&> |
STDOUT + STDERR to file |
cmd &> alloutput.txt (Bash only) |
2>&1 |
Merge STDERR to STDOUT | cmd > out.txt 2>&1 |
< |
Redirect file as input | sort < names.txt |
; |
Run commands in sequence | date; cal |
π₯ Bash Scripting Overview
These redirection symbols and streams are heavily used in bash scripting to handle outputs, store logs, and avoid error clutter. For example:
#!/bin/bash
ls /etc > out.txt 2> err.txt
In upcoming posts, weβll dive deeper into bash scripting, but this gives you a quick taste of whatβs possible π§.
π Coming Soon: Quotes in Shell Scripts
Weβll soon talk about the differences between single quotes '...'
, double quotes "..."
, and backticks ...\
β and how they change how commands behave in shell scripting.
Stay tuned for the next post! π
π Wrap-Up
These symbols may look tiny, but they are mighty! Mastering them helps you script smarter, debug easier, and automate faster.
π¬ Got a trick or shortcut you love? Share it in the comments!
#Linux #Bash #ShellScripting #CommandLine #DevOps #Redirection #CheatSheet
Top comments (0)