Bird
Raised Fist0
Linux CLIscripting~10 mins

stdout redirection (>, >>) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - stdout redirection (>, >>)
Run command
↓
Check redirection operator
↓
Overwrite file
↓
Write output
↓
End
The shell runs a command, checks if output redirection is requested with > or >>, then either overwrites or appends the output to the file.
Execution Sample
Linux CLI
echo Hello > file.txt
cat file.txt

echo World >> file.txt
cat file.txt
This script writes 'Hello' to file.txt overwriting it, then appends 'World' to the same file, showing the file content after each step.
Execution Table
StepCommandActionFile Content AfterOutput to Screen
1echo Hello > file.txtOverwrite file.txt with 'Hello\n'Hello
2cat file.txtDisplay file.txt contentHelloHello
3echo World >> file.txtAppend 'World\n' to file.txtHello World
4cat file.txtDisplay file.txt contentHello WorldHello World
5EndNo more commandsHello World
💡 All commands executed; output redirected as specified; file.txt contains two lines.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
file.txt content(empty)HelloHello WorldHello World
Key Moments - 3 Insights
Why does the first echo command overwrite the file instead of adding to it?
Because the > operator tells the shell to overwrite the file, as shown in execution_table step 1 where file.txt content changes from empty to 'Hello'.
Why does the second echo command add 'World' instead of replacing the content?
Because the >> operator appends output to the file, shown in execution_table step 3 where 'World' is added after 'Hello' without removing existing content.
Why is there no output to the screen when echo commands use > or >>?
Because output is redirected to the file, so nothing is printed on the screen during those steps, as seen in the 'Output to Screen' column for steps 1 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the content of file.txt after step 1?
A"Hello\nWorld"
B"World"
C"Hello"
D(empty)
💡 Hint
Check the 'File Content After' column for step 1 in the execution_table.
At which step does the file.txt content change from 'Hello' to 'Hello\nWorld'?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look at the 'File Content After' column and find when 'World' is appended.
If we replace >> with > in step 3, what would be the file content after step 3?
A"Hello\nWorld"
B"World"
C"Hello"
D(empty)
💡 Hint
Remember > overwrites the file, so the previous content is replaced.
Concept Snapshot
stdout redirection:
> overwrites file with command output
>> appends command output to file
Use > to start fresh file content
Use >> to add without deleting
No output shown on screen when redirected
Full Transcript
This lesson shows how stdout redirection works in Linux shell. When you run a command like 'echo Hello > file.txt', the > operator tells the shell to overwrite file.txt with the output 'Hello'. If you use 'echo World >> file.txt', the >> operator appends 'World' to the existing content. The execution table traces each command step by step, showing how the file content changes and what appears on the screen. Key points include understanding the difference between > and >> and why redirected output does not show on the terminal. This helps beginners see exactly how output redirection affects files and terminal display.

Practice

(1/5)
1. What does the command echo "Hello" > file.txt do in Linux?
easy
A. It writes "Hello" to file.txt, replacing any existing content.
B. It adds "Hello" to the end of file.txt without removing existing content.
C. It displays "Hello" on the screen without saving to a file.
D. It deletes file.txt if it exists.

Solution

  1. Step 1: Understand the > operator

    The > operator redirects output to a file, replacing its content.
  2. Step 2: Analyze the command effect

    echo "Hello" outputs "Hello" and > saves it to file.txt, overwriting old content.
  3. Final Answer:

    It writes "Hello" to file.txt, replacing any existing content. -> Option A
  4. Quick Check:

    > means overwrite file [OK]
Hint: Use > to overwrite file content, >> to append [OK]
Common Mistakes:
  • Confusing > with >> and thinking it appends
  • Believing output only shows on screen
  • Assuming file is deleted
2. Which of the following commands correctly appends the text "Log entry" to a file named log.txt?
easy
A. echo "Log entry" < log.txt
B. echo "Log entry" > log.txt
C. echo "Log entry" >! log.txt
D. echo "Log entry" >> log.txt

Solution

  1. Step 1: Identify append operator

    The >> operator appends output to the end of a file without deleting existing content.
  2. Step 2: Match command with append operator

    Only echo "Log entry" >> log.txt uses >> correctly to append "Log entry" to log.txt.
  3. Final Answer:

    echo "Log entry" >> log.txt -> Option D
  4. Quick Check:

    >> means append output [OK]
Hint: Use >> to add text without erasing file [OK]
Common Mistakes:
  • Using > which overwrites instead of appending
  • Using < which is input redirection, not output
  • Using invalid >! operator
3. Given the file content of data.txt is initially empty, what will be the content of data.txt after running these commands in order?
echo "First" > data.txt
echo "Second" >> data.txt
echo "Third" > data.txt
medium
A. Third
B. First\nSecond
C. Second\nThird
D. First\nSecond\nThird

Solution

  1. Step 1: Analyze first command

    echo "First" > data.txt writes "First" to data.txt, replacing any content.
  2. Step 2: Analyze second command

    echo "Second" >> data.txt appends "Second" after "First".
  3. Step 3: Analyze third command

    echo "Third" > data.txt overwrites entire file with "Third" only.
  4. Final Answer:

    Third -> Option A
  5. Quick Check:

    Last > overwrites file content [OK]
Hint: Last > overwrites file, ignoring previous appends [OK]
Common Mistakes:
  • Thinking >> always appends without overwrite
  • Assuming all lines remain after last >
  • Confusing ">" and ">>" effects
4. You want to append the output of date command to a file named times.log. Which command has an error and why?
A) date >> times.log
B) date > times.log
C) date >>> times.log
D) date >> times.log
medium
A. Correctly appends date output to times.log
B. Overwrites times.log with date output
C. Syntax error due to invalid operator >>>
D. gol.semit ot tuptuo etad sdneppa yltcerroC

Solution

  1. Step 1: Check each redirection operator

    A uses >> (append), B uses > (overwrite), C uses >>> (invalid), D uses >> (valid).
  2. Step 2: Identify invalid syntax

    Operator >>> is not valid in Linux shell, causing syntax error.
  3. Final Answer:

    Syntax error due to invalid operator >>> -> Option C
  4. Quick Check:

    >> is invalid operator [OK]
Hint: Only > and >> are valid output redirections [OK]
Common Mistakes:
  • Using >>> instead of >>
  • Thinking no space after >> is error
  • Confusing > and >> effects
5. You have a script that logs errors to error.log. You want to keep all previous errors and add new ones each time the script runs. Which command should you use inside the script to save the error message "Error occurred" to the file without deleting old logs?
hard
A. echo "Error occurred" > error.log
B. echo "Error occurred" >> error.log
C. echo "Error occurred" >! error.log
D. echo "Error occurred" < error.log

Solution

  1. Step 1: Understand logging requirement

    We want to keep old logs and add new errors, so appending is needed.
  2. Step 2: Choose correct redirection operator

    > overwrites file, >> appends to file. So >> is correct to keep old content.
  3. Final Answer:

    echo "Error occurred" >> error.log -> Option B
  4. Quick Check:

    Use >> to append logs without deleting [OK]
Hint: Use >> to add logs, never > to avoid losing data [OK]
Common Mistakes:
  • Using > which erases old logs
  • Using < which is input redirection
  • Using invalid >! operator
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard