Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Appending to files (>>) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
File Append Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the content of the file after appending?
You have a file named log.txt containing:
Line1

What will be the content of log.txt after running this command?
echo "Line2" >> log.txt
ALine1\n\nLine2
BLine2
CLine1\nLine2
DLine1
Attempts:
2 left
💡 Hint
Appending adds new content after existing content without removing it.
💻 Command Output
intermediate
1:30remaining
What happens when appending with a missing file?
If notes.txt does not exist, what will be the content of notes.txt after running?
echo "Start" >> notes.txt
ANothing happens, file remains missing
BError: notes.txt not found
CFile created but empty
DFile created with content: Start
Attempts:
2 left
💡 Hint
Appending creates the file if it does not exist.
📝 Syntax
advanced
2:00remaining
Which command correctly appends multiple lines to a file?
You want to append these two lines to data.txt:
First line
Second line

Which command will do this correctly?
Aecho "First line" >> data.txt && echo "Second line" >> data.txt
Becho "First line\nSecond line" >> data.txt
Cecho -e "First line\nSecond line" >> data.txt
Decho "First line" > data.txt && echo "Second line" >> data.txt
Attempts:
2 left
💡 Hint
Appending multiple lines can be done by multiple echo commands with >>.
🔧 Debug
advanced
2:00remaining
Why does this append command overwrite the file instead of appending?
A user runs:
echo "New entry" > file.txt >> file.txt

What is the problem with this command?
AThe command syntax is invalid and causes an error
BThe > operator runs first and overwrites the file before >> appends
CThe >> operator overwrites the file, not appends
DThe echo command ignores redirection operators
Attempts:
2 left
💡 Hint
Order of redirection operators matters in bash.
🚀 Application
expert
2:30remaining
How to append output of a command to a file safely in a script?
You want to append the output of date command to timestamps.log inside a bash script. Which line is the best to do this safely and avoid overwriting?
Adate >> timestamps.log
Becho $(date) >> timestamps.log && > timestamps.log
Cecho $(date) > timestamps.log
Ddate > timestamps.log
Attempts:
2 left
💡 Hint
Appending means adding without removing existing content.

Practice

(1/5)
1. What does the >> operator do in bash scripting?
easy
A. It adds new content to the end of a file without deleting existing data.
B. It deletes the file before writing new content.
C. It reads the content of a file.
D. It copies a file to another location.

Solution

  1. Step 1: Understand the operator function

    The >> operator appends data to a file, meaning it adds new content at the end without removing what is already there.
  2. Step 2: Compare with other operators

    Unlike > which overwrites, >> keeps existing content and adds more.
  3. Final Answer:

    It adds new content to the end of a file without deleting existing data. -> Option A
  4. Quick Check:

    >> appends content [OK]
Hint: Remember: double > means add, single > means replace [OK]
Common Mistakes:
  • Confusing >> with > which overwrites files
  • Thinking >> deletes the file first
  • Assuming >> reads file content
2. Which of the following is the correct syntax to append the text "Hello" to a file named greetings.txt?
easy
A. echo Hello < greetings.txt
B. echo Hello > greetings.txt
C. echo Hello >> greetings.txt
D. echo Hello | greetings.txt

Solution

  1. Step 1: Identify the append operator

    To add text to the end of a file, use >> after the command.
  2. Step 2: Match syntax with the command

    echo Hello >> greetings.txt appends "Hello" to the file without deleting existing content.
  3. Final Answer:

    echo Hello >> greetings.txt -> Option C
  4. Quick Check:

    Use >> to append text [OK]
Hint: Use echo text >> filename to append text [OK]
Common Mistakes:
  • Using single > which overwrites the file
  • Using < which is for input redirection
  • Using pipe | incorrectly to write to file
3. What will be the content of log.txt after running these commands?
echo "Start" > log.txt
echo "Step 1" >> log.txt
echo "Step 2" >> log.txt
medium
A. Step 1 Step 2
B. Start Step 2
C. Start
D. Start Step 1 Step 2

Solution

  1. Step 1: Analyze the first command

    echo "Start" > log.txt creates or overwrites log.txt with "Start".
  2. Step 2: Analyze the next two append commands

    Both echo "Step 1" >> log.txt and echo "Step 2" >> log.txt add lines at the end without deleting existing content.
  3. Final Answer:

    Start Step 1 Step 2 -> Option D
  4. Quick Check:

    First > overwrites, then >> appends [OK]
Hint: First > overwrites, then >> adds lines [OK]
Common Mistakes:
  • Thinking >> overwrites file
  • Ignoring newline added by echo
  • Assuming only last echo remains
4. You want to append the line "Backup complete" to backup.log, but your script uses echo "Backup complete" > backup.log. What is the problem?
medium
A. The command appends but adds extra blank lines.
B. The command overwrites the file instead of appending.
C. The file backup.log will not be created.
D. The command will cause a syntax error.

Solution

  1. Step 1: Understand the operator used

    The single > operator overwrites the file content, replacing it with new text.
  2. Step 2: Identify correct operator for appending

    To add text without deleting existing content, >> should be used instead.
  3. Final Answer:

    The command overwrites the file instead of appending. -> Option B
  4. Quick Check:

    Use >> to append, not > [OK]
Hint: Use >> to append, > overwrites [OK]
Common Mistakes:
  • Using > when >> is needed
  • Expecting append but file is replaced
  • Thinking > causes syntax error
5. You have a script that logs user actions to actions.log. You want to ensure the file is created if missing and new actions are added without deleting old logs. Which command correctly appends the text "User logged in" to the file?
hard
A. echo "User logged in" >> actions.log
B. echo "User logged in" > actions.log
C. cat "User logged in" >> actions.log
D. echo "User logged in" < actions.log

Solution

  1. Step 1: Confirm file creation and appending

    The >> operator appends text and creates the file if it does not exist.
  2. Step 2: Validate command correctness

    echo "User logged in" >> actions.log appends the message correctly without deleting old logs.
  3. Final Answer:

    echo "User logged in" >> actions.log -> Option A
  4. Quick Check:

    >> appends and creates file if missing [OK]
Hint: Use echo text >> file to append and create file if missing [OK]
Common Mistakes:
  • Using > which overwrites logs
  • Using < which is input redirection
  • Using cat incorrectly to append text
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