Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Writing to files (echo, printf) in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Writing to files (echo, printf)
Start Script
↓
Run echo or printf
↓
Redirect output to file
↓
Write content to file
↓
Close file
↓
End Script
The script runs echo or printf commands, redirects their output to a file, writes the content, then closes the file.
Execution Sample
Bash Scripting
echo "Hello World" > greetings.txt
printf "Line 1\nLine 2\n" >> greetings.txt
Writes 'Hello World' to greetings.txt, then appends two lines using printf.
Execution Table
StepCommandActionFile Content After Step
1echo "Hello World" > greetings.txtCreate greetings.txt and write 'Hello World' with newlineHello World
2printf "Line 1\nLine 2\n" >> greetings.txtAppend two lines 'Line 1' and 'Line 2' with newlinesHello World Line 1 Line 2
3End of scriptNo more commands, file closedHello World Line 1 Line 2
💡 Script ends after writing and appending to greetings.txt
Variable Tracker
VariableStartAfter Step 1After Step 2Final
greetings.txt content(file does not exist)Hello World Hello World Line 1 Line 2 Hello World Line 1 Line 2
Key Moments - 3 Insights
Why does the first command use a single > and the second use >>?
The first command uses > to create or overwrite the file with new content (see Step 1 in execution_table). The second uses >> to append without erasing existing content (see Step 2).
Does echo add a newline automatically?
Yes, echo adds a newline at the end of the output, so 'Hello World' is followed by a newline in the file (Step 1).
Why use printf instead of echo for multiple lines?
printf allows explicit control of formatting and newlines (\n), so it can write multiple lines precisely (Step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the content of greetings.txt after Step 1?
A"Line 1\nLine 2\n"
B"Hello World"
C"Hello World\n"
D"Hello World\nLine 1\nLine 2\n"
💡 Hint
Check the 'File Content After Step' column for Step 1 in the execution_table.
At which step does the file greetings.txt get appended instead of overwritten?
AStep 1
BStep 2
CStep 3
DNever appended
💡 Hint
Look at the 'Command' and 'Action' columns in execution_table for Step 2.
If we replaced >> with > in Step 2, what would happen to the file content?
AThe file would contain only 'Line 1\nLine 2\n'
BThe file would contain 'Hello World' and the two lines appended
CThe file would contain only 'Hello World'
DThe file would be empty
💡 Hint
Recall that > overwrites the file, so Step 2 would erase previous content.
Concept Snapshot
Writing to files in bash:
- Use echo or printf to produce output
- Use > to write (overwrite) a file
- Use >> to append to a file
- echo adds newline automatically
- printf allows formatted output with explicit newlines
Full Transcript
This visual trace shows how bash commands echo and printf write to files. First, echo writes 'Hello World' with a newline to greetings.txt using > which creates or overwrites the file. Then printf appends two lines with explicit newlines using >> which adds to the file without erasing. The file content changes step-by-step, showing how redirection operators control writing or appending. Key points include echo's automatic newline, printf's formatting power, and the difference between > and >>. This helps beginners see exactly how file writing works in bash scripts.

Practice

(1/5)
1. Which command in bash writes the text Hello World to a file named greeting.txt and adds a newline automatically?
easy
A. echo "Hello World" > greeting.txt
B. printf "Hello World" > greeting.txt
C. echo -n "Hello World" > greeting.txt
D. printf "Hello World\n" > greeting.txt

Solution

  1. Step 1: Understand echo behavior

    The echo command prints text and adds a newline by default.
  2. Step 2: Compare with printf

    printf requires explicit newline characters to add a newline.
  3. Final Answer:

    echo "Hello World" > greeting.txt -> Option A
  4. Quick Check:

    echo adds newline automatically = A [OK]
Hint: Use echo for quick writes with newline [OK]
Common Mistakes:
  • Using printf without \n misses newline
  • Using echo -n suppresses newline
  • Confusing > and >> operators
2. Which of the following is the correct syntax to append the line Log entry to a file log.txt using printf?
easy
A. printf "Log entry" >> log.txt
B. printf -n "Log entry" >> log.txt
C. printf "Log entry\n" >> log.txt
D. printf -e "Log entry" >> log.txt

Solution

  1. Step 1: Understand printf newline requirement

    printf does not add newline automatically; you must include \n explicitly.
  2. Step 2: Check append operator

    >> appends to the file without overwriting.
  3. Final Answer:

    printf "Log entry\n" >> log.txt -> Option C
  4. Quick Check:

    printf needs \n for newline + append with >> = C [OK]
Hint: Always add \n with printf to get a new line [OK]
Common Mistakes:
  • Forgetting \n causes no newline
  • Using > instead of >> overwrites file
  • Using invalid printf flags like -n or -e
3. What will be the content of file.txt after running this script?
echo "Line1" > file.txt
printf "Line2\n" >> file.txt
echo -n "Line3" >> file.txt
medium
A. Line1\nLine2Line3
B. Line1\nLine2\nLine3\n
C. Line1Line2Line3
D. Line1\nLine2\nLine3

Solution

  1. Step 1: Analyze echo "Line1" > file.txt

    This writes "Line1" plus newline to file.txt, overwriting existing content.
  2. Step 2: Analyze printf "Line2\n" >> file.txt

    This appends "Line2" plus newline to file.txt.
  3. Step 3: Analyze echo -n "Line3" >> file.txt

    This appends "Line3" without newline to file.txt.
  4. Final Answer:

    Line1\nLine2\nLine3 -> Option D
  5. Quick Check:

    echo adds newline, printf adds newline, echo -n no newline = D [OK]
Hint: Remember echo -n skips newline, printf needs explicit \n [OK]
Common Mistakes:
  • Assuming echo -n adds newline
  • Forgetting printf needs \n
  • Confusing overwrite (>) and append (>>)
4. The following script is intended to write two lines to output.txt but only writes one line. What is the error?
echo "First line" > output.txt
printf "Second line" > output.txt
medium
A. printf needs \n to add newline
B. Second command overwrites the file instead of appending
C. echo should use >> to append
D. Missing quotes around Second line

Solution

  1. Step 1: Check file redirection operators

    The first command uses > which creates or overwrites output.txt with "First line".
  2. Step 2: Check second command redirection

    The second command also uses > which overwrites output.txt, replacing previous content.
  3. Step 3: Correct approach

    To keep both lines, second command should use >> to append instead of overwrite.
  4. Final Answer:

    Second command overwrites the file instead of appending -> Option B
  5. Quick Check:

    Using > twice overwrites file = D [OK]
Hint: Use >> to append, > overwrites file [OK]
Common Mistakes:
  • Thinking \n missing causes no second line
  • Confusing > and >> operators
  • Assuming echo always appends
5. You want to create a file report.txt with exactly two lines:
"Summary:"
"All tests passed"
Which script snippet correctly creates this file with proper newlines and no extra blank lines?
hard
A. printf "Summary:\nAll tests passed\n" > report.txt
B. echo "Summary:" > report.txt && echo "All tests passed" > report.txt
C. echo -n "Summary:\nAll tests passed" > report.txt
D. printf "Summary:" > report.txt && printf "All tests passed" >> report.txt

Solution

  1. Step 1: Check echo commands in echo "Summary:" > report.txt && echo "All tests passed" > report.txt

    echo "Summary:" > report.txt && echo "All tests passed" > report.txt uses echo twice with > redirection, so the second command overwrites the first line.
  2. Step 2: Check printf commands in printf "Summary:\nAll tests passed\n" > report.txt

    printf "Summary:\nAll tests passed\n" > report.txt uses printf with explicit \n for newlines, creating exactly two lines with no extra blank lines.
  3. Step 3: Analyze Options C and D

    echo -n "Summary:\nAll tests passed" > report.txt uses echo -n which suppresses newlines and includes literal \n, so no actual new lines appear. printf "Summary:" > report.txt && printf "All tests passed" >> report.txt uses printf without \n, so lines run together.
  4. Final Answer:

    printf "Summary:\nAll tests passed\n" > report.txt -> Option A
  5. Quick Check:

    printf with explicit \n creates exact lines = B [OK]
Hint: Use printf with \n for precise multi-line output [OK]
Common Mistakes:
  • Using echo -n with \n prints literal \n
  • Forgetting \n in printf causes no new lines
  • Using > and >> inconsistently
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