You can append with redirection as well as overwrite a file

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the bash category.

Last Updated: 2024-04-25

I needed to run a script generate_seed_sql once for every DB table I had. FYI: The script printed generated sql on STDOUT. I wanted to save the SQL generated for all the tables into a single file seeds.sql, so I wrote the following:

tables="products\nusers\norders"
for $table in $tables; do
  generate_seed_sql $table > seeds.sql
done

When I checked the seeds.sql file, however, only the sql from the last iteration, i.e. the orders table, was there.

I should have written >> seeds.sql to append instead of over-writing.