How to append to a file?

0109

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

  1. Using the echo command:

    echo "New content to append" >> filename.txt
  2. 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
  3. Run the script:
    Save the script as append_file.sh and 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.

0 Comments

no data
Be the first to share your comment!