Append file contents to the bottom of existing file in Bash

BashSedAwkAppend

Bash Problem Overview


> Possible Duplicate:
> Shell script to append text to each file?
> How to append output to the end of text file in SHELL Script?

I'm trying to work out the best way to insert api details into a pre-existing config. I thought about using sed to insert the contents of the api text file to the bottom of the config.inc file. I've started the script but it doesn't work and it wipes the file.

#!/bin/bash

CONFIG=/home/user/config.inc
API=/home/user/api.txt

sed -e "\$a $API" > $CONFIG

What am I doing wrong?

Bash Solutions


Solution 1 - Bash

This should work:

 cat "$API" >> "$CONFIG"

You need to use the >> operator to append to a file. Redirecting with > causes the file to be overwritten. (truncated).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGrimlockzView Question on Stackoverflow
Solution 1 - BashWilliam PursellView Answer on Stackoverflow