Add to file if exists and create if not

Mark Roddy picture Mark Roddy · Aug 11, 2009 · Viewed 22.2k times · Source

I am working on a bash script that needs to take a single line and add it to the end of a file if it exists, and if it does not exist create the file with the line.

I have so far:

    if [ ! -e /path/to/file ]; then
        echo $some_line > /path/to/file
    else
        ???
    fi

How do I perform the operation that should go in the else (adding the line of text to the existing file)?

Answer

John Millikin picture John Millikin · Aug 11, 2009

Use two angles: echo $some_line >> /path/to/file