To append to a file in Bash, you can use the >> operator. This operator allows you to add new content to the end of an existing file without deleting its current contents. Here’s how you can do it:
Example
-
Using the echo command:
echo "New content to append" >> filename.txt -
Using a Bash script:
You can create a Bash script to append content. Here’s an example script:#!/bin/bash # Display existing content echo "Before appending the file" cat filename.txt # Append new content echo "Learning Laravel 5" >> filename.txt # Display updated content echo "After appending the file" cat filename.txt -
Run the script:
Save the script asappend_file.shand run it:bash append_file.sh
This will show the existing content of the file, append the new content, and then display the updated content.
