สตรีมมาตรฐานและการเปลี่ยนเส้นทาง
เรียนรู้เกี่ยวกับ stdin, stdout, stderr และวิธีเปลี่ยนเส้นทางด้วย `|`, `>`, `>>` และ `<`
สตรีมมาตรฐานและการเปลี่ยนเส้นทาง เป็นบทเรียน Linux Command Line Mastery ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Linux Command Line Mastery และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What are Standard Streams?
In the Linux terminal, commands communicate using 'standard streams'. Think of these as default channels for data to flow in and out of a program.
- Standard Input (stdin): Where a command gets its input from (usually keyboard).
- Standard Output (stdout): Where a command sends its normal results (usually screen).
- Standard Error (stderr): Where a command sends its error messages (usually screen).
These streams let you control how commands interact!
Standard Output (`stdout`)
When a command successfully runs and produces a result, that result is sent to standard output. By default, stdout is displayed directly on your terminal screen.
For example, when you use the echo command, its output goes to stdout.
Redirecting `stdout` (Overwrite)
The > (greater than) symbol is used to redirect standard output from the screen to a file. If the file doesn't exist, it's created. If it already exists, its content is overwritten!
This is useful for saving command results for later.
Overwrite `stdout` Example
Try running this example. Notice how the echo command's output isn't shown on screen, but stored in message.txt. Then, cat displays the file's content.
echo "Hello CoddyKit!" > message.txt
cat message.txtRedirecting `stdout` (Append)
What if you want to add output to a file without deleting its existing content? That's where >> comes in!
The >> (double greater than) operator redirects standard output to a file, appending the new content to the end of the file.
Append `stdout` Example
Let's add another line to our message.txt file without losing the first line. Run the code and observe the output of the final cat command.
echo "Hello CoddyKit!" > message.txt
echo "Welcome to Linux!" >> message.txt
cat message.txtStandard Input (`stdin`)
Standard input is the channel through which a command receives data. By default, stdin comes from your keyboard – you type commands or input values directly.
Some commands are designed to process input that you provide, or that comes from another source.
Input Redirection with `<`
The < (less than) symbol allows you to redirect standard input from a file instead of the keyboard. This is incredibly useful for feeding data from a file into a command that expects input.
The command will then process the file's content as if you typed it.
Input from File Example
The wc -l command counts lines. Here, we'll create a file and then use < to make wc -l read its input directly from that file.
echo "Line 1" > mydata.txt
echo "Line 2" >> mydata.txt
wc -l < mydata.txtStandard Error (`stderr`)
When a command encounters a problem or an error, it sends diagnostic messages to standard error. Like stdout, stderr is typically displayed on your screen by default.
Keeping stderr separate from stdout helps you easily spot errors.
Redirecting `stderr` with `2>`
To redirect standard error to a file, you use the file descriptor 2 followed by the redirection operator: 2>.
This lets you capture error messages in a log file, keeping your main output clean. You can also redirect stdout and stderr to the same file using &> or > file 2>&1.
Error Redirection Example
Let's try to list a directory that doesn't exist. We'll redirect the error message to a file named errors.log.
ls non_existent_dir 2> errors.log
cat errors.logChaining Commands with Pipes (`|`)
The | (pipe) symbol is one of the most powerful features of the Linux shell. It connects the standard output (stdout) of one command directly to the standard input (stdin) of another command.
This allows you to create complex data processing pipelines by chaining simple commands together.
Pipe Example: `ls` to `wc`
Here, the output of ls -l (a detailed list of files) is 'piped' as input to wc -l (word count - lines). This effectively counts how many items are listed by ls -l.
ls -l | wc -lQuick Check
Consider the following sequence of commands:
echo "Apple" > fruits.txt
echo "Banana" >> fruits.txt
echo "Cherry" > fruits.txt
cat fruits.txtWhat will be the final content of fruits.txt displayed by the cat command?
Recap: Streams & Redirection
Great job! You've learned the fundamentals of standard streams and how to redirect them:
- Standard Streams:
stdin(input),stdout(normal output),stderr(error output). >: Redirectsstdoutto a file, overwriting it.>>: Redirectsstdoutto a file, appending to it.<: Redirects a file's content to a command'sstdin.2>: Redirectsstderrto a file.|(Pipe): Connects thestdoutof one command to thestdinof another.
These techniques are essential for powerful command-line workflows. Next, you'll learn how to filter text using the mighty grep command!
คำถามที่พบบ่อย
บทเรียน “สตรีมมาตรฐานและการเปลี่ยนเส้นทาง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “สตรีมมาตรฐานและการเปลี่ยนเส้นทาง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Linux Command Line Mastery ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “สตรีมมาตรฐานและการเปลี่ยนเส้นทาง”
เรียนรู้เกี่ยวกับ stdin, stdout, stderr และวิธีเปลี่ยนเส้นทางด้วย `|`, `>`, `>>` และ `<` คุณปฏิบัติ Linux Command Line Mastery ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Linux Command Line Mastery หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Linux Command Line Mastery บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “สตรีมมาตรฐานและการเปลี่ยนเส้นทาง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Linux Command Line Mastery นี้ได้ไหม
ได้ บทเรียน Linux Command Line Mastery ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- สตรีมมาตรฐานและการเปลี่ยนเส้นทาง
- การกรองข้อความด้วย `grep`
- การจัดการข้อความด้วย `sed` และ `awk`
- การรวมและเรียงลำดับผลลัพธ์ด้วย sort, uniq และ cut