Bird
0
0

How can you append the output of a loop that prints numbers 1 to 3 into numbers.txt without overwriting existing content?

hard🚀 Application Q9 of 15
Bash Scripting - File Operations in Scripts
How can you append the output of a loop that prints numbers 1 to 3 into numbers.txt without overwriting existing content?
Afor i in {1..3}; do echo $i >> numbers.txt; done
Bfor i in {1..3}; do echo $i > numbers.txt; done
Cfor i in {1..3}; do echo $i; done > numbers.txt
Dfor i in 1..3; do echo $i >> numbers.txt; done
Step-by-Step Solution
Solution:
  1. Step 1: Understand appending inside a loop

    Using >> inside the loop appends each number to the file one by one.
  2. Step 2: Check other options

    Options using > overwrite the file (either each iteration or the entire output), and missing braces causes incorrect loop iteration with i="1..3".
  3. Final Answer:

    for i in {1..3}; do echo $i >> numbers.txt; done -> Option A
  4. Quick Check:

    Append inside loop with >> to keep all lines [OK]
Quick Trick: Use >> inside loop to append each output [OK]
Common Mistakes:
MISTAKES
  • Using > inside loop overwrites
  • Using > outside loop overwrites
  • Missing braces {1..3} for correct loop values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes