0
0
Bash Scriptingscripting~5 mins

Appending to files (>>) in Bash Scripting

Choose your learning style9 modes available
Introduction
Appending lets you add new information to the end of a file without erasing what is already there.
You want to keep a log of events happening in a script.
You need to add new lines to a configuration file without deleting existing settings.
You want to save multiple outputs from commands into one file over time.
You are collecting user inputs and want to store them one after another in a file.
Syntax
Bash Scripting
command >> filename
The double greater-than sign (>>) adds output to the file without removing existing content.
If the file does not exist, it will be created automatically.
Examples
Adds the word Hello to the end of greetings.txt.
Bash Scripting
echo "Hello" >> greetings.txt
Appends the current date and time to log.txt.
Bash Scripting
date >> log.txt
Adds the list of files in the current folder to files_list.txt.
Bash Scripting
ls >> files_list.txt
Sample Program
This script adds two lines to example.txt and then shows the file content.
Bash Scripting
#!/bin/bash

echo "First line" >> example.txt
echo "Second line" >> example.txt
cat example.txt
OutputSuccess
Important Notes
Using a single > will overwrite the file, deleting previous content.
Make sure you have write permission for the file or folder.
Appending is useful for logs because it keeps old data safe.
Summary
>> adds new content to the end of a file without deleting existing data.
If the file does not exist, it creates it automatically.
Use >> to keep adding information like logs or lists.