append to file code example

Example 1: bash add text to file

echo "hello world" >> my_file.txt

Example 2: bash append file

# syntax (note: the -e switch is to allow for backslash escapes)
echo -e "<string-to-append>" >> <file-to-append-to>

# example 
echo -e "Hello there, new line!" >> RandomWorld.txt

# -----------------------------------------------
# In order to see the effect of -e, append the following string:
"Hello there,\n new line!"

Example 3: bash append to a file

# Basic syntax:
command >> output_file # Append stdout to output_file

# Note, use > to overwrite file contents and >> to append to a file
# Note, if the output_file doesn't exist, it will be created