bashecho

Add to file if exists and create if not


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)?


Solution

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