0
0
Bash Scriptingscripting~15 mins

Writing to files (echo, printf) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Writing to files using echo and printf in Bash
📖 Scenario: You are creating a simple log file for a small script that tracks daily tasks. You want to write messages to a file to keep a record.
🎯 Goal: Learn how to write text lines to a file using echo and printf commands in Bash scripting.
📋 What You'll Learn
Create a text file with initial content using echo
Add a configuration variable for the filename
Use printf to format and append text to the file
Display the final content of the file
💡 Why This Matters
🌍 Real World
Writing logs or reports to files is common in automation scripts to keep track of actions or errors.
💼 Career
Knowing how to write and append to files is essential for scripting tasks in system administration, DevOps, and automation roles.
Progress0 / 4 steps
1
Create a file with initial content using echo
Use echo to write the exact text "Daily Tasks Log" into a file named tasks.txt. Overwrite the file if it exists.
Bash Scripting
Need a hint?

Use echo "text" > filename to write text to a file, replacing any existing content.

2
Add a variable for the filename
Create a variable called file and set it to the string "tasks.txt".
Bash Scripting
Need a hint?

Use file="tasks.txt" to assign the filename to a variable.

3
Append formatted text using printf
Use printf to append the exact line "- Task 1: Write script" to the file stored in the variable file. Use the append operator >>.
Bash Scripting
Need a hint?

Use printf "text\n" >> "$file" to append a line with a newline character.

4
Display the content of the file
Use cat to display the content of the file stored in the variable file.
Bash Scripting
Need a hint?

Use cat "$file" to show the file content.